aneka 0.17.5 → 0.17.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.jsdoc.conf.json +0 -0
- package/CHANGES.md +87 -79
- package/LICENSE +21 -21
- package/README.md +63 -63
- package/docs/arrange-array.js.html +0 -0
- package/docs/create-class.js.html +0 -0
- package/docs/current-loc.js.html +0 -0
- package/docs/data/search.json +0 -0
- package/docs/defaults-deep.js.html +0 -0
- package/docs/delay.js.html +0 -0
- package/docs/extract-text.js.html +0 -0
- package/docs/fill-object.js.html +0 -0
- package/docs/filter-duplicate.js.html +0 -0
- package/docs/filter-index.js.html +0 -0
- package/docs/fonts/Inconsolata-Regular.ttf +0 -0
- package/docs/fonts/OpenSans-Regular.ttf +0 -0
- package/docs/fonts/WorkSans-Bold.ttf +0 -0
- package/docs/format-text.js.html +0 -0
- package/docs/generate-id.js.html +0 -0
- package/docs/get-caller-filename.js.html +0 -0
- package/docs/get-global-module-dir.js.html +0 -0
- package/docs/get-index.js.html +0 -0
- package/docs/get-key-by-value.js.html +0 -0
- package/docs/global.html +0 -0
- package/docs/includes.js.html +0 -0
- package/docs/index.html +0 -0
- package/docs/is-class.js.html +0 -0
- package/docs/is-set.js.html +0 -0
- package/docs/merge-objects-by-key.js.html +0 -0
- package/docs/paginate.js.html +0 -0
- package/docs/parse-args-argv.js.html +0 -0
- package/docs/parse-duration.js.html +0 -0
- package/docs/parse-env.js.html +0 -0
- package/docs/parse-kv-string.js.html +0 -0
- package/docs/parse-object.js.html +0 -0
- package/docs/pascal-case.js.html +0 -0
- package/docs/random-range.js.html +0 -0
- package/docs/resolve-path.js.html +0 -0
- package/docs/round.js.html +0 -0
- package/docs/scripts/core.js +0 -0
- package/docs/scripts/core.min.js +0 -0
- package/docs/scripts/resize.js +0 -0
- package/docs/scripts/search.js +0 -0
- package/docs/scripts/search.min.js +5 -5
- package/docs/scripts/third-party/Apache-License-2.0.txt +0 -0
- package/docs/scripts/third-party/fuse.js +0 -0
- package/docs/scripts/third-party/hljs-line-num-original.js +0 -0
- package/docs/scripts/third-party/hljs-line-num.js +0 -0
- package/docs/scripts/third-party/hljs-original.js +0 -0
- package/docs/scripts/third-party/hljs.js +0 -0
- package/docs/scripts/third-party/popper.js +0 -0
- package/docs/scripts/third-party/tippy.js +0 -0
- package/docs/scripts/third-party/tocbot.js +0 -0
- package/docs/scripts/third-party/tocbot.min.js +0 -0
- package/docs/sec-to-hms.js.html +0 -0
- package/docs/set-immediate.js.html +0 -0
- package/docs/set-interval.js.html +0 -0
- package/docs/set-timeout.js.html +0 -0
- package/docs/shim.js.html +0 -0
- package/docs/static/bitcoin.jpeg +0 -0
- package/docs/styles/clean-jsdoc-theme-base.css +0 -0
- package/docs/styles/clean-jsdoc-theme-dark.css +0 -0
- package/docs/styles/clean-jsdoc-theme-light.css +0 -0
- package/docs/styles/clean-jsdoc-theme-scrollbar.css +0 -0
- package/docs/styles/clean-jsdoc-theme-without-scrollbar.min.css +0 -0
- package/docs/styles/clean-jsdoc-theme.min.css +0 -0
- package/docs/text-to-array.js.html +0 -0
- package/docs/titleize.js.html +0 -0
- package/docs/without.js.html +0 -0
- package/index.js +119 -119
- package/package.json +48 -48
- package/src/arrange-array.js +25 -25
- package/src/create-class.js +35 -35
- package/src/current-loc.js +0 -0
- package/src/defaults-deep.js +20 -20
- package/src/delay.js +19 -19
- package/src/extract-text.js +23 -23
- package/src/fill-object.js +17 -17
- package/src/filter-duplicate.js +26 -26
- package/src/filter-index.js +17 -17
- package/src/format-text.js +88 -88
- package/src/generate-id.js +35 -35
- package/src/get-caller-filename.js +25 -25
- package/src/get-global-module-dir.js +24 -20
- package/src/get-index.js +15 -15
- package/src/get-key-by-value.js +12 -12
- package/src/includes.js +18 -18
- package/src/is-class.js +13 -13
- package/src/is-set.js +11 -11
- package/src/log.js +19 -19
- package/src/merge-objects-by-key.js +24 -24
- package/src/paginate.js +45 -45
- package/src/parse-args-argv.js +0 -0
- package/src/parse-duration.js +14 -14
- package/src/parse-env.js +0 -0
- package/src/parse-kv-string.js +21 -21
- package/src/parse-object.js +76 -76
- package/src/pascal-case.js +14 -14
- package/src/random-range.js +15 -15
- package/src/resolve-path.js +0 -0
- package/src/round.js +13 -13
- package/src/sec-to-hms.js +27 -27
- package/src/set-immediate.js +12 -12
- package/src/set-interval.js +56 -55
- package/src/set-timeout.js +34 -34
- package/src/shim.js +28 -28
- package/src/text-to-array.js +17 -17
- package/src/titleize.js +37 -37
- package/src/without.js +18 -18
- package/test/arrange-array.js +41 -41
- package/test/create-class.js +33 -33
- package/test/current-loc.js +24 -24
- package/test/defaults-deep.js +19 -19
- package/test/delay.js +17 -17
- package/test/extract-text.js +17 -17
- package/test/fill-object.js +17 -17
- package/test/filter-duplicate.js +19 -19
- package/test/filter-index.js +17 -17
- package/test/format-text.js +26 -26
- package/test/generate-id.js +28 -28
- package/test/get-caller-filename.js +22 -22
- package/test/get-global-module-dir.js +51 -51
- package/test/get-index.js +17 -17
- package/test/get-key-by-value.js +17 -17
- package/test/includes.js +18 -18
- package/test/is-class.js +19 -19
- package/test/is-set.js +21 -21
- package/test/log.js +47 -47
- package/test/merge-objects-by-key.js +19 -19
- package/test/paginate.js +24 -24
- package/test/parse-args-argv.js +63 -63
- package/test/parse-duration.js +18 -18
- package/test/parse-env.js +35 -35
- package/test/parse-kv-string.js +17 -17
- package/test/parse-object.js +30 -30
- package/test/pascal-case.js +18 -18
- package/test/random-range.js +21 -21
- package/test/resolve-path.js +22 -22
- package/test/round.js +19 -19
- package/test/sec-to-hms.js +18 -18
- package/test/set-immediate.js +17 -17
- package/test/set-interval.js +196 -180
- package/test/set-timeout.js +120 -120
- package/test/shim.js +34 -34
- package/test/text-to-array.js +18 -18
- package/test/titleize.js +27 -27
- package/test/without.js +17 -17
package/test/set-interval.js
CHANGED
|
@@ -1,180 +1,196 @@
|
|
|
1
|
-
/* eslint-env mocha */
|
|
2
|
-
import { expect } from 'chai'
|
|
3
|
-
import lockfile from '@bybrave/proper-lockfile2'
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
timer.
|
|
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
|
-
expect(
|
|
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
|
-
|
|
1
|
+
/* eslint-env mocha */
|
|
2
|
+
import { expect } from 'chai'
|
|
3
|
+
import lockfile from '@bybrave/proper-lockfile2'
|
|
4
|
+
import fs from 'fs-extra'
|
|
5
|
+
import fn from '../src/set-interval.js'
|
|
6
|
+
|
|
7
|
+
const originalSetTimeout = global.setTimeout
|
|
8
|
+
const originalClearTimeout = global.clearTimeout
|
|
9
|
+
const originalLock = lockfile.lock
|
|
10
|
+
const originalEnsureFileSync = fs.ensureFileSync
|
|
11
|
+
|
|
12
|
+
async function flushMicrotasks (turns = 6) {
|
|
13
|
+
for (let index = 0; index < turns; index++) {
|
|
14
|
+
await Promise.resolve()
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function installTimerQueue () {
|
|
19
|
+
const queue = []
|
|
20
|
+
|
|
21
|
+
global.setTimeout = (callback, ms, ...args) => {
|
|
22
|
+
queue.push({ callback, ms, args })
|
|
23
|
+
return queue.length
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
global.clearTimeout = () => {}
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
queue,
|
|
30
|
+
async drainOne () {
|
|
31
|
+
const timer = queue.shift()
|
|
32
|
+
if (!timer) return false
|
|
33
|
+
timer.callback(...timer.args)
|
|
34
|
+
await flushMicrotasks()
|
|
35
|
+
return true
|
|
36
|
+
},
|
|
37
|
+
async drainAll () {
|
|
38
|
+
while (await this.drainOne());
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function deferred () {
|
|
44
|
+
let resolve
|
|
45
|
+
const promise = new Promise(function (done) {
|
|
46
|
+
resolve = done
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
return { promise, resolve }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
describe('setInterval', function () {
|
|
53
|
+
afterEach(function () {
|
|
54
|
+
global.setTimeout = originalSetTimeout
|
|
55
|
+
global.clearTimeout = originalClearTimeout
|
|
56
|
+
lockfile.lock = originalLock
|
|
57
|
+
fs.ensureFileSync = originalEnsureFileSync
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('returns a promise and schedules the first execution immediately', async function () {
|
|
61
|
+
const timers = installTimerQueue()
|
|
62
|
+
const result = fn(() => {}, 10)
|
|
63
|
+
|
|
64
|
+
expect(result).to.be.instanceOf(Promise)
|
|
65
|
+
await flushMicrotasks()
|
|
66
|
+
|
|
67
|
+
expect(timers.queue).to.have.length(1)
|
|
68
|
+
expect(timers.queue[0].ms).to.equal(10)
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it('stops without running the handler when maxIterations is zero', async function () {
|
|
72
|
+
const timers = installTimerQueue()
|
|
73
|
+
const calls = []
|
|
74
|
+
|
|
75
|
+
fn(() => calls.push('handler'), '1s', { maxIterations: 0 })
|
|
76
|
+
await flushMicrotasks()
|
|
77
|
+
|
|
78
|
+
expect(calls).to.deep.equal([])
|
|
79
|
+
expect(timers.queue).to.have.length(0)
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
it('runs the handler the requested number of times', async function () {
|
|
83
|
+
const timers = installTimerQueue()
|
|
84
|
+
const calls = []
|
|
85
|
+
|
|
86
|
+
fn(() => calls.push('tick'), 25, { maxIterations: 3 })
|
|
87
|
+
await flushMicrotasks()
|
|
88
|
+
|
|
89
|
+
expect(timers.queue).to.have.length(1)
|
|
90
|
+
expect(timers.queue[0].ms).to.equal(25)
|
|
91
|
+
|
|
92
|
+
await timers.drainAll()
|
|
93
|
+
|
|
94
|
+
expect(calls).to.deep.equal(['tick', 'tick', 'tick'])
|
|
95
|
+
expect(timers.queue).to.have.length(0)
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
it('does not schedule the next tick until a long-running handler finishes', async function () {
|
|
99
|
+
const timers = installTimerQueue()
|
|
100
|
+
const calls = []
|
|
101
|
+
const gate = deferred()
|
|
102
|
+
|
|
103
|
+
fn(async function () {
|
|
104
|
+
calls.push('start')
|
|
105
|
+
await gate.promise
|
|
106
|
+
calls.push('end')
|
|
107
|
+
}, 25, { maxIterations: 2 })
|
|
108
|
+
|
|
109
|
+
await flushMicrotasks()
|
|
110
|
+
|
|
111
|
+
expect(calls).to.deep.equal(['start'])
|
|
112
|
+
expect(timers.queue).to.have.length(0)
|
|
113
|
+
|
|
114
|
+
gate.resolve()
|
|
115
|
+
await flushMicrotasks()
|
|
116
|
+
|
|
117
|
+
expect(calls).to.deep.equal(['start', 'end'])
|
|
118
|
+
expect(timers.queue).to.have.length(1)
|
|
119
|
+
expect(timers.queue[0].ms).to.equal(25)
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
it('passes args and scope on each iteration', async function () {
|
|
123
|
+
const timers = installTimerQueue()
|
|
124
|
+
const scope = { total: 0 }
|
|
125
|
+
|
|
126
|
+
fn(function (amount) {
|
|
127
|
+
this.total += amount
|
|
128
|
+
}, 5, { args: [2], maxIterations: 2, scope })
|
|
129
|
+
await flushMicrotasks()
|
|
130
|
+
|
|
131
|
+
await timers.drainAll()
|
|
132
|
+
|
|
133
|
+
expect(scope.total).to.equal(4)
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
it('locks and releases around every iteration when a lock file is configured', async function () {
|
|
137
|
+
const timers = installTimerQueue()
|
|
138
|
+
const calls = []
|
|
139
|
+
const ensured = []
|
|
140
|
+
fs.ensureFileSync = lockFile => {
|
|
141
|
+
ensured.push(lockFile)
|
|
142
|
+
}
|
|
143
|
+
lockfile.lock = async (lockFile, lockFileOpts) => {
|
|
144
|
+
calls.push(['lock', lockFile, lockFileOpts])
|
|
145
|
+
return async () => {
|
|
146
|
+
calls.push(['release'])
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
fn(() => calls.push(['handler']), 5, {
|
|
151
|
+
lockFile: '/tmp/aneka-set-interval.lock',
|
|
152
|
+
lockFileOpts: { stale: 2000 },
|
|
153
|
+
maxIterations: 2
|
|
154
|
+
})
|
|
155
|
+
await flushMicrotasks()
|
|
156
|
+
|
|
157
|
+
await timers.drainAll()
|
|
158
|
+
|
|
159
|
+
expect(ensured).to.deep.equal([
|
|
160
|
+
'/tmp/aneka-set-interval.lock',
|
|
161
|
+
'/tmp/aneka-set-interval.lock'
|
|
162
|
+
])
|
|
163
|
+
expect(calls).to.deep.equal([
|
|
164
|
+
['lock', '/tmp/aneka-set-interval.lock', { stale: 2000 }],
|
|
165
|
+
['handler'],
|
|
166
|
+
['release'],
|
|
167
|
+
['lock', '/tmp/aneka-set-interval.lock', { stale: 2000 }],
|
|
168
|
+
['handler'],
|
|
169
|
+
['release']
|
|
170
|
+
])
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
it('suppresses lock errors when silent is left at the default', async function () {
|
|
174
|
+
const timers = installTimerQueue()
|
|
175
|
+
const calls = []
|
|
176
|
+
const ensured = []
|
|
177
|
+
fs.ensureFileSync = lockFile => {
|
|
178
|
+
ensured.push(lockFile)
|
|
179
|
+
}
|
|
180
|
+
lockfile.lock = async () => {
|
|
181
|
+
throw new Error('lock failed')
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
fn(() => calls.push('handler'), 5, {
|
|
185
|
+
lockFile: '/tmp/aneka-set-interval.lock',
|
|
186
|
+
maxIterations: 1
|
|
187
|
+
})
|
|
188
|
+
await flushMicrotasks()
|
|
189
|
+
|
|
190
|
+
await timers.drainAll()
|
|
191
|
+
|
|
192
|
+
expect(ensured).to.deep.equal(['/tmp/aneka-set-interval.lock'])
|
|
193
|
+
expect(calls).to.deep.equal([])
|
|
194
|
+
expect(timers.queue).to.have.length(0)
|
|
195
|
+
})
|
|
196
|
+
})
|
package/test/set-timeout.js
CHANGED
|
@@ -1,120 +1,120 @@
|
|
|
1
|
-
/* eslint-env mocha */
|
|
2
|
-
import { expect } from 'chai'
|
|
3
|
-
import lockfile from '@bybrave/proper-lockfile2'
|
|
4
|
-
import fn from '../src/set-timeout.js'
|
|
5
|
-
|
|
6
|
-
const originalSetTimeout = global.setTimeout
|
|
7
|
-
const originalClearTimeout = global.clearTimeout
|
|
8
|
-
const originalLock = lockfile.lock
|
|
9
|
-
|
|
10
|
-
function installTimerMock () {
|
|
11
|
-
const calls = []
|
|
12
|
-
|
|
13
|
-
global.setTimeout = (callback, ms, ...args) => {
|
|
14
|
-
calls.push({ callback, ms, args })
|
|
15
|
-
queueMicrotask(() => callback(...args))
|
|
16
|
-
return calls.length
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
global.clearTimeout = () => {}
|
|
20
|
-
|
|
21
|
-
return calls
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
describe('setTimeout', function () {
|
|
25
|
-
afterEach(function () {
|
|
26
|
-
global.setTimeout = originalSetTimeout
|
|
27
|
-
global.clearTimeout = originalClearTimeout
|
|
28
|
-
lockfile.lock = originalLock
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
it('returns a promise and resolves after the parsed timeout', async function () {
|
|
32
|
-
const timerCalls = installTimerMock()
|
|
33
|
-
const calls = []
|
|
34
|
-
|
|
35
|
-
const result = fn(() => calls.push('done'), '2s')
|
|
36
|
-
|
|
37
|
-
expect(result).to.be.instanceOf(Promise)
|
|
38
|
-
await result
|
|
39
|
-
|
|
40
|
-
expect(timerCalls).to.have.length(1)
|
|
41
|
-
expect(timerCalls[0].ms).to.equal(2000)
|
|
42
|
-
expect(calls).to.deep.equal(['done'])
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
it('passes args to the handler', async function () {
|
|
46
|
-
installTimerMock()
|
|
47
|
-
const calls = []
|
|
48
|
-
|
|
49
|
-
await fn((a, b, c) => calls.push([a, b, c]), 1, { args: [1, 'two', true] })
|
|
50
|
-
|
|
51
|
-
expect(calls).to.deep.equal([[1, 'two', true]])
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
it('binds the provided scope to the handler', async function () {
|
|
55
|
-
installTimerMock()
|
|
56
|
-
const scope = { value: 0 }
|
|
57
|
-
|
|
58
|
-
await fn(function (step) {
|
|
59
|
-
this.value += step
|
|
60
|
-
}, 1, { args: [3], scope })
|
|
61
|
-
|
|
62
|
-
expect(scope.value).to.equal(3)
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
it('locks and releases the lock file around the handler', async function () {
|
|
66
|
-
installTimerMock()
|
|
67
|
-
const calls = []
|
|
68
|
-
lockfile.lock = async (lockFile, lockFileOpts) => {
|
|
69
|
-
calls.push(['lock', lockFile, lockFileOpts])
|
|
70
|
-
return async () => {
|
|
71
|
-
calls.push(['release'])
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
await fn(() => calls.push(['handler']), 1, {
|
|
76
|
-
lockFile: '/tmp/aneka-set-timeout.lock',
|
|
77
|
-
lockFileOpts: { stale: 1000 }
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
expect(calls).to.deep.equal([
|
|
81
|
-
['lock', '/tmp/aneka-set-timeout.lock', { stale: 1000 }],
|
|
82
|
-
['handler'],
|
|
83
|
-
['release']
|
|
84
|
-
])
|
|
85
|
-
})
|
|
86
|
-
|
|
87
|
-
it('suppresses lock errors when silent is left at the default', async function () {
|
|
88
|
-
installTimerMock()
|
|
89
|
-
const calls = []
|
|
90
|
-
lockfile.lock = async () => {
|
|
91
|
-
throw new Error('lock failed')
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
await fn(() => calls.push('handler'), 1, {
|
|
95
|
-
lockFile: '/tmp/aneka-set-timeout.lock'
|
|
96
|
-
})
|
|
97
|
-
|
|
98
|
-
expect(calls).to.deep.equal([])
|
|
99
|
-
})
|
|
100
|
-
|
|
101
|
-
it('rethrows lock errors when silent is false', async function () {
|
|
102
|
-
installTimerMock()
|
|
103
|
-
const error = new Error('lock failed')
|
|
104
|
-
lockfile.lock = async () => {
|
|
105
|
-
throw error
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
let thrown
|
|
109
|
-
try {
|
|
110
|
-
await fn(() => {}, 1, {
|
|
111
|
-
lockFile: '/tmp/aneka-set-timeout.lock',
|
|
112
|
-
silent: false
|
|
113
|
-
})
|
|
114
|
-
} catch (err) {
|
|
115
|
-
thrown = err
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
expect(thrown).to.equal(error)
|
|
119
|
-
})
|
|
120
|
-
})
|
|
1
|
+
/* eslint-env mocha */
|
|
2
|
+
import { expect } from 'chai'
|
|
3
|
+
import lockfile from '@bybrave/proper-lockfile2'
|
|
4
|
+
import fn from '../src/set-timeout.js'
|
|
5
|
+
|
|
6
|
+
const originalSetTimeout = global.setTimeout
|
|
7
|
+
const originalClearTimeout = global.clearTimeout
|
|
8
|
+
const originalLock = lockfile.lock
|
|
9
|
+
|
|
10
|
+
function installTimerMock () {
|
|
11
|
+
const calls = []
|
|
12
|
+
|
|
13
|
+
global.setTimeout = (callback, ms, ...args) => {
|
|
14
|
+
calls.push({ callback, ms, args })
|
|
15
|
+
queueMicrotask(() => callback(...args))
|
|
16
|
+
return calls.length
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
global.clearTimeout = () => {}
|
|
20
|
+
|
|
21
|
+
return calls
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe('setTimeout', function () {
|
|
25
|
+
afterEach(function () {
|
|
26
|
+
global.setTimeout = originalSetTimeout
|
|
27
|
+
global.clearTimeout = originalClearTimeout
|
|
28
|
+
lockfile.lock = originalLock
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('returns a promise and resolves after the parsed timeout', async function () {
|
|
32
|
+
const timerCalls = installTimerMock()
|
|
33
|
+
const calls = []
|
|
34
|
+
|
|
35
|
+
const result = fn(() => calls.push('done'), '2s')
|
|
36
|
+
|
|
37
|
+
expect(result).to.be.instanceOf(Promise)
|
|
38
|
+
await result
|
|
39
|
+
|
|
40
|
+
expect(timerCalls).to.have.length(1)
|
|
41
|
+
expect(timerCalls[0].ms).to.equal(2000)
|
|
42
|
+
expect(calls).to.deep.equal(['done'])
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('passes args to the handler', async function () {
|
|
46
|
+
installTimerMock()
|
|
47
|
+
const calls = []
|
|
48
|
+
|
|
49
|
+
await fn((a, b, c) => calls.push([a, b, c]), 1, { args: [1, 'two', true] })
|
|
50
|
+
|
|
51
|
+
expect(calls).to.deep.equal([[1, 'two', true]])
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('binds the provided scope to the handler', async function () {
|
|
55
|
+
installTimerMock()
|
|
56
|
+
const scope = { value: 0 }
|
|
57
|
+
|
|
58
|
+
await fn(function (step) {
|
|
59
|
+
this.value += step
|
|
60
|
+
}, 1, { args: [3], scope })
|
|
61
|
+
|
|
62
|
+
expect(scope.value).to.equal(3)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
it('locks and releases the lock file around the handler', async function () {
|
|
66
|
+
installTimerMock()
|
|
67
|
+
const calls = []
|
|
68
|
+
lockfile.lock = async (lockFile, lockFileOpts) => {
|
|
69
|
+
calls.push(['lock', lockFile, lockFileOpts])
|
|
70
|
+
return async () => {
|
|
71
|
+
calls.push(['release'])
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
await fn(() => calls.push(['handler']), 1, {
|
|
76
|
+
lockFile: '/tmp/aneka-set-timeout.lock',
|
|
77
|
+
lockFileOpts: { stale: 1000 }
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
expect(calls).to.deep.equal([
|
|
81
|
+
['lock', '/tmp/aneka-set-timeout.lock', { stale: 1000 }],
|
|
82
|
+
['handler'],
|
|
83
|
+
['release']
|
|
84
|
+
])
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('suppresses lock errors when silent is left at the default', async function () {
|
|
88
|
+
installTimerMock()
|
|
89
|
+
const calls = []
|
|
90
|
+
lockfile.lock = async () => {
|
|
91
|
+
throw new Error('lock failed')
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
await fn(() => calls.push('handler'), 1, {
|
|
95
|
+
lockFile: '/tmp/aneka-set-timeout.lock'
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
expect(calls).to.deep.equal([])
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('rethrows lock errors when silent is false', async function () {
|
|
102
|
+
installTimerMock()
|
|
103
|
+
const error = new Error('lock failed')
|
|
104
|
+
lockfile.lock = async () => {
|
|
105
|
+
throw error
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
let thrown
|
|
109
|
+
try {
|
|
110
|
+
await fn(() => {}, 1, {
|
|
111
|
+
lockFile: '/tmp/aneka-set-timeout.lock',
|
|
112
|
+
silent: false
|
|
113
|
+
})
|
|
114
|
+
} catch (err) {
|
|
115
|
+
thrown = err
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
expect(thrown).to.equal(error)
|
|
119
|
+
})
|
|
120
|
+
})
|