-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmaster-worker-test.js
131 lines (117 loc) · 3.61 KB
/
master-worker-test.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const hub = require('..');
const commands = require('../lib/globals').commands;
const cluster = require('cluster');
const assert = require('assert');
const path = require('path');
const fork = require('./fork');
if (cluster.isWorker) throw Error('file should not run under worker');
describe('Communicate from master to workers', () => {
afterEach(cluster.disconnect);
afterEach(() => hub.reset());
it('Listens for event from a worker and responds', (done) => {
fork('worker-master-1.js');
hub.on('yesmaster', (data) => {
assert.deepEqual(data, { hello: 'there' });
hub.emit('work', 'now');
hub.emit('work', 'now');
hub.on('donemaster', done);
});
});
it('Listens for set and get event', (done) => {
fork('worker-master-2.js');
hub.on('set foo', (value) => {
assert.equal(value, 66);
hub.set('bar', 'dog');
});
hub.on('result', (value) => {
assert.equal(value, 'DOG');
done();
});
hub.set('value', 24);
});
it('Does not emit worker to worker events', (done) => {
fork('worker-master-3.js');
hub.on('local', () => { throw Error('should not emit'); });
hub.on('remote', (value) => {
assert.equal(value, 'yes!');
done();
});
});
it('Listens and unlistens to events in order', (done) => {
fork('worker-master-4.js');
hub.removeAllListeners('hi');
const f = () => {};
hub.on('hi', f);
hub.on('hi', () => {
hub.off('hi', f);
hub.off('hi', f);
done();
});
hub.off('hi', () => {});
hub.emit('call');
});
it('Listeners are called in order', (done) => {
fork('worker-master-order.js');
const results = [];
hub.on('the-event', () => results.push(1));
hub.on('the-event', () => results.push(2));
hub.prependListener('the-event', () => results.push(3));
let doneCall = false;
hub.on('after', () => {
assert.equal(doneCall, true);
});
hub.prependManyListener(2, 'after', () => {
doneCall = true;
assert.deepEqual(results, [3, 1, 2]);
done();
});
});
describe('Send a badly formatted messages to each other', () => {
it('Master and worker ignore it', (done) => {
const worker = fork('worker-bad-msg.js');
hub.on('done', done);
hub.ready(() => {
const dir = path.resolve(__dirname, '../lib');
worker.send({ dir, hub: 'none' });
worker.send({ dir, hub: '', cmd: commands.CB, key: -1 });
worker.send({ dir, hub: '', cmd: commands.FN, key: -1 });
worker.send({});
hub.emit('ok');
});
});
});
describe('Send a message from master first', () => {
it('Worker should receive it after it connects', (done) => {
fork('worker-get-msg.js');
hub.on('done', done);
hub.emit('bad');
});
});
describe('Emit event with a callback', () => {
it('Function gets sent to worker then back to master', (done) => {
fork('worker-emit-fn-1.js');
hub.emit('my-done', done);
hub.on('same-done', (done2) => done2());
});
it('Function gets sent between workers', (done) => {
const WORKERS = 2;
for (let i = 0; i < WORKERS; i++) {
fork('worker-emit-fn-2.js');
}
let n = WORKERS;
hub.on('sum', (sum) => {
assert.equal(sum, 4);
if (--n === 0) done();
});
});
it('Deleted func does nothing', (done) => {
fork('worker-emit-fn-3.js');
hub._maxFuncs = 1;
after(() => hub._maxFuncs = 100);
let n = 2;
hub.emit('bad-good', done, () => {
if (--n == 0) throw Error('should not be called');
});
});
});
});