[Pkg-javascript-commits] [pdf.js] 149/174: Use random port in test webserver
David Prévot
taffit at moszumanska.debian.org
Thu Nov 19 18:45:38 UTC 2015
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository pdf.js.
commit ff8c3342076546b772c0936b215e726f590716bc
Author: Rob Wu <rob at robwu.nl>
Date: Tue Nov 10 19:08:52 2015 +0100
Use random port in test webserver
---
test/test.js | 2 +-
test/webserver.js | 16 +++++++++++++++-
2 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/test/test.js b/test/test.js
index aae5551..895aa6b 100644
--- a/test/test.js
+++ b/test/test.js
@@ -60,7 +60,7 @@ function parseOptions() {
.example('$0 --b=firefox -t=issue5567 -t=issue5909',
'Run the reftest identified by issue5567 and issue5909 in Firefox.')
.describe('port', 'The port the HTTP server should listen on.')
- .default('port', 8000)
+ .default('port', 0)
.describe('unitTest', 'Run the unit tests.')
.describe('fontTest', 'Run the font tests.')
.describe('noDownload', 'Skips test PDFs downloading.')
diff --git a/test/webserver.js b/test/webserver.js
index 9fac55c..9ea02d2 100644
--- a/test/webserver.js
+++ b/test/webserver.js
@@ -44,7 +44,7 @@ var defaultMimeType = 'application/octet-stream';
function WebServer() {
this.root = '.';
this.host = 'localhost';
- this.port = 8000;
+ this.port = 0;
this.server = null;
this.verbose = false;
this.cacheExpirationTime = 0;
@@ -56,6 +56,7 @@ function WebServer() {
}
WebServer.prototype = {
start: function (callback) {
+ this._ensureNonZeroPort();
this.server = http.createServer(this._handler.bind(this));
this.server.listen(this.port, this.host, callback);
console.log(
@@ -65,6 +66,19 @@ WebServer.prototype = {
this.server.close(callback);
this.server = null;
},
+ _ensureNonZeroPort: function () {
+ if (!this.port) {
+ // If port is 0, a random port will be chosen instead. Do not set a host
+ // name to make sure that the port is synchronously set by .listen().
+ var server = http.createServer().listen(0);
+ var address = server.address();
+ // .address().port being available synchronously is merely an
+ // implementation detail. So we are defensive here and fall back to some
+ // fixed port when the address is not available yet.
+ this.port = address ? address.port : 8000;
+ server.close();
+ }
+ },
_handler: function (req, res) {
var url = req.url;
var urlParts = /([^?]*)((?:\?(.*))?)/.exec(url);
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/pdf.js.git
More information about the Pkg-javascript-commits
mailing list