-
Notifications
You must be signed in to change notification settings - Fork 0
/
CacheHitError.spec.ts
102 lines (95 loc) · 2.7 KB
/
CacheHitError.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { sleep } from "bun";
import { sf } from ".";
import { CacheHitError } from "./CacheHitError";
it("handles cache hit error", async () => {
const ws = new WritableStream(
{
write: (chunk, ctrl) => {
console.log("write chunk: ", chunk);
ctrl.error(
new CacheHitError("Cache hit when stream write", {
cause: { chunk },
}),
);
},
},
{ highWaterMark: 1 },
);
const w = ws.getWriter();
expect(await w.write("asdf")).toBe(undefined);
expect(await w.write("zxcv").catch(CacheHitError.nil)).toBe(null);
expect(await w.write("qwer").catch(CacheHitError.nil)).toBe(null);
expect(await w.write("qwop").catch(CacheHitError.nil)).toBe(null);
console.log("done");
});
it("handles cache hit error when stream start", async () => {
const ws = new WritableStream(
{
start: (ctrl) => {
ctrl.error(new CacheHitError("Cache hit when stream start"));
},
write: (chunk, ctrl) => {
console.log("write chunk: ", chunk);
},
},
{ highWaterMark: 1 },
);
const w = ws.getWriter();
expect(await w.write("zxcv").catch(CacheHitError.nil)).toBe(null);
expect(await w.write("qwer").catch(CacheHitError.nil)).toBe(null);
expect(await w.write("qwop").catch(CacheHitError.nil)).toBe(null);
console.log("done");
});
it("handles cache hit error with start pipeTo", async () => {
let i = 0;
const rs = new ReadableStream(
{
pull: (ctrl) => {
console.log("pull st " + i);
ctrl.enqueue(i);
++i < 10 || ctrl.close();
},
},
{ highWaterMark: 0 },
);
const ws = new WritableStream(
{
start: async (ctrl) => {
await sleep(100);
ctrl.error(new CacheHitError("Cache hit when stream start"));
},
},
{ highWaterMark: 0 }, // WARN: not writable...
);
await rs.pipeTo(ws).catch(CacheHitError.nil);
console.log("done");
});
it("handles cache hit error with write pipeTo", async () => {
const ws = new WritableStream({
write: (_, ctrl) =>
ctrl.error(new CacheHitError("Cache hit when stream write")),
});
await sf([1, 2, 3, 4]).pipeTo(ws).catch(CacheHitError.nil);
console.log("done");
});
it("handles cache hit error with pipeTo", async () => {
let i = 0;
const rs = new ReadableStream({
pull: (ctrl) => {
console.log("pull" + i);
ctrl.enqueue(i);
++i < 10 || ctrl.close();
},
});
const ws = new WritableStream(
{
write: (chunk, ctrl) => {
ctrl.error(new CacheHitError("Cache hit when stream start"));
console.log("write chunk: ", chunk);
},
},
{ highWaterMark: 1 },
);
await rs.pipeTo(ws).catch(CacheHitError.nil);
console.log("done");
});