From 3c42c0a6af2a975c71c5798b5eabdcaf21c6a47f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Fri, 5 Feb 2016 13:29:43 +0100 Subject: [PATCH] test: listen on ephemeral ports --- test/helper.js | 4 ++-- test/remote-connector.test.js | 22 +++++++++++++++------- test/remote-models.test.js | 10 +++++++--- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/test/helper.js b/test/helper.js index f43df80..30a47cc 100644 --- a/test/helper.js +++ b/test/helper.js @@ -7,11 +7,11 @@ exports.createRemoteDataSource = createRemoteDataSource; exports.createRestAppAndListen = createRestAppAndListen; exports.getUserProperties = getUserProperties; -function createRestAppAndListen(port) { +function createRestAppAndListen() { var app = loopback(); app.set('host', '127.0.0.1'); - if (port) app.set('port', port); + app.set('port', 0); app.use(loopback.rest()); app.locals.handler = app.listen(); diff --git a/test/remote-connector.test.js b/test/remote-connector.test.js index 60453ec..695cbab 100644 --- a/test/remote-connector.test.js +++ b/test/remote-connector.test.js @@ -4,19 +4,24 @@ var helper = require('./helper'); describe('RemoteConnector', function() { var ctx = this; - before(function() { - ctx.serverApp = helper.createRestAppAndListen(3001); + before(function setupServer(done) { + ctx.serverApp = helper.createRestAppAndListen(); ctx.ServerModel = helper.createModel({ parent: 'TestModel', app: ctx.serverApp, datasource: helper.createMemoryDataSource() }); - ctx.remoteApp = helper.createRestAppAndListen(3002); + ctx.serverApp.locals.handler.on('listening', function() { done(); }); + }); + + before(function setupRemoteClient(done) { + ctx.remoteApp = helper.createRestAppAndListen(); ctx.RemoteModel = helper.createModel({ parent: 'TestModel', app: ctx.remoteApp, datasource: helper.createRemoteDataSource(ctx.serverApp) }); + ctx.remoteApp.locals.handler.on('listening', function() { done(); }); }); after(function() { @@ -66,8 +71,8 @@ describe('RemoteConnector', function() { describe('Custom Path', function() { var ctx = this; - before(function(done) { - ctx.serverApp = helper.createRestAppAndListen(3001); + before(function setupServer(done) { + ctx.serverApp = helper.createRestAppAndListen(); ctx.ServerModel = helper.createModel({ parent: 'TestModel', app: ctx.serverApp, @@ -76,8 +81,11 @@ describe('Custom Path', function() { http: {path: '/custom'} } }); + ctx.serverApp.locals.handler.on('listening', function() { done(); }); + }); - ctx.remoteApp = helper.createRestAppAndListen(3002); + before(function setupRemoteClient(done) { + ctx.remoteApp = helper.createRestAppAndListen(); ctx.RemoteModel = helper.createModel({ parent: 'TestModel', app: ctx.remoteApp, @@ -87,7 +95,7 @@ describe('Custom Path', function() { http: {path: '/custom'} } }); - done(); + ctx.remoteApp.locals.handler.on('listening', function() { done(); }); }); after(function(done) diff --git a/test/remote-models.test.js b/test/remote-models.test.js index 78ad69e..ffcf0e4 100644 --- a/test/remote-models.test.js +++ b/test/remote-models.test.js @@ -5,22 +5,26 @@ var TaskEmitter = require('strong-task-emitter'); describe('Remote model tests', function() { var ctx = this; - beforeEach(function() { - ctx.serverApp = helper.createRestAppAndListen(3001); + beforeEach(function(done) { + ctx.serverApp = helper.createRestAppAndListen(); ctx.ServerModel = helper.createModel({ parent: 'TestModel', app: ctx.serverApp, datasource: helper.createMemoryDataSource(), properties: helper.userProperties }); + ctx.serverApp.locals.handler.on('listening', function() { done(); }); + }); - ctx.remoteApp = helper.createRestAppAndListen(3002); + beforeEach(function setupRemoteClient(done) { + ctx.remoteApp = helper.createRestAppAndListen(); ctx.RemoteModel = helper.createModel({ parent: 'TestModel', app: ctx.remoteApp, datasource: helper.createRemoteDataSource(ctx.serverApp), properties: helper.userProperties }); + ctx.remoteApp.locals.handler.on('listening', function() { done(); }); }); afterEach(function() {