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) { constructor(dirPath, port) {
this._server = http.createServer(this._onRequest.bind(this)); 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 = new WebSocketServer({server: this._server});
this._wsServer.on('connection', this._onWebSocketConnection.bind(this)); this._wsServer.on('connection', this._onWebSocketConnection.bind(this));
this._server.listen(port); this._server.listen(port);
this._dirPath = dirPath; this._dirPath = dirPath;
/** @type {!Set<!net.Socket>} */
this._sockets = new Set();
/** @type {!Map<string, function(!IncomingMessage, !ServerResponse)>} */ /** @type {!Map<string, function(!IncomingMessage, !ServerResponse)>} */
this._routes = new Map(); this._routes = new Map();
/** @type {!Map<string, !Promise>} */ /** @type {!Map<string, !Promise>} */
this._requestSubscribers = new Map(); this._requestSubscribers = new Map();
} }
stop() { _onSocket(socket) {
this._server.close(); 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));
} }
/** /**

View File

@ -36,10 +36,10 @@ describe('Puppeteer', function() {
GoldenUtils.removeOutputDir(); GoldenUtils.removeOutputDir();
})); }));
afterAll(function() { afterAll(SX(async function() {
server.stop(); await server.stop();
browser.close(); browser.close();
}); }));
beforeEach(SX(async function() { beforeEach(SX(async function() {
page = await browser.newPage(); page = await browser.newPage();
@ -105,7 +105,7 @@ describe('Puppeteer', function() {
}); });
describe('Page.injectFile', 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'); const helloPath = path.join(__dirname, './assets/injectedfile.js');
await page.injectFile(helloPath); await page.injectFile(helloPath);
const result = await page.evaluate(() => __injected); const result = await page.evaluate(() => __injected);