From 9a56796b3e61713f7f2e44ea8c9622b158697c45 Mon Sep 17 00:00:00 2001 From: Alessandro Commodari Date: Sat, 16 Mar 2024 08:46:34 -0400 Subject: [PATCH] fix: fix retry handler option (#2962) --- lib/handler/retry-handler.js | 7 ++-- test/retry-handler.js | 72 ++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 3 deletions(-) diff --git a/lib/handler/retry-handler.js b/lib/handler/retry-handler.js index f2e22f6d4c0..15894470ff3 100644 --- a/lib/handler/retry-handler.js +++ b/lib/handler/retry-handler.js @@ -1,3 +1,4 @@ +'use strict' const assert = require('node:assert') const { kRetryHandlerDefaultRetry } = require('../core/symbols') @@ -37,7 +38,7 @@ class RetryHandler { retry: retryFn ?? RetryHandler[kRetryHandlerDefaultRetry], retryAfter: retryAfter ?? true, maxTimeout: maxTimeout ?? 30 * 1000, // 30s, - timeout: minTimeout ?? 500, // .5s + minTimeout: minTimeout ?? 500, // .5s timeoutFactor: timeoutFactor ?? 2, maxRetries: maxRetries ?? 5, // What errors we should retry @@ -104,7 +105,7 @@ class RetryHandler { const { method, retryOptions } = opts const { maxRetries, - timeout, + minTimeout, maxTimeout, timeoutFactor, statusCodes, @@ -114,7 +115,7 @@ class RetryHandler { let { counter, currentTimeout } = state currentTimeout = - currentTimeout != null && currentTimeout > 0 ? currentTimeout : timeout + currentTimeout != null && currentTimeout > 0 ? currentTimeout : minTimeout // Any code that is not a Undici's originated and allowed to retry if ( diff --git a/test/retry-handler.js b/test/retry-handler.js index f6b83cd34dd..2792f23c2b0 100644 --- a/test/retry-handler.js +++ b/test/retry-handler.js @@ -706,3 +706,75 @@ test('should not error if request is not meant to be retried', async t => { await t.completed }) + +test('Should be able to properly pass the minTimeout to the RetryContext when constructing a RetryCallback function', async t => { + t = tspl(t, { plan: 2 }) + + let counter = 0 + const server = createServer() + server.on('request', (req, res) => { + switch (counter) { + case 0: + res.writeHead(500) + res.end('failed') + return + case 1: + res.writeHead(200) + res.end('hello world!') + return + default: + t.fail() + } + }) + + const dispatchOptions = { + retryOptions: { + retry: (err, { state, opts }, done) => { + counter++ + t.strictEqual(opts.retryOptions.minTimeout, 100) + + if (err.statusCode === 500) { + return done() + } + + return done(err) + }, + minTimeout: 100 + }, + method: 'GET', + path: '/', + headers: { + 'content-type': 'application/json' + } + } + + server.listen(0, () => { + const client = new Client(`http://localhost:${server.address().port}`) + const handler = new RetryHandler(dispatchOptions, { + dispatch: client.dispatch.bind(client), + handler: new RequestHandler(dispatchOptions, (err, data) => { + t.ifError(err) + }) + }) + + after(async () => { + await client.close() + server.close() + + await once(server, 'close') + }) + + client.dispatch( + { + method: 'GET', + path: '/', + headers: { + 'content-type': 'application/json' + } + }, + handler + ) + }) + + await t.completed +})