|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | 3 | const { randomFillSync } = require('node:crypto') |
4 | | -const { setTimeout: sleep } = require('timers/promises') |
| 4 | +const { setTimeout: sleep, setImmediate: nextTick } = require('node:timers/promises') |
5 | 5 | const { test } = require('node:test') |
6 | | -const { fetch, Agent, setGlobalDispatcher } = require('../..') |
| 6 | +const { fetch, Request, Response, Agent, setGlobalDispatcher } = require('../..') |
7 | 7 | const { createServer } = require('node:http') |
8 | 8 | const { closeServerAsPromise } = require('../utils/node-http') |
9 | 9 |
|
10 | 10 | const blob = randomFillSync(new Uint8Array(1024 * 512)) |
11 | 11 |
|
| 12 | +const hasGC = typeof global.gc !== 'undefined' |
| 13 | + |
12 | 14 | // Enable when/if FinalizationRegistry in Node.js 18 becomes stable again |
13 | 15 | const isNode18 = process.version.startsWith('v18') |
14 | 16 |
|
| 17 | +// https://github.com/nodejs/undici/issues/4150 |
| 18 | +test('test finalizer cloned request', async () => { |
| 19 | + if (!hasGC) { |
| 20 | + throw new Error('gc is not available. Run with \'--expose-gc\'.') |
| 21 | + } |
| 22 | + |
| 23 | + const request = new Request('http://localhost', { method: 'POST', body: 'Hello' }) |
| 24 | + |
| 25 | + request.clone() |
| 26 | + |
| 27 | + await nextTick() |
| 28 | + // eslint-disable-next-line no-undef |
| 29 | + gc() |
| 30 | + |
| 31 | + await nextTick() |
| 32 | + await request.arrayBuffer() // check consume body |
| 33 | +}) |
| 34 | + |
| 35 | +test('test finalizer cloned response', async () => { |
| 36 | + if (!hasGC) { |
| 37 | + throw new Error('gc is not available. Run with \'--expose-gc\'.') |
| 38 | + } |
| 39 | + |
| 40 | + const response = new Response('Hello') |
| 41 | + |
| 42 | + response.clone() |
| 43 | + |
| 44 | + await nextTick() |
| 45 | + // eslint-disable-next-line no-undef |
| 46 | + gc() |
| 47 | + |
| 48 | + await nextTick() |
| 49 | + await response.arrayBuffer() // check consume body |
| 50 | +}) |
| 51 | + |
15 | 52 | test('does not need the body to be consumed to continue', { timeout: 180_000, skip: isNode18 }, async (t) => { |
16 | 53 | const agent = new Agent({ |
17 | 54 | keepAliveMaxTimeout: 10, |
|
0 commit comments