Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ephemeral port for testing #71

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/camp.js
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,9 @@ function start (settings) {
settings.ca = settings.ca || [];
}

settings.port = settings.port || (settings.secure ? 443 : 80);
if (settings.port == null) {
settings.port = settings.secure ? 443 : 80;
}

return startServer(settings);
};
Expand Down
13 changes: 2 additions & 11 deletions test/test-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var post = function (path, postData, contentType, callback) {
};

var launchTests = function () {
portNumber = server.address().port;
t.seq([
function t0 (next) {
get('', function (res) {
Expand Down Expand Up @@ -244,21 +245,11 @@ var launchTests = function () {
});
};

// FIXME: is there a good way to make a server get a port for testing?
var server;
var portNumber = 8000;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Please keep a var portNumber; or var portNumber = 0; here instead of relying on the portNumber = ... above to create an accidental global.

var startServer = function () {
server = camp.start({port:portNumber, documentRoot:'./test/web'});
server = camp.start({port: 0, documentRoot: './test/web'});
server.on('listening', launchTests);
};
var serverStartDomain = require('domain').create();
serverStartDomain.on('error', function (err) {
if (err.code === 'EADDRINUSE') {
portNumber++;
serverStartDomain.run(startServer);
} else {
throw err;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you certain that serverStartDomain errors are automatically thrown when you don't have a .on('error', listener)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it isn't. Can we just remove the domain wrapper here?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't the domain's only purpose to handle EADDRINUSE? It seems like we can remove the domain altogether.

}
});
serverStartDomain.run(startServer);