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:
Andrey Lushnikov
2017-07-07 08:55:30 -07:00
parent 83b8af6987
commit 18b2a46a83
2 changed files with 22 additions and 6 deletions

View File

@@ -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));
}
/**