aneka 0.17.6 → 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.
Files changed (147) hide show
  1. package/.jsdoc.conf.json +0 -0
  2. package/CHANGES.md +87 -83
  3. package/LICENSE +21 -21
  4. package/README.md +63 -63
  5. package/docs/arrange-array.js.html +0 -0
  6. package/docs/create-class.js.html +0 -0
  7. package/docs/current-loc.js.html +0 -0
  8. package/docs/data/search.json +0 -0
  9. package/docs/defaults-deep.js.html +0 -0
  10. package/docs/delay.js.html +0 -0
  11. package/docs/extract-text.js.html +0 -0
  12. package/docs/fill-object.js.html +0 -0
  13. package/docs/filter-duplicate.js.html +0 -0
  14. package/docs/filter-index.js.html +0 -0
  15. package/docs/fonts/Inconsolata-Regular.ttf +0 -0
  16. package/docs/fonts/OpenSans-Regular.ttf +0 -0
  17. package/docs/fonts/WorkSans-Bold.ttf +0 -0
  18. package/docs/format-text.js.html +0 -0
  19. package/docs/generate-id.js.html +0 -0
  20. package/docs/get-caller-filename.js.html +0 -0
  21. package/docs/get-global-module-dir.js.html +0 -0
  22. package/docs/get-index.js.html +0 -0
  23. package/docs/get-key-by-value.js.html +0 -0
  24. package/docs/global.html +0 -0
  25. package/docs/includes.js.html +0 -0
  26. package/docs/index.html +0 -0
  27. package/docs/is-class.js.html +0 -0
  28. package/docs/is-set.js.html +0 -0
  29. package/docs/merge-objects-by-key.js.html +0 -0
  30. package/docs/paginate.js.html +0 -0
  31. package/docs/parse-args-argv.js.html +0 -0
  32. package/docs/parse-duration.js.html +0 -0
  33. package/docs/parse-env.js.html +0 -0
  34. package/docs/parse-kv-string.js.html +0 -0
  35. package/docs/parse-object.js.html +0 -0
  36. package/docs/pascal-case.js.html +0 -0
  37. package/docs/random-range.js.html +0 -0
  38. package/docs/resolve-path.js.html +0 -0
  39. package/docs/round.js.html +0 -0
  40. package/docs/scripts/core.js +0 -0
  41. package/docs/scripts/core.min.js +0 -0
  42. package/docs/scripts/resize.js +0 -0
  43. package/docs/scripts/search.js +0 -0
  44. package/docs/scripts/search.min.js +5 -5
  45. package/docs/scripts/third-party/Apache-License-2.0.txt +0 -0
  46. package/docs/scripts/third-party/fuse.js +0 -0
  47. package/docs/scripts/third-party/hljs-line-num-original.js +0 -0
  48. package/docs/scripts/third-party/hljs-line-num.js +0 -0
  49. package/docs/scripts/third-party/hljs-original.js +0 -0
  50. package/docs/scripts/third-party/hljs.js +0 -0
  51. package/docs/scripts/third-party/popper.js +0 -0
  52. package/docs/scripts/third-party/tippy.js +0 -0
  53. package/docs/scripts/third-party/tocbot.js +0 -0
  54. package/docs/scripts/third-party/tocbot.min.js +0 -0
  55. package/docs/sec-to-hms.js.html +0 -0
  56. package/docs/set-immediate.js.html +0 -0
  57. package/docs/set-interval.js.html +0 -0
  58. package/docs/set-timeout.js.html +0 -0
  59. package/docs/shim.js.html +0 -0
  60. package/docs/static/bitcoin.jpeg +0 -0
  61. package/docs/styles/clean-jsdoc-theme-base.css +0 -0
  62. package/docs/styles/clean-jsdoc-theme-dark.css +0 -0
  63. package/docs/styles/clean-jsdoc-theme-light.css +0 -0
  64. package/docs/styles/clean-jsdoc-theme-scrollbar.css +0 -0
  65. package/docs/styles/clean-jsdoc-theme-without-scrollbar.min.css +0 -0
  66. package/docs/styles/clean-jsdoc-theme.min.css +0 -0
  67. package/docs/text-to-array.js.html +0 -0
  68. package/docs/titleize.js.html +0 -0
  69. package/docs/without.js.html +0 -0
  70. package/index.js +119 -119
  71. package/package.json +48 -48
  72. package/src/arrange-array.js +25 -25
  73. package/src/create-class.js +35 -35
  74. package/src/current-loc.js +0 -0
  75. package/src/defaults-deep.js +20 -20
  76. package/src/delay.js +19 -19
  77. package/src/extract-text.js +23 -23
  78. package/src/fill-object.js +17 -17
  79. package/src/filter-duplicate.js +26 -26
  80. package/src/filter-index.js +17 -17
  81. package/src/format-text.js +88 -88
  82. package/src/generate-id.js +35 -35
  83. package/src/get-caller-filename.js +25 -25
  84. package/src/get-global-module-dir.js +24 -20
  85. package/src/get-index.js +15 -15
  86. package/src/get-key-by-value.js +12 -12
  87. package/src/includes.js +18 -18
  88. package/src/is-class.js +13 -13
  89. package/src/is-set.js +11 -11
  90. package/src/log.js +19 -19
  91. package/src/merge-objects-by-key.js +24 -24
  92. package/src/paginate.js +45 -45
  93. package/src/parse-args-argv.js +0 -0
  94. package/src/parse-duration.js +14 -14
  95. package/src/parse-env.js +0 -0
  96. package/src/parse-kv-string.js +21 -21
  97. package/src/parse-object.js +76 -76
  98. package/src/pascal-case.js +14 -14
  99. package/src/random-range.js +15 -15
  100. package/src/resolve-path.js +0 -0
  101. package/src/round.js +13 -13
  102. package/src/sec-to-hms.js +27 -27
  103. package/src/set-immediate.js +12 -12
  104. package/src/set-interval.js +56 -56
  105. package/src/set-timeout.js +34 -34
  106. package/src/shim.js +28 -28
  107. package/src/text-to-array.js +17 -17
  108. package/src/titleize.js +37 -37
  109. package/src/without.js +18 -18
  110. package/test/arrange-array.js +41 -41
  111. package/test/create-class.js +33 -33
  112. package/test/current-loc.js +24 -24
  113. package/test/defaults-deep.js +19 -19
  114. package/test/delay.js +17 -17
  115. package/test/extract-text.js +17 -17
  116. package/test/fill-object.js +17 -17
  117. package/test/filter-duplicate.js +19 -19
  118. package/test/filter-index.js +17 -17
  119. package/test/format-text.js +26 -26
  120. package/test/generate-id.js +28 -28
  121. package/test/get-caller-filename.js +22 -22
  122. package/test/get-global-module-dir.js +51 -51
  123. package/test/get-index.js +17 -17
  124. package/test/get-key-by-value.js +17 -17
  125. package/test/includes.js +18 -18
  126. package/test/is-class.js +19 -19
  127. package/test/is-set.js +21 -21
  128. package/test/log.js +47 -47
  129. package/test/merge-objects-by-key.js +19 -19
  130. package/test/paginate.js +24 -24
  131. package/test/parse-args-argv.js +63 -63
  132. package/test/parse-duration.js +18 -18
  133. package/test/parse-env.js +35 -35
  134. package/test/parse-kv-string.js +17 -17
  135. package/test/parse-object.js +30 -30
  136. package/test/pascal-case.js +18 -18
  137. package/test/random-range.js +21 -21
  138. package/test/resolve-path.js +22 -22
  139. package/test/round.js +19 -19
  140. package/test/sec-to-hms.js +18 -18
  141. package/test/set-immediate.js +17 -17
  142. package/test/set-interval.js +196 -196
  143. package/test/set-timeout.js +120 -120
  144. package/test/shim.js +34 -34
  145. package/test/text-to-array.js +18 -18
  146. package/test/titleize.js +27 -27
  147. package/test/without.js +17 -17
@@ -1,196 +1,196 @@
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
- })
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
+ })
@@ -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
+ })