context.js
4.52 KB
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
global.TEST = true
var Bottleneck = require('./bottleneck')
var assert = require('assert')
module.exports = function (options={}) {
var mustEqual = function (a, b) {
var strA = JSON.stringify(a)
var strB = JSON.stringify(b)
if (strA !== strB) {
console.log(strA + ' !== ' + strB, (new Error('').stack))
assert(strA === strB)
}
}
var start
var calls = []
// set options.datastore
var setRedisClientOptions = function (options) {
options.clearDatastore = true
if (options.clientOptions == null) {
options.clientOptions = {
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT,
}
}
}
if (options.datastore == null && process.env.DATASTORE === 'redis') {
options.datastore = 'redis'
setRedisClientOptions(options)
} else if (options.datastore == null && process.env.DATASTORE === 'ioredis') {
options.datastore = 'ioredis'
setRedisClientOptions(options)
} else {
options.datastore = 'local'
}
var limiter = new Bottleneck(options)
// limiter.on("debug", function (str, args) { console.log(`${Date.now()-start} ${str} ${JSON.stringify(args)}`) })
if (!options.errorEventsExpected) {
limiter.on("error", function (err) {
console.log('(CONTEXT) ERROR EVENT', err)
})
}
limiter.ready().then(function (client) {
start = Date.now()
})
var getResults = function () {
return {
elapsed: Date.now() - start,
callsDuration: calls.length > 0 ? calls[calls.length - 1].time : null,
calls: calls
}
}
var context = {
job: function (err, ...result) {
var cb = result.pop()
calls.push({err: err, result: result, time: Date.now()-start})
if (process.env.DEBUG) console.log(result, calls)
cb.apply({}, [err].concat(result))
},
slowJob: function (duration, err, ...result) {
setTimeout(function () {
var cb = result.pop()
calls.push({err: err, result: result, time: Date.now()-start})
if (process.env.DEBUG) console.log(result, calls)
cb.apply({}, [err].concat(result))
}, duration)
},
promise: function (err, ...result) {
return new Promise(function (resolve, reject) {
if (process.env.DEBUG) console.log('In c.promise. Result: ', result)
calls.push({err: err, result: result, time: Date.now()-start})
if (process.env.DEBUG) console.log(result, calls)
if (err === null) {
return resolve(result)
} else {
return reject(err)
}
})
},
slowPromise: function (duration, err, ...result) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
if (process.env.DEBUG) console.log('In c.slowPromise. Result: ', result)
calls.push({err: err, result: result, time: Date.now()-start})
if (process.env.DEBUG) console.log(result, calls)
if (err === null) {
return resolve(result)
} else {
return reject(err)
}
}, duration)
})
},
pNoErrVal: function (promise, ...expected) {
if (process.env.DEBUG) console.log('In c.pNoErrVal. Expected:', expected)
return promise.then(function (actual) {
mustEqual(actual, expected)
})
},
noErrVal: function (...expected) {
return function (err, ...actual) {
mustEqual(err, null)
mustEqual(actual, expected)
}
},
last: function (options) {
var opt = options != null ? options : {}
return limiter.schedule(opt, function () { return Promise.resolve(getResults()) })
.catch(function (err) { console.error("Error in context.last:", err)})
},
wait: function (wait) {
return new Promise(function (resolve, reject) {
setTimeout(resolve, wait)
})
},
limiter: limiter,
mustEqual: mustEqual,
mustExist: function (a) { assert(a != null) },
results: getResults,
checkResultsOrder: function (order) {
mustEqual(order.length, calls.length)
for (var i = 0; i < Math.max(calls.length, order.length); i++) {
mustEqual(order[i], calls[i].result)
}
},
checkDuration: function (shouldBe, minBound = 10) {
var results = getResults()
var min = shouldBe - minBound
var max = shouldBe + 50
if (!(results.callsDuration > min && results.callsDuration < max)) {
console.error('Duration not around ' + shouldBe + '. Was ' + results.callsDuration)
}
assert(results.callsDuration > min && results.callsDuration < max)
}
}
return context
}