11'use strict'
22
33const { randomFillSync } = require ( 'node:crypto' )
4- const { setTimeout : sleep } = require ( 'node:timers/promises' )
4+ const { setTimeout : sleep , setImmediate : nextTick } = require ( 'node:timers/promises' )
55const { test } = require ( 'node:test' )
6- const { fetch, Request, Agent, setGlobalDispatcher } = require ( '../..' )
6+ const { fetch, Request, Response , Agent, setGlobalDispatcher } = require ( '../..' )
77const { createServer } = require ( 'node:http' )
88const { closeServerAsPromise } = require ( '../utils/node-http' )
9- const assert = require ( 'node:assert' )
109
1110const blob = randomFillSync ( new Uint8Array ( 1024 * 512 ) )
1211
@@ -20,14 +19,31 @@ test('test finalizer cloned request', async () => {
2019
2120 const request = new Request ( 'http://localhost' , { method : 'POST' , body : 'Hello' } )
2221
23- for ( let i = 0 ; i < 800 ; ++ i ) request . clone ( )
22+ request . clone ( )
2423
25- await sleep ( 50 )
24+ await nextTick ( )
25+ // eslint-disable-next-line no-undef
26+ gc ( )
27+
28+ await nextTick ( )
29+ await request . arrayBuffer ( ) // check consume body
30+ } )
31+
32+ test ( 'test finalizer cloned response' , async ( ) => {
33+ if ( ! hasGC ) {
34+ throw new Error ( 'gc is not available. Run with \'--expose-gc\'.' )
35+ }
36+
37+ const response = new Response ( 'Hello' )
38+
39+ response . clone ( )
2640
41+ await nextTick ( )
2742 // eslint-disable-next-line no-undef
28- gc ( true )
43+ gc ( )
2944
30- assert . strictEqual ( request . bodyUsed , false )
45+ await nextTick ( )
46+ await response . arrayBuffer ( ) // check consume body
3147} )
3248
3349test ( 'does not need the body to be consumed to continue' , { timeout : 180_000 } , async ( t ) => {
0 commit comments