|
| 1 | +// Flags: --experimental-quic --experimental-stream-iter --no-warnings |
| 2 | + |
| 3 | +// Test: initialRtt session option is accepted and the session functions |
| 4 | +// correctly with a custom initial RTT estimate. |
| 5 | + |
| 6 | +import { hasQuic, skip, mustCall } from '../common/index.mjs'; |
| 7 | +import assert from 'node:assert'; |
| 8 | + |
| 9 | +const { ok } = assert; |
| 10 | + |
| 11 | +if (!hasQuic) { |
| 12 | + skip('QUIC is not enabled'); |
| 13 | +} |
| 14 | + |
| 15 | +const { listen, connect } = await import('../common/quic.mjs'); |
| 16 | +const { bytes } = await import('stream/iter'); |
| 17 | + |
| 18 | +const encoder = new TextEncoder(); |
| 19 | +const payload = encoder.encode('hello rtt'); |
| 20 | +const serverDone = Promise.withResolvers(); |
| 21 | + |
| 22 | +// Use a low initialRtt (1ms) to simulate a low-latency environment. |
| 23 | +// The session should complete successfully and the smoothed RTT in |
| 24 | +// stats should converge to a value well below the default 333ms. |
| 25 | +const serverEndpoint = await listen(mustCall((serverSession) => { |
| 26 | + serverSession.onstream = mustCall(async (stream) => { |
| 27 | + const data = await bytes(stream); |
| 28 | + ok(data.byteLength > 0); |
| 29 | + stream.writer.endSync(); |
| 30 | + await stream.closed; |
| 31 | + serverSession.close(); |
| 32 | + serverDone.resolve(); |
| 33 | + }); |
| 34 | +}), { |
| 35 | + initialRtt: 1, // 1ms |
| 36 | +}); |
| 37 | + |
| 38 | +const clientSession = await connect(serverEndpoint.address, { |
| 39 | + initialRtt: 1, // 1ms |
| 40 | +}); |
| 41 | +await clientSession.opened; |
| 42 | + |
| 43 | +const stream = await clientSession.createBidirectionalStream({ |
| 44 | + body: payload, |
| 45 | +}); |
| 46 | + |
| 47 | +for await (const _ of stream) { /* drain */ } // eslint-disable-line no-unused-vars |
| 48 | +await stream.closed; |
| 49 | +await serverDone.promise; |
| 50 | + |
| 51 | +// After data exchange, the smoothed RTT should have converged to a |
| 52 | +// realistic value. On loopback it should be well under 10ms (10,000,000ns). |
| 53 | +// The stat is in nanoseconds. |
| 54 | +const smoothedRtt = clientSession.stats.smoothedRtt; |
| 55 | +ok(smoothedRtt > 0n, 'smoothedRtt should be non-zero after data exchange'); |
| 56 | +ok(smoothedRtt < 10_000_000n, |
| 57 | + `smoothedRtt should be under 10ms on loopback, got ${smoothedRtt}ns`); |
| 58 | + |
| 59 | +await clientSession.close(); |
| 60 | +await serverEndpoint.close(); |
0 commit comments