-
-
Notifications
You must be signed in to change notification settings - Fork 805
/
Copy pathtest.node.worker.worker.js
224 lines (179 loc) · 4.43 KB
/
test.node.worker.worker.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
'use strict';
// MODULES //
var tape = require( 'tape' );
var EventEmitter = require( 'events' ).EventEmitter;
var proxyquire = require( 'proxyquire' );
var worker = require( './../lib/node/worker/worker.js' );
// FIXTURES //
function Proc() {
EventEmitter.call( this );
this.env = {
'WORKER_CMD': 'node'
};
return this;
}
Proc.prototype = Object.create( EventEmitter.prototype );
Proc.prototype.constructor = Proc;
Proc.prototype.send = function send() {};
// TESTS //
tape( 'main export is a function', function test( t ) {
t.ok( true, __filename );
t.equal( typeof worker, 'function', 'main export is a function' );
t.end();
});
tape( 'if the worker receives a `close` message, the worker closes child processes', function test( t ) {
var worker;
var proc;
proc = new Proc();
worker = proxyquire( './../lib/node/worker/worker.js', {
'./close.js': closeProcess,
'./process.js': proc
});
worker();
proc.emit( 'message', 'close' );
function closeProcess() {
t.ok( true, 'calls fcn to close child processes' );
t.end();
}
});
tape( 'if the `WORKER_ORDERED` environment variable is set, the worker should "exec" a script', function test( t ) {
var worker;
var proc;
proc = new Proc();
proc.env.WORKER_ORDERED = '1';
worker = proxyquire( './../lib/node/worker/worker.js', {
'./process.js': proc,
'./exec.js': exec,
'./children.js': {}
});
worker();
proc.emit( 'message', '/a/b/c.js' );
t.end();
function exec() {
t.ok( true, 'calls an exec script' );
return {
'pid': 1234
};
}
});
tape( 'if the `WORKER_ORDERED` environment variable is not set, the worker should "spawn" a script', function test( t ) {
var worker;
var proc;
proc = new Proc();
delete proc.env.WORKER_ORDERED;
worker = proxyquire( './../lib/node/worker/worker.js', {
'./process.js': proc,
'./spawn.js': spawn,
'./children.js': {}
});
worker();
proc.emit( 'message', '/a/b/c.js' );
t.end();
function spawn() {
t.ok( true, 'calls a spawn script' );
return {
'pid': 12345
};
}
});
tape( 'the process emits an error and closes itself if an error is encountered when spawning a script', function test( t ) {
var worker;
var proc;
var flg;
proc = new Proc();
delete proc.env.WORKER_ORDERED;
worker = proxyquire( './../lib/node/worker/worker.js', {
'./close.js': closeProcess,
'./process.js': proc,
'./spawn.js': spawn,
'./children.js': {}
});
worker();
proc.on( 'error', onError );
proc.emit( 'message', '/a/b/c.js' );
function spawn( cmd, msg, clbk ) {
process.nextTick( onTick );
return {
'pid': 12345
};
function onTick() {
clbk( new Error( 'beep' ) );
}
}
function closeProcess() {
t.ok( true, 'calls fcn to close a process' );
flg = true;
}
function onError( error ) {
t.ok( error, 'emits an error' );
if ( !flg ) {
t.ok( false, 'did not call fcn to close a process' );
}
t.end();
}
});
tape( 'the process emits an error and closes itself if an error is encountered when executing a script', function test( t ) {
var worker;
var proc;
var flg;
proc = new Proc();
proc.env.WORKER_ORDERED = '1';
worker = proxyquire( './../lib/node/worker/worker.js', {
'./close.js': closeProcess,
'./process.js': proc,
'./exec.js': exec,
'./children.js': {}
});
worker();
proc.on( 'error', onError );
proc.emit( 'message', '/a/b/c.js' );
function exec( cmd, msg, clbk ) {
process.nextTick( onTick );
return {
'pid': 12345
};
function onTick() {
clbk( new Error( 'beep' ) );
}
}
function closeProcess() {
t.ok( true, 'calls fcn to close a process' );
flg = true;
}
function onError( error ) {
t.ok( error, 'emits an error' );
if ( !flg ) {
t.ok( false, 'did not call fcn to close a process' );
}
t.end();
}
});
tape( 'the process sends a message to the parent process containing the script filepath as an identifier, informing the parent process that the script has finished executing', function test( t ) {
var worker;
var proc;
var msg;
proc = new Proc();
delete proc.env.WORKER_ORDERED;
proc.send = send;
worker = proxyquire( './../lib/node/worker/worker.js', {
'./process.js': proc,
'./spawn.js': spawn,
'./children.js': {}
});
worker();
msg = '/a/b/c.js';
proc.emit( 'message', msg );
function spawn( cmd, msg, clbk ) {
process.nextTick( onTick );
return {
'pid': 12345
};
function onTick() {
clbk( null, 12345, msg );
}
}
function send( val ) {
t.equal( val, msg, 'sends file path to parent process' );
t.end();
}
});