Browse files

listen on port 0 when testing.

worker tells master where worker is listening to, to achieve this
  • Loading branch information...
1 parent 414bab1 commit dbc372c4e4b75c460b32ebfa8f7a0f42ac1ce9f4 @mash committed Oct 25, 2013
Showing with 59 additions and 35 deletions.
  1. +3 −0 angel.js
  2. +19 −12 test/01_restart_app.js
  3. +19 −12 test/02_accidental_death.js
  4. +18 −11 test/03_max_requests_per_child.js
View
3 angel.js
@@ -203,6 +203,9 @@ function startServer (server, options) {
server.isListening = true;
log( "listening on "+ ( (typeof options.port !== "undefined") ? server.address().port
: options.path ) );
+
+ // tell master where we're listening (used in tests)
+ process.send({ cmd: "listening", address: server.address() });
});
server.listen.apply( server, listenArgs );
View
31 test/01_restart_app.js
@@ -8,18 +8,16 @@ app = require('./_app.js');
// this will be our test app's response body
var expected_body = process.env.NODE_ANGEL_TEST_MESSAGE = "Hello";
-var test_port = 3000;
-
angel( app, {
- port: test_port, // mmm, test requires port 3000 to be open
- workers: 1,
- refresh_modules_regexp: "/_app\\.js$"
+ port : 0, // listen whereever available
+ workers : 1,
+ refresh_modules_regexp : "/_app\\.js$"
} );
-function sendRequest( callback ) {
+function sendRequest( port, callback ) {
http.get({
host: 'localhost',
- port: test_port,
+ port: port,
path: '/'
}, function(res) {
var body = '';
@@ -32,8 +30,8 @@ function sendRequest( callback ) {
});
}
-function runTest () {
- sendRequest( function(body) {
+function runTest (port) {
+ sendRequest( port, function(body) {
var body_and_pid = body.split(':')
, worker_pid = body_and_pid[ 1 ]
;
@@ -49,7 +47,7 @@ function runTest () {
// wait til worker restarts
setTimeout( function() {
- sendRequest( function(body2) {
+ sendRequest( port, function(body2) {
var body_and_pid2 = body2.split(':');
// console.log( "body_and_pid2[ 0 ]: ",body_and_pid2);
@@ -65,7 +63,16 @@ function runTest () {
});
}
+// worker tells master where the worker is listening on
+// master uses that port to request
if ( cluster.isMaster ) {
- // wait til listening
- setTimeout( runTest, 100 );
+ Object.keys(cluster.workers).forEach( function(id) {
+ cluster.workers[id].on("message", function(m) {
+ switch (m.cmd) {
+ case "listening":
+ runTest( m.address.port );
+ break;
+ }
+ });
+ });
}
View
31 test/02_accidental_death.js
@@ -8,18 +8,16 @@ app = require('./_suicide_app.js');
// this will be our test app's response body
var expected_body = process.env.NODE_ANGEL_TEST_MESSAGE = "Hello";
-var test_port = 3000;
-
angel( app, {
- port: test_port, // mmm, test requires port 3000 to be open
- workers: 1,
- refresh_modules_regexp: "/_suicide_app\\.js$"
+ port : 0,
+ workers : 1,
+ refresh_modules_regexp : "/_suicide_app\\.js$"
} );
-function sendRequest( callback ) {
+function sendRequest( port, callback ) {
var req = http.request({
host: 'localhost',
- port: test_port,
+ port: port,
path: '/'
}, function(res) {
var body = '';
@@ -37,9 +35,9 @@ function sendRequest( callback ) {
req.end();
}
-function runTest () {
+function runTest (port) {
// console.log( "master requests app" );
- sendRequest( function(res, body) {
+ sendRequest( port, function(res, body) {
assert( res.statusCode === 200 );
var body_and_pid = body.split(':')
@@ -56,7 +54,7 @@ function runTest () {
// wait til worker restarts after suicide
setTimeout( function() {
// console.log( "master requests app again" );
- sendRequest( function(res2, body2) {
+ sendRequest( port, function(res2, body2) {
assert( res2.statusCode === 200 );
var body_and_pid2 = body2.split(':');
@@ -74,7 +72,16 @@ function runTest () {
});
}
+// worker tells master where the worker is listening on
+// master uses that port to request
if ( cluster.isMaster ) {
- // wait til listening
- setTimeout( runTest, 100 );
+ Object.keys(cluster.workers).forEach( function(id) {
+ cluster.workers[id].on("message", function(m) {
+ switch (m.cmd) {
+ case "listening":
+ runTest( m.address.port );
+ break;
+ }
+ });
+ });
}
View
29 test/03_max_requests_per_child.js
@@ -9,18 +9,16 @@ app = require('./_app.js');
// this will be our test app's response body
var expected_body = process.env.NODE_ANGEL_TEST_MESSAGE = "Hello";
-var test_port = 3000;
-
angel( app, {
- port: test_port, // mmm, test requires port 3000 to be open
- workers: 1,
- max_requests_per_child: 2,
+ port : 0,
+ workers : 1,
+ max_requests_per_child : 2,
} );
-function sendRequest( callback ) {
+function sendRequest( port, callback ) {
http.get({
host: 'localhost',
- port: test_port,
+ port: port,
path: '/'
}, function(res) {
var body = '';
@@ -33,12 +31,12 @@ function sendRequest( callback ) {
});
}
-function runTest () {
+function runTest (port) {
var jobs = []
, requests_count = 10;
while ( requests_count -- ) {
jobs.push( function(callback) {
- sendRequest( function(body) {
+ sendRequest( port, function(body) {
callback( null, body );
});
});
@@ -59,7 +57,16 @@ function runTest () {
});
}
+// worker tells master where the worker is listening on
+// master uses that port to request
if ( cluster.isMaster ) {
- // wait til listening
- setTimeout( runTest, 100 );
+ Object.keys(cluster.workers).forEach( function(id) {
+ cluster.workers[id].on("message", function(m) {
+ switch (m.cmd) {
+ case "listening":
+ runTest( m.address.port );
+ break;
+ }
+ });
+ });
}

0 comments on commit dbc372c

Please sign in to comment.