mirror of
https://github.com/appium/appium.git
synced 2026-10-28 12:21:20 +08:00
Merge pull request #54 from appium/isaac-close
Make sure the server is closed when we call close
This commit is contained in:
@@ -17,6 +17,18 @@ async function server (configureRoutes, port, hostname = null) {
|
||||
let app = express();
|
||||
let httpServer = http.createServer(app);
|
||||
|
||||
// http.Server.close() only stops new connections, but we need to wait until
|
||||
// all connections are closed and the `close` event is emitted
|
||||
let close = httpServer.close.bind(httpServer);
|
||||
httpServer.close = async () => {
|
||||
return await new Promise((resolve, reject) => {
|
||||
httpServer.on('close', resolve);
|
||||
close((err) => {
|
||||
if (err) reject(err);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
return await new Promise((resolve, reject) => {
|
||||
httpServer.on('error', (err) => {
|
||||
if (err.code === 'EADDRNOTAVAIL') {
|
||||
|
||||
@@ -14,8 +14,8 @@ function baseDriverE2ETests (DriverClass, defaultCaps = {}) {
|
||||
before(async () => {
|
||||
baseServer = await server(routeConfiguringFunction(d), 8181);
|
||||
});
|
||||
after(() => {
|
||||
baseServer.close();
|
||||
after(async () => {
|
||||
await baseServer.close();
|
||||
});
|
||||
|
||||
describe('session handling', () => {
|
||||
|
||||
@@ -6,6 +6,7 @@ import request from 'request-promise';
|
||||
import chai from 'chai';
|
||||
import chaiAsPromised from 'chai-as-promised';
|
||||
import sinon from 'sinon';
|
||||
import B from 'bluebird';
|
||||
|
||||
|
||||
chai.should();
|
||||
@@ -43,11 +44,16 @@ describe('server', () => {
|
||||
app.get('/error', () => {
|
||||
throw new Error('hahaha');
|
||||
});
|
||||
app.get('/pause', async (req, res) => {
|
||||
res.header['content-type'] = 'text/html';
|
||||
await B.delay(1000);
|
||||
res.status(200).send('We have waited!');
|
||||
});
|
||||
}
|
||||
hwServer = await server(configureRoutes, 8181);
|
||||
});
|
||||
after(async () => {
|
||||
hwServer.close();
|
||||
await hwServer.close();
|
||||
errorStub.restore();
|
||||
});
|
||||
|
||||
@@ -76,4 +82,17 @@ describe('server', () => {
|
||||
await server(() => {}, 8181, 'lolcathost').should.be.rejectedWith(/ENOTFOUND/);
|
||||
await server(() => {}, 8181, '1.1.1.1').should.be.rejectedWith(/EADDRNOTAVAIL/);
|
||||
});
|
||||
it('should wait for the server close connections before finishing closing', async () => {
|
||||
let bodyPromise = request('http://localhost:8181/pause');
|
||||
|
||||
// relinquish control so that we don't close before the request is received
|
||||
await B.delay(100);
|
||||
|
||||
let before = Date.now();
|
||||
await hwServer.close();
|
||||
// expect slightly less than the request waited, since we paused above
|
||||
(Date.now() - before).should.be.above(900);
|
||||
|
||||
(await bodyPromise).should.equal('We have waited!');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user