Skip to content

Commit

Permalink
test: timeout flaky fix (#668)
Browse files Browse the repository at this point in the history
* test: timeout flaky fix

* test: fix by chaining `nock`

* test: doc

* chore: some test environments are _really_ slow

* chore: timeout

* chore: additional enhancements

* test: cleanup

* fix: rebuild `timeout` between requests
  • Loading branch information
d-goog authored Nov 14, 2024
1 parent 352d61b commit 26d6346
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
20 changes: 14 additions & 6 deletions src/gaxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,12 @@ export class Gaxios {
// copy the retry state over to the existing config
opts.retryConfig = err.config?.retryConfig;

// re-prepare timeout for the next request
this.#appendTimeoutToSignal(opts);

return this._request<T>(opts);
}

throw err;
}
}
Expand Down Expand Up @@ -473,20 +477,24 @@ export class Gaxios {
(opts as {duplex: string}).duplex = 'half';
}

this.#appendTimeoutToSignal(opts);

return Object.assign(opts, {
headers: preparedHeaders,
url: opts.url instanceof URL ? opts.url : new URL(opts.url),
});
}

#appendTimeoutToSignal(opts: GaxiosOptions) {
if (opts.timeout) {
const timeoutSignal = AbortSignal.timeout(opts.timeout);

if (opts.signal) {
if (opts.signal && !opts.signal.aborted) {
opts.signal = AbortSignal.any([opts.signal, timeoutSignal]);
} else {
opts.signal = timeoutSignal;
}
}

return Object.assign(opts, {
headers: preparedHeaders,
url: opts.url instanceof URL ? opts.url : new URL(opts.url),
});
}

/**
Expand Down
15 changes: 10 additions & 5 deletions test/test.retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,19 @@ describe('🛸 retry & exponential backoff', () => {
});

it('should retry on `timeout`', async () => {
let scope = nock(url).get('/').delay(2000).reply(400);
const scope = nock(url).get('/').delay(500).reply(400).get('/').reply(204);

const gaxios = new Gaxios();
const timeout = 10;
const timeout = 100;

function onRetryAttempt() {
// prepare nock for next request
scope = nock(url).get('/').reply(204);
async function onRetryAttempt({config, message}: GaxiosError) {
assert(config.signal?.reason instanceof DOMException);
assert.equal(config.signal.reason.name, 'TimeoutError');
assert.match(message, /timeout/i);

// increase timeout to something higher to avoid time-sensitive flaky tests
// note: the second `nock` GET is not delayed like the first one
config.timeout = 10000;
}

const res = await gaxios.request({
Expand Down

0 comments on commit 26d6346

Please sign in to comment.