Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50660 views
1
/*
2
* multiple-workers-test.js: Tests for spawning multiple workers with forever
3
*
4
* (C) 2010 Nodejitsu Inc.
5
* MIT LICENCE
6
*
7
*/
8
9
var assert = require('assert'),
10
net = require('net'),
11
path = require('path'),
12
request = require('request'),
13
vows = require('vows'),
14
forever = require('../../lib/forever');
15
16
var children = [],
17
pids;
18
19
//
20
// Helper function test requests against children.
21
//
22
function assertRunning(port, i) {
23
return {
24
topic: function () {
25
request('http://127.0.0.1:' + port, this.callback);
26
},
27
"should respond with `i know nodejitsu`": function (err, res, body) {
28
assert.isNull(err);
29
assert.equal(res.statusCode, 200);
30
assert.equal(body, 'hello, i know nodejitsu.');
31
},
32
"stop the child process": function () {
33
children[i].stop();
34
}
35
}
36
}
37
38
vows.describe('forever/workers/multiple').addBatch({
39
"When using forever": {
40
"and spawning two processes using the same script": {
41
topic: function () {
42
var that = this,
43
script = path.join(__dirname, '..', 'fixtures', 'server.js');
44
45
children[0] = new (forever.Monitor)(script, {
46
silent: true,
47
maxRestart: 1,
48
options: [ "--port=8080"]
49
});
50
51
children[1] = new (forever.Monitor)(script, {
52
silent: true,
53
maxRestart: 1,
54
options: [ "--port=8081"]
55
});
56
57
children[0].on('start', function () {
58
children[1].on('start', function () {
59
pids = children.map(function (child) {
60
return child.child.pid;
61
});
62
63
setTimeout(function () {
64
forever.startServer(children[0], children[1], that.callback);
65
}, 1000);
66
});
67
68
children[1].start();
69
});
70
71
children[0].start();
72
},
73
"should respond with no error": function (err, workers) {
74
assert.lengthOf(workers, 2);
75
assert.equal(workers[0].monitor, children[0]);
76
assert.equal(workers[1].monitor, children[1]);
77
workers.forEach(function (worker) {
78
assert.instanceOf(worker, forever.Worker);
79
});
80
},
81
"requests against the first child": assertRunning(8080, 0),
82
"requests against the second child": assertRunning(8081, 1)
83
//
84
// TODO: We should cleanup these processes.
85
//
86
}
87
},
88
}).addBatch({
89
"Once the stop attempt has been made": {
90
topic: function () {
91
setTimeout(this.callback, 200);
92
},
93
"the processes should be dead": function () {
94
assert.isFalse(forever.checkProcess(pids[0]));
95
assert.isFalse(forever.checkProcess(pids[1]));
96
}
97
}
98
}).export(module);
99
100