|
| 1 | +// Flags: --expose-gc |
1 | 2 | 'use strict'; |
2 | 3 |
|
3 | | -require('../common'); |
| 4 | +const common = require('../common'); |
4 | 5 | const assert = require('assert'); |
5 | 6 | const { once } = require('node:events'); |
6 | 7 | const { describe, it } = require('node:test'); |
7 | 8 |
|
8 | 9 | describe('AbortSignal.any() with timeout signals', () => { |
9 | 10 | it('should abort when the first timeout signal fires', async () => { |
10 | | - const signal = AbortSignal.any([AbortSignal.timeout(9000), AbortSignal.timeout(110000)]); |
| 11 | + const signal = AbortSignal.any([ |
| 12 | + AbortSignal.timeout(common.platformTimeout(1000)), |
| 13 | + AbortSignal.timeout(110000), |
| 14 | + ]); |
| 15 | + let timeout; |
11 | 16 |
|
12 | 17 | const abortPromise = Promise.race([ |
13 | 18 | once(signal, 'abort').then(() => { |
14 | 19 | throw signal.reason; |
15 | 20 | }), |
16 | | - new Promise((resolve) => setTimeout(resolve, 10000)), |
| 21 | + new Promise((resolve) => { |
| 22 | + timeout = setTimeout(resolve, common.platformTimeout(10000)); |
| 23 | + }), |
17 | 24 | ]); |
18 | 25 |
|
19 | | - // The promise should be aborted by the 9000ms timeout |
20 | | - await assert.rejects( |
21 | | - () => abortPromise, |
22 | | - { |
23 | | - name: 'TimeoutError', |
24 | | - message: 'The operation was aborted due to timeout' |
25 | | - } |
26 | | - ); |
| 26 | + // Collect after this turn so the WeakRefs no longer keep the timeout |
| 27 | + // signals alive by themselves. |
| 28 | + setImmediate(common.mustCall(() => globalThis.gc())); |
| 29 | + |
| 30 | + try { |
| 31 | + await assert.rejects( |
| 32 | + () => abortPromise, |
| 33 | + { |
| 34 | + name: 'TimeoutError', |
| 35 | + message: 'The operation was aborted due to timeout' |
| 36 | + } |
| 37 | + ); |
| 38 | + } finally { |
| 39 | + clearTimeout(timeout); |
| 40 | + } |
27 | 41 | }); |
28 | 42 | }); |
0 commit comments