This repository has been archived by the owner on Feb 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_max_requests_per_child.js
77 lines (69 loc) · 1.99 KB
/
03_max_requests_per_child.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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";
angel( app, {
port : 0,
workers : 1,
max_requests_per_child : 2,
} );
function sendRequest( port, callback ) {
http.get({
host: 'localhost',
port: port,
path: '/'
}, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
callback( body );
});
});
}
function runTest (port) {
var jobs = []
, requests_count = 10;
while ( requests_count -- ) {
jobs.push( function(callback) {
sendRequest( port, 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 ( process.version.match(/v0\.6/) ) {
// cheat, 0.6 doesn't have cluster.workers, so forget them
console.log( "Result: PASS" );
process.exit(0);
}
// worker tells master where the worker is listening on
// master uses that port to request
if ( cluster.isMaster ) {
Object.keys(cluster.workers).forEach( function(id) {
cluster.workers[id].on("message", function(m) {
switch (m.cmd) {
case "listening":
runTest( m.address.port );
break;
}
});
});
}