Skip to content
File

Blob: src/workerd/io/io-context-test.js

javascript45 lines
1// Copyright (c) 2025 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4import assert from 'node:assert';
5 
6export let testHangingPromise = {
7 async test(controller, env, ctx) {
8 await assert.rejects(
9 ctx.exports.testHangingPromise.fetch('http://example.com'),
10 {
11 name: 'Error',
12 message:
13 "The Workers runtime canceled this request because it detected that your Worker's " +
14 'code had hung and would never generate a response. Refer to: ' +
15 'https://developers.cloudflare.com/workers/observability/errors/',
16 }
17 );
18 },
19 
20 async fetch(req, env, ctx) {
21 await new Promise((resolve) => {});
22 },
23};
24 
25let abortCalled = false;
26let abortReturned = false;
27 
28export let testAbort = {
29 async test(controller, env, ctx) {
30 await assert.rejects(ctx.exports.testAbort.fetch('http://example.com'), {
31 name: 'Error',
32 message: 'test abort reason',
33 });
34 
35 assert.strictEqual(abortCalled, true);
36 assert.strictEqual(abortReturned, false);
37 },
38 
39 async fetch(req, env, ctx) {
40 abortCalled = true;
41 ctx.abort(new Error('test abort reason'));
42 abortReturned = true; // shouldn't get here!
43 },
44};