File
Blob: src/workerd/api/node/tests/process-exit-test.js
| 1 | // Copyright (c) 2024 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 |
| 4 | import { fail, ok, rejects, strictEqual } from 'assert'; |
| 5 | |
| 6 | let called = false; |
| 7 | |
| 8 | process.exit(9999); |
| 9 | |
| 10 | export const test = { |
| 11 | async test(_, env) { |
| 12 | // We don't really have a way to verifying that the correct log messages |
| 13 | // we emitted. For now, we need to manually check the log out, and here |
| 14 | // we check only that we received an internal error and that no other |
| 15 | // lines of code ran after the process.exit call. |
| 16 | await rejects(env.subrequest.fetch('http://example.org'), { |
| 17 | message: /^The Node.js process.exit/, |
| 18 | }); |
| 19 | ok(!called); |
| 20 | }, |
| 21 | }; |
| 22 | |
| 23 | let fooCreateCount = 0; |
| 24 | |
| 25 | export const test2 = { |
| 26 | async test(_, env) { |
| 27 | // We don't really have a way to verifying that the correct log messages |
| 28 | // we emitted. For now, we need to manually check the log out, and here |
| 29 | // we check only that we received an internal error and that no other |
| 30 | // lines of code ran after the process.exit call. |
| 31 | { |
| 32 | let obj = env.foo.get( |
| 33 | env.foo.idFromName('210bd0cbd803ef7883a1ee9d86cce06f') |
| 34 | ); |
| 35 | await rejects(obj.fetch('http://example.org'), { |
| 36 | message: /^The Node.js process.exit/, |
| 37 | }); |
| 38 | await rejects(obj.fetch('http://example.org'), { |
| 39 | message: /^The Node.js process.exit/, |
| 40 | }); |
| 41 | // The durable object should have been created once, since we didn't recreate the stub. |
| 42 | strictEqual(fooCreateCount, 1); |
| 43 | |
| 44 | // If we recreate the stub and try again, then the counter will increment. |
| 45 | obj = env.foo.get(env.foo.idFromName('210bd0cbd803ef7883a1ee9d86cce06f')); |
| 46 | await rejects(obj.fetch('http://example.org'), { |
| 47 | message: /^The Node.js process.exit/, |
| 48 | }); |
| 49 | strictEqual(fooCreateCount, 2); |
| 50 | } |
| 51 | }, |
| 52 | }; |
| 53 | |
| 54 | export default { |
| 55 | async fetch() { |
| 56 | try { |
| 57 | process.exit(123); |
| 58 | } finally { |
| 59 | called = true; |
| 60 | } |
| 61 | }, |
| 62 | }; |
| 63 | |
| 64 | export class Foo { |
| 65 | constructor() { |
| 66 | fooCreateCount++; |
| 67 | } |
| 68 | |
| 69 | fetch() { |
| 70 | process.exit(123); |
| 71 | fail('Should never get here.'); |
| 72 | } |
| 73 | } |