-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
Expand file tree
/
Copy pathtest-quic-session-close-error-code.mjs
More file actions
169 lines (142 loc) Β· 5.35 KB
/
test-quic-session-close-error-code.mjs
File metadata and controls
169 lines (142 loc) Β· 5.35 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Flags: --experimental-quic --no-warnings
// Test: session close/destroy with application and transport error codes
// .
// Application error propagated as ERR_QUIC_APPLICATION_ERROR.
// Session close with specific app error code β peer receives it.
// Verifies that close() and destroy() with { code, type, reason } options
// send the correct CONNECTION_CLOSE frame, and the peer receives the
// correct error type, code, and reason.
import { hasQuic, skip, mustCall } from '../common/index.mjs';
import assert from 'node:assert';
import { setTimeout } from 'node:timers/promises';
const { strictEqual, rejects } = assert;
if (!hasQuic) {
skip('QUIC is not enabled');
}
const { listen, connect } = await import('../common/quic.mjs');
// --- Test 1: close() with application error code ---
// The client closes with an application error. The server receives
// ERR_QUIC_APPLICATION_ERROR with the exact code and reason.
{
const serverGot = Promise.withResolvers();
const serverEndpoint = await listen(mustCall(async (serverSession) => {
serverSession.onerror = mustCall((err) => {
strictEqual(err.code, 'ERR_QUIC_APPLICATION_ERROR');
strictEqual(err.message.includes('42'), true,
'error message should contain the code');
strictEqual(err.message.includes('client shutdown'), true,
'error message should contain the reason');
strictEqual(err.errorCode, 42n);
strictEqual(err.type, 'application');
strictEqual(err.reason, 'client shutdown');
});
await rejects(serverSession.closed, {
code: 'ERR_QUIC_APPLICATION_ERROR',
errorCode: 42n,
reason: 'client shutdown',
});
serverGot.resolve();
}));
const clientSession = await connect(serverEndpoint.address, {
reuseEndpoint: false,
});
await clientSession.opened;
// Small delay to ensure handshake is fully confirmed so ngtcp2
// generates the 1-RTT APPLICATION CONNECTION_CLOSE frame.
await setTimeout(100);
// close() with application error β the client's closed promise
// resolves because the close was locally initiated (intentional).
// The peer receives the error code, but the local side is not in error.
await clientSession.close({
code: 42n,
type: 'application',
reason: 'client shutdown',
});
await serverGot.promise;
await serverEndpoint.close();
}
// --- Test 2: close() with transport error code ---
// The client closes with a transport error. The server receives
// ERR_QUIC_TRANSPORT_ERROR with the exact code.
{
const serverGot = Promise.withResolvers();
const serverEndpoint = await listen(mustCall(async (serverSession) => {
serverSession.onerror = mustCall((err) => {
strictEqual(err.code, 'ERR_QUIC_TRANSPORT_ERROR');
strictEqual(err.message.includes('1'), true,
'error message should contain the code');
strictEqual(err.errorCode, 1n);
strictEqual(err.type, 'transport');
});
await rejects(serverSession.closed, {
code: 'ERR_QUIC_TRANSPORT_ERROR',
});
serverGot.resolve();
}));
const clientSession = await connect(serverEndpoint.address, {
reuseEndpoint: false,
});
await clientSession.opened;
await setTimeout(100);
// close() with transport error β resolves locally (intentional).
await clientSession.close({ code: 1n });
await serverGot.promise;
await serverEndpoint.close();
}
// --- Test 3: destroy() with application error code ---
// The client destroys with both a JS error and a QUIC error code.
// The peer receives the QUIC application error.
{
const serverGot = Promise.withResolvers();
const serverEndpoint = await listen(mustCall(async (serverSession) => {
serverSession.onerror = mustCall((err) => {
strictEqual(err.code, 'ERR_QUIC_APPLICATION_ERROR');
strictEqual(err.message.includes('99'), true);
strictEqual(err.errorCode, 99n);
strictEqual(err.type, 'application');
strictEqual(err.reason, 'destroy with code');
});
await rejects(serverSession.closed, {
code: 'ERR_QUIC_APPLICATION_ERROR',
});
serverGot.resolve();
}));
const clientSession = await connect(serverEndpoint.address, {
reuseEndpoint: false,
onerror: mustCall((err) => {
// The JS error passed to destroy is delivered via onerror.
strictEqual(err.message, 'fatal error');
}),
});
await clientSession.opened;
await setTimeout(100);
const jsError = new Error('fatal error');
clientSession.destroy(jsError, {
code: 99n,
type: 'application',
reason: 'destroy with code',
});
// The closed promise rejects with the JS error, not the QUIC error.
await rejects(clientSession.closed, jsError);
await serverGot.promise;
await serverEndpoint.close();
}
// --- Test 4: close() with no options (default behavior) ---
// Verify the default close sends NO_ERROR and the peer closes cleanly.
{
const serverGot = Promise.withResolvers();
const serverEndpoint = await listen(mustCall(async (serverSession) => {
// No onerror β clean close should not trigger errors.
await serverSession.closed;
serverGot.resolve();
}));
const clientSession = await connect(serverEndpoint.address, {
reuseEndpoint: false,
});
await clientSession.opened;
await setTimeout(100);
// Default close β no error code, clean shutdown.
await clientSession.close();
await serverGot.promise;
await serverEndpoint.close();
}