Skip to content

Instantly share code, notes, and snippets.

@avnersorek
Created May 20, 2014 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avnersorek/ae36e1de73bca2a8e188 to your computer and use it in GitHub Desktop.
Save avnersorek/ae36e1de73bca2a8e188 to your computer and use it in GitHub Desktop.
This is an example of an express node.js server which can be launched using naught, with an integration test using supertest. Each worker naught runs will test itself on a unique port, and only if passes it will listen in on the main port and join the pool.
var express = require('express');
var supertest = require('supertest');
var mainport = 80;
var app = express();
app.get('/test', function(req, res) { res.send({ 'pid' : process.pid }); } );
function listenToMainport() {
app.listen(mainport, function(){
console.log("Listening to port %d ...", mainport);
if (process.send) process.send('online');
});
}
function integrationTest() {
console.log('Testing pid %d ...', process.pid);
var testport = 10000 + process.pid;
var server = app.listen(testport, function() {
console.log('pid %d listening on port %d ...', process.pid, testport);
supertest(server)
.get('/test')
.expect(200)
.end(function(err, res){
if (err) {
console.log(err);
if (process.send) process.send('offline');
process.exit(-1);
}
else {
console.log('pid %d tested OK, closing test server and launching main one...', process.pid);
server.close();
listenToMainport();
}
});
});
}
process.on('message', function(message) {
if (message === 'shutdown') {
process.exit(0);
}
});
integrationTest();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment