This repository has been archived by the owner on Oct 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.coffee
531 lines (476 loc) · 18.3 KB
/
runner.coffee
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
jsjob = require '..'
chai = require 'chai' if not chai
fs = require 'fs'
path = require 'path'
Promise = require 'bluebird'
wait = (delay, func) ->
setTimeout func, delay
local = (name) ->
return "http://localhost:8001/spec/fixtures/jsjobs/#{name}.js"
runJobs = (runner, inputs, options, callback) ->
runKey = (key, idx, length, cb) ->
data = inputs[key]
runner.runJob data.url, data.payload, options, (err, res, details) ->
r =
error: err
result: res
details: details
key: key
return cb null, r # don't bail out on errors, just capture it all
return null
keys = Object.keys inputs
mapOptions =
concurrency: Infinity
Promise.map(keys, Promise.promisify(runKey), mapOptions)
.then (array) ->
obj = {}
for item in array
k = item.key
delete item.key
obj[k] = item
return obj
.nodeify callback
return
describe 'Runner', ->
solver = null
solveroptions =
timeout: null # set in beforeEach
hardtimeout: null # set in beforeEach
verbose: false
detailsLog: true
before (done) ->
solver = new jsjob.Runner solveroptions
solver.start (err) ->
chai.expect(err).to.be.a 'undefined'
done()
after (done) ->
solver.stop (err) ->
chai.expect(err).to.be.a 'undefined'
done()
beforeEach () ->
solveroptions.hardtimeout = 8000
solveroptions.timeout = 3500
describe 'passthrough jsjob', ->
@timeout 4000
it 'should succeed and return input', (done) ->
filter = local 'return-input'
input = { 'foo': 'barbaz', 'hello': 'world' }
options = {}
solver.runJob filter, input, options, (err, solution, details) ->
chai.expect(err).to.not.exist
chai.expect(solution).to.eql input
done()
describe 'filter URL which gives 404', ->
@timeout 4000 # TODO: fail faster?
it 'should fail and return error', (done) ->
filter = local '--non-existing--'
page = ""
options = {}
solver.runJob filter, page, options, (err, solution, details) ->
chai.expect(err).to.be.a 'error'
chai.expect(err.message).to.contain 'window.jsJobRun'
done()
describe 'filter without window.jsJobRun', ->
@timeout 4000 # TODO: fail faster?
it 'should fail', (done) ->
filter = local 'no-entrypoint'
page = ""
options = {}
solver.runJob filter, page, options, (err, solution, details) ->
chai.expect(err).to.be.a 'error'
chai.expect(err.message).to.contain 'window.jsJobRun'
done()
describe 'filter returning error', ->
it 'should fail and return the error', (done) ->
filter = local 'return-error'
page = ""
options = {}
solver.runJob filter, page, options, (err, solution, details) ->
chai.expect(err).to.be.a 'error'
chai.expect(err.message).to.contain 'this is my error'
done()
describe 'filter returning thrown error', ->
it 'should fail and return the error with stacktrace', (done) ->
filter = local 'return-thrown-error'
page = ""
options = {}
solver.runJob filter, page, options, (err, solution, details) ->
chai.expect(err).to.be.a 'error'
chai.expect(err.message).to.contain 'this error was thrown'
chai.expect(err.stack).to.contain 'mythrowingfunction'
chai.expect(err.stack).to.contain 'return-thrown-error.js:3'
done()
describe 'filter returning no solution and no error', ->
it 'should fail and return error', (done) ->
filter = local 'return-nothing'
page = ""
options = {}
solver.runJob filter, page, options, (err, solution, details) ->
chai.expect(solution).to.not.exist
chai.expect(err).to.be.a 'error'
chai.expect(err.message).to.contain 'solution nor error'
done()
describe 'filter returning data multiple times', ->
it 'should succeed and ignore everything but first data', (done) ->
filter = local 'return-multiple'
page = ""
options = {}
solver.runJob filter, page, options, (err, solution, details) ->
chai.expect(err).to.not.exist
chai.expect(solution).to.equal 'my first data 1'
done()
describe 'filter throwing exception in jsJobRun', ->
it 'should fail and return error', (done) ->
@timeout 4000 # TODO: fail faster?
filter = local 'throw-directly'
page = ""
options = {}
solver.runJob filter, page, options, (err, solution, details) ->
chai.expect(solution).to.not.exist
chai.expect(err).to.be.an 'error'
chai.expect(err.message).to.contain 'thrown in polySolvePage'
done()
describe 'filter throwing uncaught exception', ->
it 'should fail and return error', (done) ->
@timeout 4000 # TODO: fail faster?
filter = local 'throw-indirectly'
page = ""
options = {}
solver.runJob filter, page, options, (err, solution, details) ->
chai.expect(solution).to.not.exist
chai.expect(err).to.be.an 'error'
chai.expect(err.message).to.contain 'thrown in a setTimeout'
chai.expect(err.message).to.contain 'poly: main start'
chai.expect(err.stack, 'stack should not duplicate message').to.not.contain 'poly: main start'
done()
describe 'filter returning data then throwing exception', ->
# XXX: should maybe pass, but is an edge case, and hard to differentiate
it 'should fail and return error with stacktrace', (done) ->
filter = local 'callback-then-throw'
page = ""
options = {}
solver.runJob filter, page, options, (err, solution, details) ->
chai.expect(solution).to.not.exist
chai.expect(err).to.be.an 'error'
chai.expect(err.message).to.contain 'function'
chai.expect(err.message).to.contain 'line'
done()
describe 'filter throwing exception then returning data', ->
it 'should fail and return error with stacktrace', (done) ->
filter = local 'throw-then-callback'
page = ""
options = {}
solver.runJob filter, page, options, (err, solution, details) ->
chai.expect(solution).to.not.exist
chai.expect(err).to.be.an 'error'
chai.expect(err.message).to.contain 'function'
chai.expect(err.message).to.contain 'line'
done()
describe 'filter returning error with custom properties', ->
it 'should fail and return the error including properties', (done) ->
filter = local 'return-custom-error'
page = ""
options = {}
solver.runJob filter, page, options, (err, solution, details) ->
chai.expect(err).to.be.a 'error'
chai.expect(err.message).to.contain 'custom error'
chai.expect(err).to.include.keys ['myproperty', 'otherproperty']
chai.expect(err.myproperty).to.equal 'myvalue'
chai.expect(err.otherproperty).to.equal 'othervalue'
done()
describe 'passing options to filter', ->
it 'should able to roundtrip back through details', (done) ->
filter = local 'pass-options-in-details'
page =
config:
color: 'red'
layout: 'directed'
items: []
options =
foo: 'foofoo'
bar: 'barbaz'
solver.runJob filter, page, options, (err, solution, details) ->
chai.expect(err).to.be.a 'null'
chai.expect(solution).to.be.a 'string'
chai.expect(details.options).to.eql options
done()
describe 'sending "images" array as part of details', ->
it 'should be returned so it can be stored', (done) ->
filter = local 'details-images'
page = ""
options = {}
solver.runJob filter, page, options, (err, solution, details) ->
chai.expect(err).to.not.exist
chai.expect(solution).to.be.a 'string'
chai.expect(details).to.include.keys 'images'
chai.expect(details.images).to.have.length 2
chai.expect(details.images[0]).to.contain 'imgflo.herokuapp.com/graph/'
chai.expect(details.images[1]).to.contain 'imgflo.herokuapp.com/graph/'
done()
describe 'accessing a URL that fails to load', ->
details = null
it 'should return error from jsJobRun()', (done) ->
filter = local 'script-load-error'
page = ""
options = {}
solver.runJob filter, page, options, (err, solution, d) ->
details = d
chai.expect(err).to.exist
chai.expect(err.message).to.include 'script load error'
done()
it 'details.stdout should contain error', () ->
chai.expect(details.stdout).to.include 'resource error'
chai.expect(details.stdout).to.include '203'
chai.expect(details.stdout).to.include 'thegrid.io/non-existant.js'
describe 'specifying a resource whitelist in options', ->
details = null
it 'should return blocked error', (done) ->
filter = local 'script-load-blocked'
page = ""
options =
allowedResources: [
'http://ajax.googleapis.com'
]
solver.runJob filter, page, options, (err, solution, d) ->
details = d
chai.expect(err).to.exist
chai.expect(err.message).to.include 'Failed to load script'
done()
it 'details.stdout should contain blocked log', () ->
chai.expect(details.stdout).to.include 'blocked resource'
chai.expect(details.stdout).to.include 'cdn.thegrid.io/design-systems/helloworld'
it 'details.stdout should not contain resource error for blocked request', () ->
chai.expect(details.stdout).to.not.include 'resource error'
describe 'jsjob requesting a screenshot', ->
details = null
pngMagic = new Buffer "PNG\r\n", 'utf8'
it.skip 'should be stored in details as base64 PNG dataurl', (done) -> # feature disabled
@timeout 4000
filter = local 'take-screenshots'
page = {}
options = {}
solver.runJob filter, page, options, (err, solution, d) ->
details = d
chai.expect(err).to.not.exist
chai.expect(details.screenshots).to.have.keys ['myname']
dataurl = details.screenshots.myname
chai.expect(dataurl).to.contain 'data:image/png'
chai.expect(dataurl).to.contain 'base64,'
data = dataurl.slice(dataurl.indexOf(',')+1)
decoded = new Buffer(data, 'base64')
chai.expect(decoded.toString('ascii')).to.contain pngMagic.toString('ascii')
done()
it 'should be empty in details', (done) ->
@timeout 4000
filter = local 'take-screenshots'
page = {}
options = {}
solver.runJob filter, page, options, (err, solution, d) ->
details = d
chai.expect(err).to.not.exist
chai.expect(details.screenshots).to.eql {}
done()
describe 'many jsjobs concurrencly', ->
it 'should return correct data for each', (done) ->
@timeout 9000
doJob = Promise.promisify (id, index, total, callback) ->
url = local 'return-input'
input =
common: 'data-ABC'
unique: "request-#{id}"
options = {}
solver.runJob url, input, options, (err, out, details) ->
return callback err, out
requests = [11, 22, 33, 44, 55]
Promise.map(requests, doJob, {concurrency: 10}).nodeify (err, results) ->
return done err if err
commons = results.map (r) -> r.common
expectCommons = results.map (r) -> 'data-ABC'
chai.expect(commons).to.eql expectCommons
uniques = results.map (r) -> r.unique
expectUniques = requests.map (i) -> "request-#{i}"
chai.expect(uniques).to.eql expectUniques
done()
return
describe 'using scripts as API adapter', ->
details = null
hidden = 'not visible to the inner function'
it 'should return results', (done) ->
url = local 'foreign-polysolvepage'
input = { 'body': 'something' }
options =
scripts: [
"""
window.jsJobRun = function(input, options, callback) {
var hidden = options._hidden;
options._hidden = undefined;
window.polySolvePage(input, options, function(err, out, det) {
det._hidden = hidden;
return callback(err, out, det);
});
};
"""
]
_hidden: hidden
solver.runJob url, input, options, (err, output, d) ->
details = d
chai.expect(err).to.not.exist
chai.expect(output).to.include "<html>something</html>"
done()
it 'should pass through information', ->
chai.expect(details._hidden).to.eql hidden
describe 'filter never returning data', ->
it 'should fail after timeout', (done) ->
@timeout 4000
filter = local 'never-returning'
page = ""
options = {}
solver.runJob filter, page, options, (err, solution, details) ->
chai.expect(err).to.be.a 'error'
chai.expect(err.message).to.contain 'TIMEOUT'
done()
describe 'input data containing <script>...</script>', ->
it 'should succeed', (done) ->
filter = local 'return-input'
input = { 'foo': 'barbaz', 'htmlscript': '<script>alert("Works!")</script>' }
options = {}
solver.runJob filter, input, options, (err, solution, details) ->
chai.expect(err).to.not.exist
chai.expect(solution).to.eql input
done()
describe 'input data containing </script>...<script>', ->
it 'should succeed', (done) ->
filter = local 'return-input'
input = { 'foo': 'barbaz', 'htmlbogusscript': '</script> <script>alert("Works!")</script> <script>' }
options = {}
solver.runJob filter, input, options, (err, solution, details) ->
chai.expect(err).to.not.exist
chai.expect(solution).to.eql input
done()
describe 'input data containing HTML comment', ->
it 'should succeed', (done) ->
filter = local 'return-input'
input = { 'foo': 'barbaz', 'htmlcomment': '<!-- FFOO -->' }
options = {}
solver.runJob filter, input, options, (err, solution, details) ->
chai.expect(err).to.not.exist
chai.expect(solution).to.eql input
done()
describe 'filter with infinite loop', ->
it 'should timeout and return error', (done) ->
@timeout 9000
filter = local 'infinite-loop'
page = ""
options = {}
solver.runJob filter, page, options, (err, solution, details) ->
chai.expect(solution).to.not.exist
chai.expect(err).to.be.a 'error'
chai.expect(err.message).to.include 'hard timeout'
chai.expect(err.message).to.include 'logged before infinite loop'
chai.expect(err.message).to.include 'poly: starting'
chai.expect(err.stack, 'stack should not duplicate message').to.not.include 'poly: starting'
done()
describe 'several jobs concurrently', ->
it 'each job should return correct result', (done) ->
@timeout 9000
options = {}
jobs =
returnerror:
url: local 'return-error'
payload: null
expect:
error: 'this is my error'
timeout:
url: local 'infinite-loop'
payload: null
expect:
error: 'hard timeout'
successA:
url: local 'return-input'
payload: { 'success': 'A' }
expect:
result: { 'success': 'A' }
throwerror:
url: local 'return-thrown-error'
payload: null
expect:
error: 'this error was thrown'
successB:
url: local 'return-input'
payload: { 'success': 'B' }
expect:
result: { 'success': 'B' }
runJobs solver, jobs, options, (err, results) ->
chai.expect(err).to.not.exist
jobNames = Object.keys jobs
resultNames = Object.keys results
chai.expect(resultNames).to.eql jobNames
for jobname, result of results
expect = jobs[jobname].expect
chai.expect(expect, "missing expect for job #{jobname}").to.exist
if expect.error
chai.expect(result.error, "job '#{jobname}' should have error").to.exist
chai.expect(result.error.message, "job '#{jobname}' error").to.contain expect.error
if expect.result
chai.expect(result.result, "job '#{jobname}' results").to.eql expect.result
done()
describe 'terminating child process while running', ->
testRunning = false
beforeEach () ->
testRunning = true
afterEach () ->
testRunning = false
it 'should error with helpful message', (done) ->
filter = local 'return-delayed'
page =
delay: 900
foo: 'somedata'
options = {}
chai.expect(Object.keys(solver.jobs)).to.have.length 0
solver.runJob filter, page, options, (err, outdata, details) ->
chai.expect(outdata).to.not.exist
chai.expect(err).to.be.a 'error'
chai.expect(err.message).to.not.include 'returned falsy'
chai.expect(err.message).to.not.include 'and no Error'
chai.expect(err.message).to.include 'child process terminated'
done()
# XXX: relies on internal/private details
wait 100, () ->
jobNames = Object.keys solver.jobs
return if not testRunning
chai.expect(jobNames).to.have.length 1
p = solver.jobs[jobNames[0]].process
chai.expect(p.child).to.exist
p.child.kill 'SIGTERM'
describe 'Runner.stop() with in-flight jobs', ->
solver = null
solveroptions =
timeout: null # set in beforeEach
hardtimeout: null # set in beforeEach
verbose: false
detailsLog: true
stopErr = new Error 'init'
before (done) ->
solver = new jsjob.Runner solveroptions
solver.start (err) ->
chai.expect(err).to.be.a 'undefined'
done()
after (done) ->
done()
it 'should return cancellation Error for job', (done) ->
filter = local 'return-delayed'
page =
delay: 1300
foo: 'somedata3'
options = {}
solver.runJob filter, page, options, (err, outdata, details) ->
chai.expect(outdata).to.not.exist
chai.expect(err).to.be.a 'error'
chai.expect(err.message).to.include 'JsJob cancelled due to Runner.stop()'
chai.expect(err.type).to.equal "cancelled" # so apps can detect this kind of error and ignore it, as likely they intended to do stop()
done()
wait 100, () ->
solver.stop (err) ->
stopErr = err
it 'should not have errored on stop()', ->
chai.expect(stopErr).to.not.exist