Add graceful shutdown to TestServer
這個提交存在於:
父節點
9c40914bba
當前提交
4a4b5e1303
共有 1 個檔案被更改,包括 18 行新增 和 6 行删除
|
@ -37,16 +37,28 @@ export default class TestServer {
|
|||
return `http://${addr.address}:${addr.port}${path}`
|
||||
}
|
||||
|
||||
listen() {
|
||||
this.http = http.createServer(this.app)
|
||||
this.http.listen(this.port, this.address);
|
||||
start(): Promise<void> {
|
||||
if (this.http) {
|
||||
throw new Error('http server already started');
|
||||
}
|
||||
|
||||
close(): void {
|
||||
if (!this.http) {
|
||||
return;
|
||||
this.http = http.createServer(this.app)
|
||||
return new Promise((resolve) => {
|
||||
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;
|
||||
resolve();
|
||||
});
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
新增問題並參考