From 5c6e2bcf4579c3e30351c72a1148a241bcfb3678 Mon Sep 17 00:00:00 2001 From: Adam Bukowski Date: Tue, 30 Jan 2024 09:22:06 +0100 Subject: [PATCH] fix: do not persist rejections (#55) if a fetch promise rejected for some reason, the resource cache was not cleared, so every consecutive request was failing. --- lib/data-provider.js | 2 +- test/data-provider-test.js | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/lib/data-provider.js b/lib/data-provider.js index fa66ff5..8d59c63 100644 --- a/lib/data-provider.js +++ b/lib/data-provider.js @@ -56,7 +56,7 @@ module.exports = function DataProvider(config) { return response.text(); }) - .then((text) => { + .finally((text) => { pendingReqests.delete(resource); return text; diff --git a/test/data-provider-test.js b/test/data-provider-test.js index b99dab5..dfd0bb8 100644 --- a/test/data-provider-test.js +++ b/test/data-provider-test.js @@ -92,6 +92,52 @@ describe('Data Provider', () => { .catch(done); }); + it('should not hang the next request when the previous fails', async () => { + // given + let requestCount = 0; + const server = http.createServer((req, res) => { + requestCount += 1; + + setTimeout(() => { + if (requestCount === 1) { + res.writeHead(403, { 'Content-Type': 'text/html' }); + res.end(`failing ${requestCount}`); + } else { + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(`ok ${requestCount}`); + } + }, 1); + }); + server.listen(); + + const port = server.address().port; + const dataProvider = new DataProvider({ + baseUrl: 'http://localhost:' + port, + }); + + // when + await Promise.all([ + assert.rejects(dataProvider.get('/'), { + name: 'Error', + message: 'HTTP error 403: Forbidden', + }), + assert.rejects(dataProvider.get('/'), { + name: 'Error', + message: 'HTTP error 403: Forbidden', + }), + ]); + + // then + const results = Promise.all([ + dataProvider.get('/'), + dataProvider.get('/'), + ]); + + await assert.doesNotReject(results); + assert.deepEqual(await results, ['ok 2', 'ok 2']); + assert.equal(requestCount, 2); + }); + it('allows to configure custom user agent', (done) => { // given let calledUserAgent;