@@ -10,16 +10,17 @@ const { fetch, Agent, RetryAgent } = require('..')
1010test ( 'https://github.com/nodejs/undici/issues/3356' , { skip : process . env . CITGM } , async ( t ) => {
1111 t = tspl ( t , { plan : 3 } )
1212
13- let shouldRetry = true
13+ let requestCount = 0
1414 const server = createServer ( { joinDuplicateHeaders : true } )
1515 server . on ( 'request' , ( req , res ) => {
16+ requestCount ++
1617 res . writeHead ( 200 , { 'content-type' : 'text/plain' } )
17- if ( shouldRetry ) {
18- shouldRetry = false
19-
18+ if ( requestCount === 1 ) {
19+ // First request: send headers and partial body, then delay the rest
20+ // long enough for the bodyTimeout to fire via fast timers
2021 res . flushHeaders ( )
2122 res . write ( 'h' )
22- setTimeout ( ( ) => { res . end ( 'ello world!' ) } , 100 )
23+ setTimeout ( ( ) => { res . end ( 'ello world!' ) } , 3000 )
2324 } else {
2425 res . end ( 'hello world!' )
2526 }
@@ -29,8 +30,10 @@ test('https://github.com/nodejs/undici/issues/3356', { skip: process.env.CITGM }
2930
3031 await once ( server , 'listening' )
3132
32- const agent = new RetryAgent ( new Agent ( { bodyTimeout : 50 } ) , {
33- errorCodes : [ 'UND_ERR_BODY_TIMEOUT' ]
33+ const agent = new RetryAgent ( new Agent ( { bodyTimeout : 1500 } ) , {
34+ errorCodes : [ 'UND_ERR_BODY_TIMEOUT' ] ,
35+ minTimeout : 10 ,
36+ maxTimeout : 100
3437 } )
3538
3639 after ( async ( ) => {
@@ -44,11 +47,14 @@ test('https://github.com/nodejs/undici/issues/3356', { skip: process.env.CITGM }
4447 dispatcher : agent
4548 } )
4649
47- fastTimersTick ( )
50+ // Advance fast timers to trigger the body timeout.
51+ // The fast timer resolution is ~1s, so we need to tick past the 1500ms bodyTimeout.
52+ fastTimersTick ( 2000 )
4853
4954 try {
5055 t . equal ( response . status , 200 )
51- // consume response
56+ // consume response - this should throw because the retry mechanism
57+ // cannot transparently retry after headers have already been forwarded
5258 await response . text ( )
5359 } catch ( err ) {
5460 t . equal ( err . name , 'TypeError' )
0 commit comments