Skip to content

Commit 974f1e9

Browse files
authoredJun 26, 2024··
Improve robustness of request construction (#600)
* Improve robustness of request construction * Fix flakey retry tests
1 parent b1effd9 commit 974f1e9

File tree

5 files changed

+40
-57
lines changed

5 files changed

+40
-57
lines changed
 

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"node": ">=18"
2323
},
2424
"scripts": {
25-
"test": "xo && npm run build && ava --timeout=10m --serial",
25+
"test": "xo && npm run build && ava",
2626
"debug": "PWDEBUG=1 ava --timeout=2m",
2727
"release": "np",
2828
"build": "del-cli distribution && tsc --project tsconfig.dist.json",

‎source/core/Ky.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ export class Ky {
179179
this._options.duplex = 'half';
180180
}
181181

182+
if (this._options.json !== undefined) {
183+
this._options.body = this._options.stringifyJson?.(this._options.json) ?? JSON.stringify(this._options.json);
184+
this._options.headers.set('content-type', this._options.headers.get('content-type') ?? 'application/json');
185+
}
186+
182187
this.request = new globalThis.Request(this._input, this._options);
183188

184189
if (this._options.searchParams) {
@@ -201,12 +206,6 @@ export class Ky {
201206
// The spread of `this.request` is required as otherwise it misses the `duplex` option for some reason and throws.
202207
this.request = new globalThis.Request(new globalThis.Request(url, {...this.request}), this._options as RequestInit);
203208
}
204-
205-
if (this._options.json !== undefined) {
206-
this._options.body = this._options.stringifyJson?.(this._options.json) ?? JSON.stringify(this._options.json);
207-
this.request.headers.set('content-type', this._options.headers.get('content-type') ?? 'application/json');
208-
this.request = new globalThis.Request(this.request, {body: this._options.body});
209-
}
210209
}
211210

212211
protected _calculateRetryDelay(error: unknown) {

‎test/helpers/with-performance-observer.ts

-42
This file was deleted.

‎test/helpers/with-performance.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {performance} from 'node:perf_hooks';
2+
import process from 'node:process';
3+
import type {ExecutionContext} from 'ava';
4+
5+
type Argument = {
6+
expectedDuration: number;
7+
t: ExecutionContext;
8+
test: () => Promise<void>;
9+
};
10+
11+
// We allow the tests to take more time on CI than locally, to reduce flakiness
12+
const allowedOffset = process.env.CI ? 300 : 200;
13+
14+
export async function withPerformance({
15+
expectedDuration,
16+
t,
17+
test,
18+
}: Argument) {
19+
const startTime = performance.now();
20+
await test();
21+
const endTime = performance.now();
22+
23+
const duration = endTime - startTime;
24+
t.true(
25+
Math.abs(duration - expectedDuration) < allowedOffset,
26+
`Duration of ${duration}ms is not close to expected duration ${expectedDuration}ms`,
27+
);
28+
}

‎test/retry.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import test from 'ava';
22
import ky from '../source/index.js';
33
import {createHttpTestServer} from './helpers/create-http-test-server.js';
4-
import {withPerformanceObserver} from './helpers/with-performance-observer.js';
4+
import {withPerformance} from './helpers/with-performance.js';
55

66
const fixture = 'fixture';
77
const defaultRetryCount = 2;
@@ -442,7 +442,7 @@ test('throws when retry.statusCodes is not an array', async t => {
442442
await server.close();
443443
});
444444

445-
test('respect maximum backoff', async t => {
445+
test('respect maximum backoffLimit', async t => {
446446
const retryCount = 4;
447447
let requestCount = 0;
448448

@@ -457,9 +457,8 @@ test('respect maximum backoff', async t => {
457457
}
458458
});
459459

460-
await withPerformanceObserver({
460+
await withPerformance({
461461
t,
462-
name: 'default',
463462
expectedDuration: 300 + 600 + 1200 + 2400,
464463
async test() {
465464
t.is(await ky(server.url, {
@@ -469,9 +468,9 @@ test('respect maximum backoff', async t => {
469468
});
470469

471470
requestCount = 0;
472-
await withPerformanceObserver({
471+
472+
await withPerformance({
473473
t,
474-
name: 'custom',
475474
expectedDuration: 300 + 600 + 1000 + 1000,
476475
async test() {
477476
t.is(await ky(server.url, {
@@ -501,9 +500,8 @@ test('respect custom retry.delay', async t => {
501500
}
502501
});
503502

504-
await withPerformanceObserver({
503+
await withPerformance({
505504
t,
506-
name: 'linear',
507505
expectedDuration: 200 + 300 + 400 + 500,
508506
async test() {
509507
t.is(await ky(server.url, {

0 commit comments

Comments
 (0)
Please sign in to comment.