-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
process.spec.js
335 lines (265 loc) · 10.5 KB
/
process.spec.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
const path = require('path')
const _ = require('lodash')
const BaseLauncher = require('../../../lib/launchers/base')
const RetryLauncher = require('../../../lib/launchers/retry')
const CaptureTimeoutLauncher = require('../../../lib/launchers/capture_timeout')
const ProcessLauncher = require('../../../lib/launchers/process')
const EventEmitter = require('../../../lib/events').EventEmitter
const createMockTimer = require('../mocks/timer')
describe('launchers/process.js', () => {
let emitter
let mockSpawn
let mockTempDir
let launcher
const BROWSER_PATH = path.normalize('/usr/bin/browser')
beforeEach(() => {
emitter = new EventEmitter()
launcher = new BaseLauncher('fake-id', emitter)
mockSpawn = sinon.spy(function (cmd, args) {
const process = new EventEmitter()
process.stdout = new EventEmitter()
process.stderr = new EventEmitter()
process.kill = sinon.spy()
process.exitCode = null
mockSpawn._processes.push(process)
return process
})
mockSpawn._processes = []
mockTempDir = {
getPath: (suffix) => `/temp${suffix}`,
create: sinon.spy(),
remove: sinon.spy()
}
})
it('should create a temp directory', () => {
ProcessLauncher.call(launcher, mockSpawn, mockTempDir)
launcher._getCommand = () => null
launcher.start('http://host:9988/')
expect(launcher._tempDir).to.equal('/temp/karma-fake-id')
expect(mockTempDir.create).to.have.been.calledWith('/temp/karma-fake-id')
})
it('should remove the temp directory', (done) => {
ProcessLauncher.call(launcher, mockSpawn, mockTempDir)
launcher._getCommand = () => null
launcher.start('http://host:9988/')
launcher.kill()
_.defer(() => {
expect(mockTempDir.remove).to.have.been.called
expect(mockTempDir.remove.args[0][0]).to.equal('/temp/karma-fake-id')
done()
})
})
describe('_normalizeCommand', () => {
it('should remove quotes from the cmd', () => {
ProcessLauncher.call(launcher, null, mockTempDir)
expect(launcher._normalizeCommand('"/bin/brow ser"')).to.equal(path.normalize('/bin/brow ser'))
expect(launcher._normalizeCommand("'/bin/brow ser'")).to.equal
path.normalize('/bin/brow ser')
expect(launcher._normalizeCommand('`/bin/brow ser`')).to.equal(path.normalize('/bin/brow ser'))
})
})
describe('with RetryLauncher', () => {
it('should handle spawn ENOENT error and not even retry', (done) => {
ProcessLauncher.call(launcher, mockSpawn, mockTempDir)
RetryLauncher.call(launcher, 2)
launcher._getCommand = () => BROWSER_PATH
const failureSpy = sinon.spy()
emitter.on('browser_process_failure', failureSpy)
launcher.start('http://host:9876/')
mockSpawn._processes[0].emit('error', {code: 'ENOENT'})
mockSpawn._processes[0].emit('exit', 1)
mockTempDir.remove.callArg(1)
_.defer(() => {
expect(launcher.state).to.equal(launcher.STATE_FINISHED)
expect(failureSpy).to.have.been.called
done()
})
})
it('should handle spawn EACCES error and not even retry', (done) => {
ProcessLauncher.call(launcher, mockSpawn, mockTempDir)
RetryLauncher.call(launcher, 2)
launcher._getCommand = () => BROWSER_PATH
const failureSpy = sinon.spy()
emitter.on('browser_process_failure', failureSpy)
launcher.start('http://host:9876/')
mockSpawn._processes[0].emit('error', {code: 'EACCES'})
mockSpawn._processes[0].emit('exit', 1)
mockTempDir.remove.callArg(1)
_.defer(() => {
expect(launcher.state).to.equal(launcher.STATE_FINISHED)
expect(failureSpy).to.have.been.called
done()
})
})
})
// higher level tests with Retry and CaptureTimeout launchers
describe('flow', () => {
let failureSpy
let mockTimer = failureSpy = null
beforeEach(() => {
mockTimer = createMockTimer()
CaptureTimeoutLauncher.call(launcher, mockTimer, 100)
ProcessLauncher.call(launcher, mockSpawn, mockTempDir, mockTimer)
RetryLauncher.call(launcher, 2)
launcher._getCommand = () => BROWSER_PATH
failureSpy = sinon.spy()
emitter.on('browser_process_failure', failureSpy)
})
// the most common scenario, when everything works fine
it('start -> capture -> kill', (done) => {
// start the browser
launcher.start('http://localhost/')
expect(mockSpawn).to.have.been.calledWith(BROWSER_PATH, ['http://localhost/?id=fake-id'])
// mark captured
launcher.markCaptured()
// kill it
const killingLauncher = launcher.kill()
expect(launcher.state).to.equal(launcher.STATE_BEING_KILLED)
expect(mockSpawn._processes[0].kill).to.have.been.called
// process exits
mockSpawn._processes[0].emit('exit', 0)
mockTempDir.remove.callArg(1)
killingLauncher.done(() => {
expect(launcher.state).to.equal(launcher.STATE_FINISHED)
done()
})
})
// when the browser fails to get captured in default timeout, it should restart
it('start -> timeout -> restart', (done) => {
const stdOutSpy = sinon.spy(launcher, '_onStdout')
const stdErrSpy = sinon.spy(launcher, '_onStderr')
// start
launcher.start('http://localhost/')
// expect starting the process
expect(mockSpawn).to.have.been.calledWith(BROWSER_PATH, ['http://localhost/?id=fake-id'])
const browserProcess = mockSpawn._processes.shift()
const expectedStdoutString = 'starting...'
const expectedStderrString = 'Oops...there was a problem'
browserProcess.stdout.emit('data', expectedStdoutString)
browserProcess.stderr.emit('data', expectedStderrString)
// timeout
mockTimer.wind(101)
// We must've caught some output
expect(stdOutSpy).to.have.been.called
expect(stdErrSpy).to.have.been.called
stdOutSpy.calledWith(expectedStdoutString)
// expect killing browser
expect(browserProcess.kill).to.have.been.called
browserProcess.emit('exit', 0)
mockTempDir.remove.callArg(1)
mockSpawn.resetHistory()
_.defer(() => {
// expect re-starting
expect(mockSpawn).to.have.been.calledWith(BROWSER_PATH, ['http://localhost/?id=fake-id'])
expect(failureSpy).not.to.have.been.called
done()
})
})
it('start -> timeout -> 3xrestart -> failure', (done) => {
// start
launcher.start('http://localhost/')
// expect starting
expect(mockSpawn).to.have.been.calledWith(BROWSER_PATH, ['http://localhost/?id=fake-id'])
let browserProcess = mockSpawn._processes.shift()
mockSpawn.resetHistory()
// timeout - first time
mockTimer.wind(101)
// expect killing browser
expect(browserProcess.kill).to.have.been.called
browserProcess.emit('exit', 0)
mockTempDir.remove.callArg(1)
mockTempDir.remove.resetHistory()
_.defer(() => {
// expect re-starting
expect(mockSpawn).to.have.been.calledWith(BROWSER_PATH, ['http://localhost/?id=fake-id'])
browserProcess = mockSpawn._processes.shift()
expect(failureSpy).not.to.have.been.called
mockSpawn.resetHistory()
// timeout - second time
mockTimer.wind(101)
// expect killing browser
expect(browserProcess.kill).to.have.been.called
browserProcess.emit('exit', 0)
mockTempDir.remove.callArg(1)
mockTempDir.remove.resetHistory()
_.defer(() => {
// expect re-starting
expect(mockSpawn).to.have.been.calledWith(BROWSER_PATH, ['http://localhost/?id=fake-id'])
browserProcess = mockSpawn._processes.shift()
expect(failureSpy).not.to.have.been.called
mockSpawn.resetHistory()
// timeout - third time
mockTimer.wind(201)
// expect killing browser
expect(browserProcess.kill).to.have.been.called
browserProcess.emit('exit', 0)
mockTempDir.remove.callArg(1)
mockTempDir.remove.resetHistory()
_.defer(() => {
expect(mockSpawn).to.not.have.been.called
expect(failureSpy).to.have.been.called
done()
})
})
})
})
// when the browser fails to start, it should restart
it('start -> crash -> restart', (done) => {
// start
launcher.start('http://localhost/')
// expect starting the process
expect(mockSpawn).to.have.been.calledWith(BROWSER_PATH, ['http://localhost/?id=fake-id'])
let browserProcess = mockSpawn._processes.shift()
mockSpawn.resetHistory()
// crash
browserProcess.emit('exit', 1)
mockTempDir.remove.callArg(1)
mockTempDir.remove.resetHistory()
_.defer(() => {
// expect re-starting
expect(mockSpawn).to.have.been.calledWith(BROWSER_PATH, ['http://localhost/?id=fake-id'])
browserProcess = mockSpawn._processes.shift()
expect(failureSpy).not.to.have.been.called
done()
})
})
})
// higher level tests - process kill timeout
describe('process-kill-timeout', () => {
let failureSpy
let mockTimer = null
beforeEach(() => {
mockTimer = createMockTimer()
CaptureTimeoutLauncher.call(launcher, mockTimer, 100)
ProcessLauncher.call(launcher, mockSpawn, mockTempDir, mockTimer, 300)
RetryLauncher.call(launcher, 2)
launcher._getCommand = () => BROWSER_PATH
failureSpy = sinon.spy()
emitter.on('browser_process_failure', failureSpy)
})
// when the browser fails to get captured in default timeout, it should restart
it('start -> capture_timeout -> kill -> process_kill_timeout -> sigkill', () => {
// start
launcher.start('http://localhost/')
// expect starting the process
expect(mockSpawn).to.have.been.calledWith(BROWSER_PATH, ['http://localhost/?id=fake-id'])
const browserProcess = mockSpawn._processes.shift()
// timeout
mockTimer.wind(101)
// expect killing browser
expect(browserProcess.kill).to.have.been.called
// processKillTimeout not reached yet
mockTimer.wind(299)
// SIGKILL not called yet
expect(browserProcess.kill.withArgs('SIGKILL')).not.to.have.been.called
// processKillTimeout
mockTimer.wind(301)
// expect killing with SIGKILL
expect(browserProcess.kill.withArgs('SIGKILL')).to.have.been.called
browserProcess.emit('exit', 0)
mockTempDir.remove.callArg(1)
mockTempDir.remove.resetHistory()
mockSpawn.resetHistory()
})
})
})