mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
Properly shutdown testing http server
It turned out that server.close() does not shutdown server but stops it from accepting *new* connections. It's our responsibility to destroy all the current connections, if any.
This commit is contained in:
@@ -42,19 +42,35 @@ class SimpleServer {
|
||||
*/
|
||||
constructor(dirPath, port) {
|
||||
this._server = http.createServer(this._onRequest.bind(this));
|
||||
this._server.on('connection', socket => this._onSocket(socket));
|
||||
this._wsServer = new WebSocketServer({server: this._server});
|
||||
this._wsServer.on('connection', this._onWebSocketConnection.bind(this));
|
||||
this._server.listen(port);
|
||||
this._dirPath = dirPath;
|
||||
|
||||
/** @type {!Set<!net.Socket>} */
|
||||
this._sockets = new Set();
|
||||
|
||||
/** @type {!Map<string, function(!IncomingMessage, !ServerResponse)>} */
|
||||
this._routes = new Map();
|
||||
/** @type {!Map<string, !Promise>} */
|
||||
this._requestSubscribers = new Map();
|
||||
}
|
||||
|
||||
stop() {
|
||||
this._server.close();
|
||||
_onSocket(socket) {
|
||||
this._sockets.add(socket);
|
||||
socket.once('close', () => this._sockets.delete(socket));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {!Promise}
|
||||
*/
|
||||
async stop() {
|
||||
this.reset();
|
||||
for (let socket of this._sockets)
|
||||
socket.destroy();
|
||||
this._sockets.clear();
|
||||
await new Promise(x => this._server.close(x));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user