Skip to content

Commit 5c6f34a

Browse files
committed
tests: start adding quic test server utilities
1 parent bc131b7 commit 5c6f34a

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { default as QuicTestServer } from '../common/quic/test-server.mjs';
2+
import * as fixtures from '../common/fixtures.mjs';
3+
4+
const server = new QuicTestServer();
5+
const fixturesPath = fixtures.path();
6+
7+
// If this completes without throwing, the test passes.
8+
await server.help({ stdio: 'ignore'});
9+
10+
setTimeout(() => {
11+
server.stop();
12+
}, 100);
13+
14+
await server.run('localhost', '12345',
15+
`${fixturesPath}/keys/agent1-key.pem`,
16+
`${fixturesPath}/keys/agent1-cert.pem`,
17+
{ stdio: 'inherit' });

0 commit comments

Comments
 (0)