Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not persist rejections #55

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/data-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module.exports = function DataProvider(config) {

return response.text();
})
.then((text) => {
.finally((text) => {
pendingReqests.delete(resource);

return text;
Expand Down
46 changes: 46 additions & 0 deletions test/data-provider-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading