aneka 0.16.0 → 0.17.1
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/CHANGES.md +33 -27
- package/docs/arrange-array.js.html +2 -2
- package/docs/create-class.js.html +2 -2
- package/docs/current-loc.js.html +2 -2
- package/docs/data/search.json +1 -1
- package/docs/defaults-deep.js.html +2 -2
- package/docs/delay.js.html +6 -3
- package/docs/extract-text.js.html +2 -2
- package/docs/fill-object.js.html +2 -2
- package/docs/filter-duplicate.js.html +2 -2
- package/docs/filter-index.js.html +2 -2
- package/docs/format-text.js.html +2 -2
- package/docs/generate-id.js.html +2 -2
- package/docs/get-caller-filename.js.html +2 -2
- package/docs/get-global-module-dir.js.html +23 -0
- package/docs/get-index.js.html +2 -2
- package/docs/get-key-by-value.js.html +2 -2
- package/docs/global.html +4 -4
- package/docs/includes.js.html +2 -2
- package/docs/index.html +2 -2
- package/docs/is-class.js.html +2 -2
- package/docs/is-set.js.html +2 -2
- package/docs/merge-objects-by-key.js.html +2 -2
- package/docs/paginate.js.html +2 -2
- package/docs/parse-args-argv.js.html +2 -2
- package/docs/parse-duration.js.html +2 -2
- package/docs/parse-env.js.html +2 -2
- package/docs/parse-kv-string.js.html +2 -2
- package/docs/parse-object.js.html +2 -2
- package/docs/pascal-case.js.html +2 -2
- package/docs/random-range.js.html +2 -2
- package/docs/resolve-path.js.html +2 -2
- package/docs/round.js.html +2 -2
- package/docs/sec-to-hms.js.html +2 -2
- package/docs/set-immediate.js.html +2 -2
- package/docs/set-interval.js.html +57 -0
- package/docs/set-timeout.js.html +37 -0
- package/docs/shim.js.html +2 -2
- package/docs/text-to-array.js.html +2 -2
- package/docs/titleize.js.html +2 -2
- package/docs/without.js.html +2 -2
- package/index.js +7 -0
- package/package.json +2 -1
- package/src/delay.js +4 -1
- package/src/set-interval.js +54 -0
- package/src/set-timeout.js +34 -0
- package/test/set-interval.js +190 -0
- package/test/set-timeout.js +120 -0
|
@@ -0,0 +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
|
+
})
|