Skip to content

Commit 51f2af6

Browse files
committed
quic: add client to server unidirectional test
1 parent 556923c commit 51f2af6

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
// The opened promise should resolve when the client finished reading
21+
const serverFinished = Promise.withResolvers();
22+
23+
24+
const serverEndpoint = await listen(async (serverSession) => {
25+
serverSession.onstream = mustCall(async (stream) => {
26+
strictEqual(stream.direction, 'uni', 'Expects an unidirectional stream');
27+
const reader = stream.readable.getReader();
28+
const readChunks = [];
29+
let readc = 0;
30+
while (true) {
31+
const { done, value } = await reader.read();
32+
// if (readc > 20) throw new Error("after read " + readc);
33+
if (value) {
34+
ok(value instanceof Uint8Array, 'Expects value to be a Uint8Array');
35+
readChunks.push(value);
36+
}
37+
if (done) break;
38+
readc++;
39+
}
40+
stream.closed.catch(() => {
41+
// ignore
42+
});
43+
// Now compare what we got
44+
deepStrictEqual(uint8concat(KNOWN_BYTES_LONG), uint8concat(readChunks));
45+
serverFinished.resolve();
46+
}, 1);
47+
48+
await serverFinished.promise;
49+
serverSession.closed.catch((err) => {
50+
// ignore the error
51+
});
52+
serverSession.close();
53+
}, { keys, certs });
54+
55+
// The server must have an address to connect to after listen resolves.
56+
ok(serverEndpoint.address !== undefined);
57+
58+
const clientSession = await connect(serverEndpoint.address);
59+
await clientSession.opened;
60+
61+
62+
const transformStream = new TransformStream();
63+
const sendStream = await clientSession.createUnidirectionalStream({ body: transformStream.readable });
64+
sendStream.closed.catch(() => {
65+
// ignore
66+
});
67+
strictEqual(sendStream.direction, 'uni');
68+
const clientWritable = transformStream.writable;
69+
const writer = clientWritable.getWriter();
70+
let run = 0
71+
for (const chunk of KNOWN_BYTES_LONG) {
72+
await writer.ready;
73+
await writer.write(chunk);
74+
run++;
75+
}
76+
await writer.ready;
77+
await writer.close();
78+
clientSession.closed.catch((err) => {
79+
// ignore the error
80+
});
81+
clientSession.close();
82+
await serverFinished.promise;

0 commit comments

Comments
 (0)