Skip to content

Commit

Permalink
(fix) test randomlly failing because port conflic
Browse files Browse the repository at this point in the history
(add) allow port to be 0 (zero), the port will be randomlly assigned by node
  • Loading branch information
jstty committed Jul 22, 2019
1 parent e6cf06f commit adfcaf8
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 54 deletions.
14 changes: 12 additions & 2 deletions legacy/hyper.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ Hyper.prototype._normalizeOptions = function (options) {
options.httpFramework.env = options.env;
delete options.env;
}
if (options.port) {
if (_.isString(options.port)) {
options.port = parseInt(options.port);
}
if (_.isInteger(options.port) && options.port >= 0) {
options.httpFramework.port = options.port;
delete options.port;
}
Expand Down Expand Up @@ -254,7 +257,10 @@ Hyper.prototype.httpServerListen = function () {
return when.promise(function (resolve, reject) {
// ------------------------------------------------

this._httpServer.listen(this._httpFramework.port(), function () {
var listener = this._httpServer.listen(this._httpFramework.port(), function () {
// set http framework internal port to match the port that is really running
// for example, if port set to 0 (zero) the http server will randomlly assign a free port
this._httpFramework.port(listener.address().port);
this._logger.log('Listening on port %d', this._httpFramework.port());
resolve();
}.bind(this));
Expand All @@ -269,6 +275,10 @@ Hyper.prototype._start = function () {
return this._serviceManager.loadServices().then(function () {
return this.httpServerListen();
}.bind(this)).then(function () {
// only update if host '127.0.0.1' and port == 0
// this is to fix and issue with port set to zero, the http server will auto set the port to a random free port
this._serviceManager.getServiceRouter().updateInternalZeroPort(this._httpFramework.port());

return this._serviceManager.postStartInit();
}.bind(this)).then(function () {
this._logger.log('---------------------------------------------');
Expand Down
13 changes: 13 additions & 0 deletions legacy/router.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,17 @@ ServiceRouter.prototype.find = function (name) {
} else {
return service.adapter;
}
};

// only update if host '127.0.0.1' and port == 0
// this is to fix and issue with port set to zero, the http server will auto set the port to a random free port
ServiceRouter.prototype.updateInternalZeroPort = function (port) {
_.forEach(this._services, function (service) {
if (service.options.hostname == '127.0.0.1' && service.options.port == 0) {
service.options.port = port;
}
if (service.adapter._options.hostname == '127.0.0.1' && service.adapter._options.port == 0) {
service.adapter._options.port = port;
}
});
};
14 changes: 12 additions & 2 deletions lib/hyper.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ Hyper.prototype._normalizeOptions = function (options) {
options.httpFramework.env = options.env;
delete options.env;
}
if (options.port) {
if (_.isString(options.port)) {
options.port = parseInt(options.port)
}
if (_.isInteger(options.port) && options.port >= 0) {
options.httpFramework.port = options.port;
delete options.port;
}
Expand Down Expand Up @@ -231,7 +234,10 @@ Hyper.prototype.httpServerListen = function () {
return when.promise(function (resolve, reject) {
// ------------------------------------------------

this._httpServer.listen(this._httpFramework.port(), function () {
let listener = this._httpServer.listen(this._httpFramework.port(), function () {
// set http framework internal port to match the port that is really running
// for example, if port set to 0 (zero) the http server will randomlly assign a free port
this._httpFramework.port(listener.address().port);
this._logger.log('Listening on port %d', this._httpFramework.port());
resolve();
}.bind(this));
Expand All @@ -248,6 +254,10 @@ Hyper.prototype._start = function () {
return this.httpServerListen();
}.bind(this))
.then(function () {
// only update if host '127.0.0.1' and port == 0
// this is to fix and issue with port set to zero, the http server will auto set the port to a random free port
this._serviceManager.getServiceRouter().updateInternalZeroPort(this._httpFramework.port());

return this._serviceManager.postStartInit();
}.bind(this))
.then(function () {
Expand Down
12 changes: 12 additions & 0 deletions lib/router.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,15 @@ ServiceRouter.prototype.find = function (name) {
}
};

// only update if host '127.0.0.1' and port == 0
// this is to fix and issue with port set to zero, the http server will auto set the port to a random free port
ServiceRouter.prototype.updateInternalZeroPort = function (port) {
_.forEach(this._services, function (service) {
if(service.options.hostname == '127.0.0.1' && service.options.port == 0) {
service.options.port = port;
}
if(service.adapter._options.hostname == '127.0.0.1' && service.adapter._options.port == 0) {
service.adapter._options.port = port;
}
})
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
"eslint": "~6.0.1",
"eslint-config-standard": "~13.0.1",
"eslint-plugin-standard": "~4.0.0",
"freeport": "~1.0.5",
"is-es6": "~1.0.0",
"istanbul": "~0.4.5",
"mocha": "^6.2.0",
Expand All @@ -82,6 +81,7 @@
"lint": "npm run clean && ./node_modules/.bin/eslint lib examples test",
"lint:fix": "npm run clean && ./node_modules/.bin/eslint lib examples test --fix",
"compile": "./node_modules/.bin/babel lib --out-dir legacy",
"build": "npm run compile",
"clean": "find examples -name 'node_modules' -print0 | xargs -0 rm -rf",
"prepublish": "npm run-script compile",
"pretest": "npm run-script compile",
Expand Down
95 changes: 46 additions & 49 deletions test/examples/tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var _ = require('lodash');
var path = require('path');
var freeport = require('freeport');
var isES6 = require('is-es6');
var shell = require('shelljs');

Expand Down Expand Up @@ -39,56 +38,54 @@ _.forEach(list, function (testList, item) {

// initialize server for test
before(function (done) {
freeport(function (err, port) {
if (err) {
console.error('freeport Error:', err);
}

var d = path.join(rootDir,
'..' + path.sep + '..' + path.sep + 'examples' + path.sep +
item + path.sep + name);

// console.log("example dir:", d, "\n");
process.chdir(d);
shell.exec('npm install', { silent: true });
// console.log("cwd:", process.cwd(), "\n");

server = {};
server.config = {
appName: appName,
env: 'dev',
silent: true,
port: port
};

process.env.HYPER_OPTIONS = JSON.stringify(server.config);

var app = null;
// try to load app.js file
try {
var appFile = path.resolve('.' + path.sep + appName + '.js');
// console.log("appFile:", appFile, "\n");
app = require(appFile);
}
catch (err) {
expect(err).not.to.be.null;
console.error(err);
}
var d = path.join(rootDir,
'..' + path.sep + '..' + path.sep + 'examples' + path.sep +
item + path.sep + name);

// console.log("example dir:", d, "\n");
process.chdir(d);
shell.exec('npm install', { silent: true });
// console.log("cwd:", process.cwd(), "\n");

server = {};
server.config = {
appName: appName,
env: 'dev',
silent: true,
port: 0 // random free port
};

process.env.HYPER_OPTIONS = JSON.stringify(server.config);
// console.log(process.env.HYPER_OPTIONS);

var app = null;
// try to load app.js file
try {
var appFile = path.resolve('.' + path.sep + appName + '.js');
// console.log("appFile:", appFile, "\n");
app = require(appFile);
}
catch (err) {
expect(err).not.to.be.null;
console.error(err);
}

// console.log("app:", !!app, "\n");
expect(app).not.to.be.null;
if (app) {
expect(app.then).not.to.be.null;
app.then(function (_app) {
server.app = _app;
server.httpFrameworkApp = _app.httpFramework().app();
done();
});
}
else {
// console.log("app:", !!app, "\n");
expect(app).not.to.be.null;
if (app) {
expect(app.then).not.to.be.null;
app.then(function (_app) {
server.app = _app;
server.httpFrameworkApp = _app.httpFramework().app();
done();
}
});
})
// .catch(function (err) {
// console.log("App Error:", err);
// });
}
else {
done();
}
});

after(function (done) {
Expand Down

0 comments on commit adfcaf8

Please sign in to comment.