Add graceful shutdown to TestServer
This commit is contained in:
parent
9c40914bba
commit
4a4b5e1303
1 changed files with 18 additions and 6 deletions
|
@ -37,16 +37,28 @@ export default class TestServer {
|
||||||
return `http://${addr.address}:${addr.port}${path}`
|
return `http://${addr.address}:${addr.port}${path}`
|
||||||
}
|
}
|
||||||
|
|
||||||
listen() {
|
start(): Promise<void> {
|
||||||
this.http = http.createServer(this.app)
|
if (this.http) {
|
||||||
this.http.listen(this.port, this.address);
|
throw new Error('http server already started');
|
||||||
}
|
}
|
||||||
|
|
||||||
close(): void {
|
this.http = http.createServer(this.app)
|
||||||
if (!this.http) {
|
return new Promise((resolve) => {
|
||||||
return;
|
this.http!!.listen(this.port, this.address, () => {
|
||||||
|
resolve();
|
||||||
|
})
|
||||||
|
});
|
||||||
}
|
}
|
||||||
this.http.close();
|
|
||||||
|
stop(): Promise<void> {
|
||||||
|
if (!this.http) {
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
this.http!!.close(() => {
|
||||||
this.http = undefined;
|
this.http = undefined;
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue