-
-
Notifications
You must be signed in to change notification settings - Fork 35.6k
Expand file tree
/
Copy pathtest-quic-session-close.mjs
More file actions
107 lines (79 loc) Β· 2.71 KB
/
test-quic-session-close.mjs
File metadata and controls
107 lines (79 loc) Β· 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Flags: --experimental-quic --no-warnings
import { mustCall } from '../common/index.mjs';
import assert from 'node:assert';
import { checkQuic, createQuicPair, defaultCerts } from '../common/quic/helpers.mjs';
checkQuic();
const { listen, connect } = await import('node:quic');
// Test 1: session.close() returns a promise that resolves.
{
const { clientSession, serverSession, endpoint } = await createQuicPair();
const closePromise = clientSession.close();
assert.ok(closePromise instanceof Promise,
'close() should return a promise');
await closePromise;
// Closed should also resolve after close() completes.
await clientSession.closed;
serverSession.close();
await serverSession.closed;
await endpoint.close();
}
// Test 2: close() is idempotent -- calling it multiple times does not throw.
{
const { clientSession, serverSession, endpoint } = await createQuicPair();
clientSession.close();
clientSession.close(); // Second call should not throw.
await clientSession.closed;
serverSession.close();
await serverSession.closed;
await endpoint.close();
}
// Test 3: Endpoint survives session close and can accept new connections.
{
const { keys, certs } = await defaultCerts();
let sessionCount = 0;
const endpoint = await listen(mustCall((session) => {
sessionCount++;
session.opened.then(mustCall(() => {
session.close();
}));
}, 2), { keys, certs });
// First connection.
const client1 = await connect(endpoint.address);
await client1.opened;
client1.close();
await client1.closed;
// Second connection on the same endpoint.
const client2 = await connect(endpoint.address);
await client2.opened;
client2.close();
await client2.closed;
assert.strictEqual(sessionCount, 2);
await endpoint.close();
}
// Test 4: session.closed resolves after close() on both client and server side.
{
const { clientSession, serverSession, endpoint } = await createQuicPair();
const clientClosed = Promise.withResolvers();
const serverClosed = Promise.withResolvers();
clientSession.closed.then(mustCall(() => {
clientClosed.resolve();
}));
serverSession.closed.then(mustCall(() => {
serverClosed.resolve();
}));
clientSession.close();
serverSession.close();
await Promise.all([clientClosed.promise, serverClosed.promise]);
await endpoint.close();
}
// Test 5: session.destroyed is true after close completes.
{
const { clientSession, serverSession, endpoint } = await createQuicPair();
assert.strictEqual(clientSession.destroyed, false);
clientSession.close();
await clientSession.closed;
assert.strictEqual(clientSession.destroyed, true);
serverSession.close();
await serverSession.closed;
await endpoint.close();
}