|
| 1 | +// Flags: --experimental-quic --no-warnings |
| 2 | + |
| 3 | +// Test: when `endpoint.destroy(err)` is called on a side that has open, |
| 4 | +// fully-handshaked sessions, the cascade through `session.destroy(err)` |
| 5 | +// passes close options derived from the error so each session emits a |
| 6 | +// CONNECTION_CLOSE on the wire. The peer learns about the teardown via |
| 7 | +// that frame, not via its own idle timer. |
| 8 | +// |
| 9 | +// How the test distinguishes the two cases: |
| 10 | +// |
| 11 | +// * If CONNECTION_CLOSE is sent, the client's `session.closed` |
| 12 | +// rejects after the network round-trip with an |
| 13 | +// `ERR_QUIC_TRANSPORT_ERROR` carrying the cascaded code. |
| 14 | +// * If CONNECTION_CLOSE is NOT sent, the client only learns of the |
| 15 | +// teardown via its own idle timer, which hits `[kFinishClose]` |
| 16 | +// case 3 (`/* Idle close */`) and resolves `session.closed` |
| 17 | +// *cleanly*. The `mustCall` rejection-handler would then never |
| 18 | +// fire and the test fails. |
| 19 | +// |
| 20 | +// A short `maxIdleTimeout` keeps the failure mode fast. |
| 21 | + |
| 22 | +import { hasQuic, skip, mustCall } from '../common/index.mjs'; |
| 23 | +import assert from 'node:assert'; |
| 24 | + |
| 25 | +const { rejects, strictEqual } = assert; |
| 26 | + |
| 27 | +if (!hasQuic) { |
| 28 | + skip('QUIC is not enabled'); |
| 29 | +} |
| 30 | + |
| 31 | +const { listen, connect } = await import('../common/quic.mjs'); |
| 32 | + |
| 33 | +// `maxIdleTimeout` is measured in seconds. One second is far longer |
| 34 | +// than CONNECTION_CLOSE on loopback needs to win, while still short |
| 35 | +// enough that a regression in which `CONNECTION_CLOSE` is *not* sent |
| 36 | +// fails the test promptly: the idle-close path takes the |
| 37 | +// `[kFinishClose]` case-3 branch and resolves `session.closed` cleanly |
| 38 | +// instead of rejecting, so the rejection-handler `mustCall` below |
| 39 | +// would fail with "expected exactly 1, actual 0". |
| 40 | +const transportParams = { maxIdleTimeout: 1 }; |
| 41 | + |
| 42 | +const serverError = new Error('cascade close frame test'); |
| 43 | + |
| 44 | +// Capture the server-side session and wait for *its* `onhandshake` to |
| 45 | +// fire before triggering the cascade. The client's `session.opened` |
| 46 | +// resolves as soon as the client receives the server's TLS Finished, |
| 47 | +// which can land slightly *before* the server has processed the |
| 48 | +// client's reciprocal Finished. Without this synchronization the |
| 49 | +// server-side `kHandshakeCompleted` flag may still be `false` at |
| 50 | +// destroy time and the cascade would skip emitting `CONNECTION_CLOSE` |
| 51 | +// (which is the regression this test is designed to catch). |
| 52 | +const serverHandshake = Promise.withResolvers(); |
| 53 | +const onsession = mustCall((serverSession) => { |
| 54 | + serverSession.onhandshake = mustCall(() => { |
| 55 | + serverHandshake.resolve(); |
| 56 | + }); |
| 57 | +}); |
| 58 | +const serverEndpoint = await listen(onsession, { transportParams }); |
| 59 | + |
| 60 | +const clientSession = await connect(serverEndpoint.address, { |
| 61 | + transportParams, |
| 62 | +}); |
| 63 | +await clientSession.opened; |
| 64 | +await serverHandshake.promise; |
| 65 | + |
| 66 | +// Attach the rejection handlers BEFORE triggering destroy so neither |
| 67 | +// `serverEndpoint.closed` (rejects with `serverError` via the |
| 68 | +// `#pendingError` semantics from B7) nor `clientSession.closed` |
| 69 | +// (rejects with the transport error decoded from the CONNECTION_CLOSE |
| 70 | +// frame) ends up as an unhandled rejection in the brief window before |
| 71 | +// this test gets back to awaiting them. |
| 72 | +const serverClosedAssertion = rejects(serverEndpoint.closed, serverError); |
| 73 | +const clientClosedAssertion = rejects(clientSession.closed, mustCall((err) => { |
| 74 | + strictEqual(err.code, 'ERR_QUIC_TRANSPORT_ERROR'); |
| 75 | + return true; |
| 76 | +})); |
| 77 | + |
| 78 | +serverEndpoint.destroy(serverError); |
| 79 | + |
| 80 | +await clientClosedAssertion; |
| 81 | +await serverClosedAssertion; |
| 82 | + |
| 83 | +// Explicit cleanup: the client-side session has been rejected via |
| 84 | +// CONNECTION_CLOSE but the underlying client endpoint is still alive. |
| 85 | +// Tear it down so the event loop drains promptly. |
| 86 | +clientSession.destroy(); |
0 commit comments