|
| 1 | +// Flags: --experimental-quic --experimental-stream-iter --no-warnings |
| 2 | + |
| 3 | +// Test: a FIN that does not fit in the current packet is still sent. |
| 4 | +// |
| 5 | +// For a fixed body the final data and the FIN go out as separate STREAM |
| 6 | +// frames, packed into the same packet where possible. When the data leaves |
| 7 | +// only a few bytes free, the FIN-only frame does not fit and the packet is |
| 8 | +// sent without it. The stream must then be retried, or the peer never sees |
| 9 | +// the end of the stream. |
| 10 | +// |
| 11 | +// Which body size leaves too little room depends on per-packet overhead, so |
| 12 | +// this sweeps sizes around one packet's payload, one stream at a time. |
| 13 | + |
| 14 | +import { hasQuic, skip, mustCall } from '../common/index.mjs'; |
| 15 | +import assert from 'node:assert'; |
| 16 | + |
| 17 | +if (!hasQuic) { |
| 18 | + skip('QUIC is not enabled'); |
| 19 | +} |
| 20 | + |
| 21 | +const { listen, connect } = await import('../common/quic.mjs'); |
| 22 | +const { bytes } = await import('stream/iter'); |
| 23 | + |
| 24 | +const kMinSize = 1000; |
| 25 | +const kMaxSize = 1300; |
| 26 | +const kCount = kMaxSize - kMinSize + 1; |
| 27 | + |
| 28 | +const received = []; |
| 29 | +let next = Promise.withResolvers(); |
| 30 | + |
| 31 | +const serverEndpoint = await listen(mustCall((serverSession) => { |
| 32 | + serverSession.onstream = mustCall(async (stream) => { |
| 33 | + const data = await bytes(stream); |
| 34 | + received.push(data.byteLength); |
| 35 | + next.resolve(); |
| 36 | + }, kCount); |
| 37 | +})); |
| 38 | + |
| 39 | +const clientSession = await connect(serverEndpoint.address); |
| 40 | +await clientSession.opened; |
| 41 | +const closed = clientSession.closed.then(() => 'closed', () => 'closed'); |
| 42 | + |
| 43 | +for (let size = kMinSize; size <= kMaxSize; size++) { |
| 44 | + const stream = await clientSession.createUnidirectionalStream(); |
| 45 | + stream.setBody(new Uint8Array(size)); |
| 46 | + const result = await Promise.race([next.promise.then(() => 'ok'), closed]); |
| 47 | + assert.strictEqual(result, 'ok', |
| 48 | + `session closed before the FIN of a ${size}-byte ` + |
| 49 | + 'stream arrived'); |
| 50 | + next = Promise.withResolvers(); |
| 51 | + assert.strictEqual(received.at(-1), size); |
| 52 | +} |
| 53 | + |
| 54 | +assert.strictEqual(received.length, kCount); |
| 55 | + |
| 56 | +await clientSession.close(); |
| 57 | +await serverEndpoint.close(); |
0 commit comments