From 18b2a46a83fd81238256fece0b8c6fbedc861bbd Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Fri, 7 Jul 2017 08:55:30 -0700 Subject: [PATCH] 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. --- test/SimpleServer.js | 20 ++++++++++++++++++-- test/test.js | 8 ++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/test/SimpleServer.js b/test/SimpleServer.js index 38f3da18..8b75081d 100644 --- a/test/SimpleServer.js +++ b/test/SimpleServer.js @@ -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} */ + this._sockets = new Set(); + /** @type {!Map} */ this._routes = new Map(); /** @type {!Map} */ 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)); } /** diff --git a/test/test.js b/test/test.js index a24c2297..6834c15c 100644 --- a/test/test.js +++ b/test/test.js @@ -36,10 +36,10 @@ describe('Puppeteer', function() { GoldenUtils.removeOutputDir(); })); - afterAll(function() { - server.stop(); + afterAll(SX(async function() { + await server.stop(); browser.close(); - }); + })); beforeEach(SX(async function() { page = await browser.newPage(); @@ -105,7 +105,7 @@ describe('Puppeteer', function() { }); describe('Page.injectFile', function() { - it('should fail when navigating to bad url', SX(async function() { + it('should work', SX(async function() { const helloPath = path.join(__dirname, './assets/injectedfile.js'); await page.injectFile(helloPath); const result = await page.evaluate(() => __injected);