Skip to content

Commit b5532b4

Browse files
committed
tests: start adding quic test server utilities
1 parent 79519d8 commit b5532b4

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Flags: --experimental-quic
2+
import { hasQuic, skip } from '../common/index.mjs';
3+
4+
if (!hasQuic) {
5+
skip('QUIC support is not enabled');
6+
}
7+
8+
const { default: QuicTestServer } = await import('../common/quic/test-server.mjs');
9+
const fixtures = await import('../common/fixtures.mjs');
10+
11+
const server = new QuicTestServer();
12+
const fixturesPath = fixtures.path();
13+
14+
// If this completes without throwing, the test passes.
15+
await server.help({ stdio: 'ignore' });
16+
17+
setTimeout(() => {
18+
server.stop();
19+
}, 100);
20+
21+
await server.run('localhost', '12345',
22+
`${fixturesPath}/keys/agent1-key.pem`,
23+
`${fixturesPath}/keys/agent1-cert.pem`,
24+
{ stdio: 'inherit' });

0 commit comments

Comments
 (0)