|
1 | 1 | import {test} from 'node:test'; |
2 | | -import assert from 'node:assert'; |
| 2 | +import assert from 'node:assert/strict'; |
3 | 3 | import delay from 'delay'; |
4 | 4 | import PQueue from '../source/index.js'; |
5 | 5 |
|
@@ -122,6 +122,63 @@ test('runningTasks returns fresh arrays', async () => { |
122 | 122 | await queue.onIdle(); |
123 | 123 | }); |
124 | 124 |
|
| 125 | +test('runningTasks includes timeoutRemaining for tasks with timeout', async () => { |
| 126 | + const queue = new PQueue({concurrency: 2, timeout: 5000}); |
| 127 | + |
| 128 | + queue.add(async () => delay(200), {id: 'with-timeout'}); |
| 129 | + queue.add(async () => delay(200), {id: 'custom-timeout', timeout: 2000}); |
| 130 | + |
| 131 | + await delay(10); |
| 132 | + |
| 133 | + const running = queue.runningTasks; |
| 134 | + const withTimeout = running.find(t => t.id === 'with-timeout'); |
| 135 | + const customTimeout = running.find(t => t.id === 'custom-timeout'); |
| 136 | + |
| 137 | + assert.ok(withTimeout); |
| 138 | + assert.ok(typeof withTimeout.timeoutRemaining === 'number'); |
| 139 | + assert.ok(withTimeout.timeoutRemaining! > 0); |
| 140 | + assert.ok(withTimeout.timeoutRemaining! <= 5000); |
| 141 | + |
| 142 | + assert.ok(customTimeout); |
| 143 | + assert.ok(typeof customTimeout.timeoutRemaining === 'number'); |
| 144 | + assert.ok(customTimeout.timeoutRemaining! > 0); |
| 145 | + assert.ok(customTimeout.timeoutRemaining! <= 2000); |
| 146 | + |
| 147 | + await queue.onIdle(); |
| 148 | +}); |
| 149 | + |
| 150 | +test('runningTasks timeoutRemaining is undefined when no timeout is set', async () => { |
| 151 | + const queue = new PQueue({concurrency: 1}); |
| 152 | + |
| 153 | + queue.add(async () => delay(100), {id: 'no-timeout'}); |
| 154 | + |
| 155 | + await delay(10); |
| 156 | + |
| 157 | + const running = queue.runningTasks; |
| 158 | + assert.equal(running.length, 1); |
| 159 | + assert.equal(running[0].timeoutRemaining, undefined); |
| 160 | + |
| 161 | + await queue.onIdle(); |
| 162 | +}); |
| 163 | + |
| 164 | +test('runningTasks timeoutRemaining decreases over time', async () => { |
| 165 | + const queue = new PQueue({concurrency: 1, timeout: 5000}); |
| 166 | + |
| 167 | + queue.add(async () => delay(300), {id: 'task'}); |
| 168 | + |
| 169 | + await delay(10); |
| 170 | + |
| 171 | + const remaining1 = queue.runningTasks[0].timeoutRemaining!; |
| 172 | + |
| 173 | + await delay(100); |
| 174 | + |
| 175 | + const remaining2 = queue.runningTasks[0].timeoutRemaining!; |
| 176 | + |
| 177 | + assert.ok(remaining2 < remaining1, `Expected ${remaining2} < ${remaining1}`); |
| 178 | + |
| 179 | + await queue.onIdle(); |
| 180 | +}); |
| 181 | + |
125 | 182 | test('runningTasks handles tasks without IDs', async () => { |
126 | 183 | const queue = new PQueue({concurrency: 2}); |
127 | 184 |
|
|
0 commit comments