Skip to content

Commit e390b1a

Browse files
committed
Add bidirectional server echo test
1 parent f60c0d2 commit e390b1a

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Flags: --experimental-quic --no-warnings
2+
3+
import { hasQuic, skip, mustCall } from '../common/index.mjs';
4+
import { ok, strictEqual, deepStrictEqual } from 'node:assert';
5+
import { readKey } from '../common/fixtures.mjs';
6+
import { KNOWN_BYTES_LONG, uint8concat } from '../common/quic/test-helpers.mjs';
7+
import { TransformStream } from 'node:stream/web';
8+
9+
if (!hasQuic) {
10+
skip('QUIC is not enabled');
11+
}
12+
13+
// Import after the hasQuic check
14+
const { listen, connect } = await import('node:quic');
15+
const { createPrivateKey } = await import('node:crypto');
16+
17+
const keys = createPrivateKey(readKey('agent1-key.pem'));
18+
const certs = readKey('agent1-cert.pem');
19+
20+
const finished = Promise.withResolvers();
21+
22+
const serverEndpoint = await listen(async (serverSession) => {
23+
serverSession.onstream = mustCall(async (stream) => {
24+
strictEqual(stream.direction, 'bidi', 'Expects an bidirectional stream');
25+
stream.closed.catch(() => {
26+
// ignore
27+
});
28+
const serverTransformStream = new TransformStream();
29+
stream.setOutbound(serverTransformStream.readable);
30+
await stream.readable.pipeTo(serverTransformStream.writable);
31+
32+
}, 1);
33+
34+
await finished.promise;
35+
serverSession.closed.catch((err) => {
36+
// ignore the error
37+
});
38+
serverSession.close();
39+
}, { keys, certs });
40+
41+
// The server must have an address to connect to after listen resolves.
42+
ok(serverEndpoint.address !== undefined);
43+
44+
const clientSession = await connect(serverEndpoint.address);
45+
await clientSession.opened;
46+
47+
48+
const transformStream = new TransformStream();
49+
const sendStream = await clientSession.createBidirectionalStream({ body: transformStream.readable });
50+
sendStream.closed.catch(() => {
51+
// ignore
52+
});
53+
strictEqual(sendStream.direction, 'bidi');
54+
const writeToStream = async () => {
55+
const clientWritable = transformStream.writable;
56+
const writer = clientWritable.getWriter();
57+
for (const chunk of KNOWN_BYTES_LONG) {
58+
await writer.ready;
59+
await writer.write(chunk);
60+
}
61+
await writer.ready;
62+
await writer.close();
63+
};
64+
const readFromStream = async () => {
65+
const reader = sendStream.readable.getReader();
66+
const readChunks = [];
67+
while (true) {
68+
const { done, value } = await reader.read();
69+
if (value) {
70+
ok(value instanceof Uint8Array, 'Expects value to be a Uint8Array');
71+
readChunks.push(value);
72+
}
73+
if (done) break;
74+
}
75+
// Now compare what we got
76+
deepStrictEqual(uint8concat(KNOWN_BYTES_LONG), uint8concat(readChunks));
77+
};
78+
79+
await Promise.all([writeToStream(), readFromStream()]);
80+
clientSession.closed.catch((err) => {
81+
// ignore the error
82+
});
83+
clientSession.close();
84+
finished.resolve();

0 commit comments

Comments
 (0)