Skip to content

Commit 630b8aa

Browse files
jasnellohodson
andauthored
Add abortable fetch tests (#1212)
Co-authored-by: Orion Hodson <[email protected]>
1 parent cf4457c commit 630b8aa

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import {
2+
strictEqual,
3+
ok,
4+
throws
5+
} from 'node:assert';
6+
7+
// Test for the AbortSignal and AbortController standard Web API implementations.
8+
// The implementation for these are in api/basics.{h|c++}
9+
10+
export const abortControllerAlreadyAborted = {
11+
async test(ctrl, env) {
12+
const ac = new AbortController();
13+
ac.abort();
14+
try {
15+
const result = await env.subrequest.fetch('http://example.org', { signal: ac.signal });
16+
throw new Error('should have failed');
17+
} catch (err) {
18+
strictEqual(err.message, 'The operation was aborted');
19+
}
20+
}
21+
};
22+
23+
export const alreadyAborted = {
24+
async test(ctrl, env) {
25+
const signal = AbortSignal.abort('boom');
26+
try {
27+
await env.subrequest.fetch('http://example.org', { signal });
28+
throw new Error('should have failed');
29+
} catch (err) {
30+
strictEqual(err, 'boom');
31+
}
32+
}
33+
};
34+
35+
export const timedAbort = {
36+
async test(ctrl, env) {
37+
const signal = AbortSignal.timeout(100);
38+
try {
39+
await env.subrequest.fetch('http://example.org', { signal });
40+
throw new Error('should have failed');
41+
} catch (err) {
42+
strictEqual(err.message, 'The operation was aborted due to timeout');
43+
}
44+
}
45+
};
46+
47+
export const abortControllerSyncAbort = {
48+
async test(ctrl, env) {
49+
const ac = new AbortController();
50+
try {
51+
const promise = env.subrequest.fetch('http://example.org', { signal: ac.signal });
52+
ac.abort();
53+
await promise;
54+
throw new Error('should have failed');
55+
} catch (err) {
56+
strictEqual(err.message, 'The operation was aborted');
57+
}
58+
}
59+
};
60+
61+
export const asyncSubrequest = {
62+
async test(ctrl, env) {
63+
try {
64+
await env.subrequest.fetch('http://example.org/sub');
65+
throw new Error('should have failed');
66+
} catch (err) {
67+
strictEqual(err.message, 'The operation was aborted due to timeout');
68+
}
69+
}
70+
};
71+
72+
export const syncSubrequest = {
73+
async test(ctrl, env) {
74+
try {
75+
await env.subrequest.fetch('http://example.org/subsync');
76+
throw new Error('should have failed');
77+
} catch (err) {
78+
strictEqual(err.message, 'The operation was aborted');
79+
}
80+
}
81+
};
82+
83+
export default {
84+
async fetch(request, env) {
85+
if (request.url.endsWith('/sub')) {
86+
// Verifies that a fetch subrequest returned as the response can be canceled
87+
// asynchronously successfully.
88+
const signal = AbortSignal.timeout(100);
89+
return env.subrequest.fetch('http://example.org', { signal });
90+
} else if (request.url.endsWith('/subsync')) {
91+
// Verifies that a fetch subrequest returned as the response can be synchronously
92+
// aborted.
93+
const ac = new AbortController();
94+
const resp = env.subrequest.fetch('http://example.org', { signal: ac.signal });
95+
ac.abort();
96+
return resp;
97+
} else {
98+
await scheduler.wait(10000);
99+
return new Response("ok");
100+
}
101+
}
102+
};
103+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Workerd = import "/workerd/workerd.capnp";
2+
3+
const unitTests :Workerd.Config = (
4+
services = [
5+
( name = "abortable-fetch-test",
6+
worker = (
7+
modules = [
8+
(name = "worker", esModule = embed "abortable-fetch-test.js")
9+
],
10+
compatibilityDate = "2023-01-15",
11+
compatibilityFlags = ["nodejs_compat"],
12+
bindings = [
13+
(name = "subrequest", service = "abortable-fetch-test")
14+
]
15+
)
16+
),
17+
],
18+
);

0 commit comments

Comments
 (0)