|
| 1 | +// Flags: --experimental-quic --no-warnings |
| 2 | + |
| 3 | +// Test: terminating a worker thread that still holds live QUIC sessions. |
| 4 | +// |
| 5 | +// worker.terminate() tears the environment down without running any of the |
| 6 | +// JavaScript close paths, so the sessions are still open when the QUIC |
| 7 | +// binding is cleaned up. Sessions must be properly destroyed before reaching |
| 8 | +// ~Session. |
| 9 | + |
| 10 | +import { hasQuic, skip, mustCall } from '../common/index.mjs'; |
| 11 | +import assert from 'node:assert'; |
| 12 | +import { Worker, isMainThread, parentPort } from 'node:worker_threads'; |
| 13 | + |
| 14 | +if (!hasQuic) { |
| 15 | + skip('QUIC is not enabled'); |
| 16 | +} |
| 17 | + |
| 18 | +const { listen, connect } = await import('../common/quic.mjs'); |
| 19 | + |
| 20 | +// Launch a client and server in a worker thread, then kill it: |
| 21 | +if (!isMainThread) { |
| 22 | + // A client and a server session, both with an open stream, and neither |
| 23 | + // closed. The worker then parks forever waiting to be terminated. |
| 24 | + const serverEndpoint = await listen((session) => { |
| 25 | + session.closed.catch(() => {}); |
| 26 | + session.onstream = (stream) => { stream.closed.catch(() => {}); }; |
| 27 | + }); |
| 28 | + |
| 29 | + const clientSession = await connect(serverEndpoint.address); |
| 30 | + clientSession.closed.catch(() => {}); |
| 31 | + await clientSession.opened; |
| 32 | + const stream = await clientSession.createBidirectionalStream({ |
| 33 | + body: new Uint8Array(1), |
| 34 | + }); |
| 35 | + stream.closed.catch(() => {}); |
| 36 | + |
| 37 | + parentPort.postMessage('ready'); |
| 38 | + await new Promise(() => {}); |
| 39 | +} else { |
| 40 | + const worker = new Worker(new URL(import.meta.url)); |
| 41 | + worker.on('error', (err) => { assert.fail(err); }); |
| 42 | + worker.on('message', mustCall(async (message) => { |
| 43 | + assert.strictEqual(message, 'ready'); |
| 44 | + assert.strictEqual(await worker.terminate(), 1); |
| 45 | + })); |
| 46 | +} |
0 commit comments