Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

KhafraDev
Copy link
Contributor

@KhafraDev KhafraDev commented Mar 16, 2022

Purpose

By relying upon a 100ms delay before aborting, it is possible for the request to succeed before being canceled.

Since these 2 tests merely ensures that cancellation via AbortController works, adding a delay isn't needed regardless.

Changes

Removes delay between request and canceling.

Additional information

Undici runs node-fetch's test suite (more or less) and this test is flaky (https://github.com/nodejs/undici/runs/5153562802?check_suite_focus=true for example).

Example code with the issue fixed:

import fetch from 'node-fetch';
import { createServer } from 'http';
import { once } from 'events';

const server = createServer((req, res) => res.end());
server.listen(3000, () => console.log('done'));
await once(server, 'listening');

let i = 1;
while (i++) {
	try {
		const controller = new AbortController();

		const promise = fetch(`http://localhost:3000/`, {
			method: 'POST',
			signal: controller.signal,
			headers: {
				'Content-Type': 'application/json',
				body: '{"hello": "world"}'
			}
		});

		controller.abort()

		const test = await promise;
		console.log('did not fail', test);
		break;
	} catch {
		if (i % 1e3 === 0) {
			console.log('did not fail after ' + i + ' tries');
		}
	}
}

@KhafraDev KhafraDev changed the title tests: fix flaky tests test: fix flaky tests Mar 16, 2022
Copy link
Member

@LinusU LinusU left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat πŸ‘

@jimmywarting jimmywarting merged commit 11b7033 into node-fetch:main Jul 12, 2022
@github-actions
Copy link

πŸŽ‰ This PR is included in version 3.2.8 πŸŽ‰

The release is available on:

Your semantic-release bot πŸ“¦πŸš€

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants