|
| 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 | +importassertfrom'node:assert'; |
| 8 | + |
| 9 | +const{ ok }=assert; |
| 10 | + |
| 11 | +if(!hasQuic){ |
| 12 | +skip('QUIC is not enabled'); |
| 13 | +} |
| 14 | + |
| 15 | +const{ listen, connect }=awaitimport('../common/quic.mjs'); |
| 16 | +const{ bytes }=awaitimport('stream/iter'); |
| 17 | + |
| 18 | +constencoder=newTextEncoder(); |
| 19 | +constpayload=encoder.encode('hello rtt'); |
| 20 | +constserverDone=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 | +constserverEndpoint=awaitlisten(mustCall((serverSession)=>{ |
| 26 | +serverSession.onstream=mustCall(async(stream)=>{ |
| 27 | +constdata=awaitbytes(stream); |
| 28 | +ok(data.byteLength>0); |
| 29 | +stream.writer.endSync(); |
| 30 | +awaitstream.closed; |
| 31 | +serverSession.close(); |
| 32 | +serverDone.resolve(); |
| 33 | +}); |
| 34 | +}),{ |
| 35 | +initialRtt: 1,// 1ms |
| 36 | +}); |
| 37 | + |
| 38 | +constclientSession=awaitconnect(serverEndpoint.address,{ |
| 39 | +initialRtt: 1,// 1ms |
| 40 | +}); |
| 41 | +awaitclientSession.opened; |
| 42 | + |
| 43 | +conststream=awaitclientSession.createBidirectionalStream({ |
| 44 | +body: payload, |
| 45 | +}); |
| 46 | + |
| 47 | +forawait(const_ofstream){/* drain */}// eslint-disable-line no-unused-vars |
| 48 | +awaitstream.closed; |
| 49 | +awaitserverDone.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 | +constsmoothedRtt=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 | +awaitclientSession.close(); |
| 60 | +awaitserverEndpoint.close(); |
0 commit comments