From dde35cd41456cb673d5df23384ecb3d93a49155d Mon Sep 17 00:00:00 2001 From: Masakazu OHTSUKA Date: Tue, 18 Sep 2012 12:25:28 +0900 Subject: [PATCH] max_requests_per_child test --- Makefile | 1 + test/03_max_requests_per_child.js | 65 +++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 test/03_max_requests_per_child.js diff --git a/Makefile b/Makefile index d35a787..dadd431 100644 --- a/Makefile +++ b/Makefile @@ -3,3 +3,4 @@ test: node ./test/01_restart_app.js node ./test/02_accidental_death.js + node ./test/03_max_requests_per_child.js diff --git a/test/03_max_requests_per_child.js b/test/03_max_requests_per_child.js new file mode 100644 index 0000000..12a12b6 --- /dev/null +++ b/test/03_max_requests_per_child.js @@ -0,0 +1,65 @@ +var fs = require('fs'), +assert = require('assert'), +http = require('http'), +cluster = require('cluster'), +async = require('async'), +angel = require('../angel'), +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, +} ); + +function sendRequest( callback ) { + http.get({ + host: 'localhost', + port: test_port, + path: '/' + }, function(res) { + var body = ''; + res.on('data', function(chunk) { + body += chunk; + }); + res.on('end', function() { + callback( body ); + }); + }); +} + +function runTest () { + var jobs = [] + , requests_count = 10; + while ( requests_count -- ) { + jobs.push( function(callback) { + sendRequest( function(body) { + callback( null, body ); + }); + }); + } + async.series( jobs, function (err, results) { + assert( err === null, 'no errors' ); + + var worker_pids = {}; + results.forEach( function( result ) { + worker_pids[ result.split(/:/).pop() ] = 1; + }); + assert( Object.keys( worker_pids ).length === 5, + '10 requests to 1 worker with 2 max_requests_per_child -> 5' ); + + console.log( 'Result: PASS' ); + + process.exit(0); + }); +} + +if ( cluster.isMaster ) { + // wait til listening + setTimeout( runTest, 100 ); +}