test: fix flaky tests by KhafraDev · Pull Request #1523 · node-fetch/node-fetch · GitHub
Skip to content

test: fix flaky tests#1523

Merged
jimmywarting merged 1 commit into
node-fetch:mainfrom
KhafraDev:fix-possibly-flaky-test
Jul 12, 2022
Merged

test: fix flaky tests#1523
jimmywarting merged 1 commit into
node-fetch:mainfrom
KhafraDev:fix-possibly-flaky-test

Conversation

@KhafraDev

@KhafraDev KhafraDev commented Mar 16, 2022

Copy link
Copy Markdown
Contributor

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

@LinusU LinusU left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown

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