tests: start adding quic test server utilities · nodejs/node@cff138c · GitHub
Skip to content

Commit cff138c

Browse files
committed
tests: start adding quic test server utilities
PR-URL: #59946 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent 65a32ba commit cff138c

5 files changed

Lines changed: 216 additions & 38 deletions

File tree

deps/ngtcp2/ngtcp2.gyp

Lines changed: 20 additions & 38 deletions

test/common/quic/test-client.mjs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { resolve } from 'node:path';
2+
import { spawn } from 'node:child_process';
3+
4+
export default class QuicTestClient {
5+
#pathToClient;
6+
#runningProcess;
7+
8+
constructor() {
9+
this.#pathToClient = resolve(process.execPath, '../ngtcp2_test_client');
10+
console.log(this.#pathToClient);
11+
}
12+
13+
help(options = { stdio: 'inherit' }) {
14+
const { promise, resolve, reject } = Promise.withResolvers();
15+
const proc = spawn(this.#pathToClient, ['--help'], options);
16+
proc.on('error', reject);
17+
proc.on('exit', (code, signal) => {
18+
if (code === 0) {
19+
resolve();
20+
} else {
21+
reject(new Error(`Process exited with code ${code} and signal ${signal}`));
22+
}
23+
});
24+
return promise;
25+
}
26+
27+
run(address, port, uri, options = { stdio: 'inherit' }) {
28+
const { promise, resolve, reject } = Promise.withResolvers();
29+
if (this.#runningProcess) {
30+
reject(new Error('Server is already running'));
31+
return promise;
32+
}
33+
const args = [
34+
address,
35+
port,
36+
uri ?? '',
37+
];
38+
this.#runningProcess = spawn(this.#pathToClient, args, options);
39+
this.#runningProcess.on('error', (err) => {
40+
this.#runningProcess = undefined;
41+
reject(err);
42+
});
43+
this.#runningProcess.on('exit', (code, signal) => {
44+
if (code === 0) {
45+
resolve();
46+
} else {
47+
if (code === null && signal === 'SIGTERM') {
48+
// Normal termination due to stop() being called.
49+
resolve();
50+
return;
51+
}
52+
reject(new Error(`Process exited with code ${code} and signal ${signal}`));
53+
}
54+
});
55+
return promise;
56+
}
57+
58+
stop() {
59+
if (this.#runningProcess) {
60+
this.#runningProcess.kill();
61+
this.#runningProcess = undefined;
62+
}
63+
}
64+
};

test/common/quic/test-server.mjs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { resolve } from 'node:path';
2+
import { spawn } from 'node:child_process';
3+
4+
export default class QuicTestServer {
5+
#pathToServer;
6+
#runningProcess;
7+
8+
constructor() {
9+
this.#pathToServer = resolve(process.execPath, '../ngtcp2_test_server');
10+
console.log(this.#pathToServer);
11+
}
12+
13+
help(options = { stdio: 'inherit' }) {
14+
const { promise, resolve, reject } = Promise.withResolvers();
15+
const proc = spawn(this.#pathToServer, ['--help'], options);
16+
proc.on('error', reject);
17+
proc.on('exit', (code, signal) => {
18+
if (code === 0) {
19+
resolve();
20+
} else {
21+
reject(new Error(`Process exited with code ${code} and signal ${signal}`));
22+
}
23+
});
24+
return promise;
25+
}
26+
27+
run(address, port, keyFile, certFile, options = { stdio: 'inherit' }) {
28+
const { promise, resolve, reject } = Promise.withResolvers();
29+
if (this.#runningProcess) {
30+
reject(new Error('Server is already running'));
31+
return promise;
32+
}
33+
const args = [
34+
address,
35+
port,
36+
keyFile,
37+
certFile,
38+
];
39+
this.#runningProcess = spawn(this.#pathToServer, args, options);
40+
this.#runningProcess.on('error', (err) => {
41+
this.#runningProcess = undefined;
42+
reject(err);
43+
});
44+
this.#runningProcess.on('exit', (code, signal) => {
45+
this.#runningProcess = undefined;
46+
if (code === 0) {
47+
resolve();
48+
} else {
49+
if (code === null && signal === 'SIGTERM') {
50+
// Normal termination due to stop() being called.
51+
resolve();
52+
return;
53+
}
54+
reject(new Error(`Process exited with code ${code} and signal ${signal}`));
55+
}
56+
});
57+
return promise;
58+
}
59+
60+
stop() {
61+
if (this.#runningProcess) {
62+
this.#runningProcess.kill();
63+
this.#runningProcess = undefined;
64+
}
65+
}
66+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Flags: --experimental-quic
2+
import { hasQuic, isAIX, isWindows, skip } from '../common/index.mjs';
3+
import { rejects } from 'node:assert';
4+
5+
if (!hasQuic) {
6+
skip('QUIC support is not enabled');
7+
}
8+
if (isAIX) {
9+
// AIX does not support some of the networking features used in the ngtcp2
10+
// example server and client.
11+
skip('QUIC third-party tests are disabled on AIX');
12+
}
13+
if (isWindows) {
14+
// Windows does not support the [Li/U]nix specific headers and system calls
15+
// required by the ngtcp2 example server/client.
16+
skip('QUIC third-party tests are disabled on Windows');
17+
}
18+
19+
const { default: QuicTestClient } = await import('../common/quic/test-client.mjs');
20+
21+
const client = new QuicTestClient();
22+
23+
// If this completes without throwing, the test passes.
24+
await client.help({ stdio: 'ignore' });
25+
26+
setTimeout(() => {
27+
client.stop();
28+
}, 100);
29+
30+
// We expect this to fail since there's no server running.
31+
await rejects(client.run('localhost', '12345', undefined, { stdio: 'ignore' }),
32+
{ message: /Process exited with code 1 and signal null/ });
Lines changed: 34 additions & 0 deletions

0 commit comments

Comments
 (0)