<commit_before>import getStream from 'get-stream';
import test from 'ava';
import pEvent from 'p-event';
import delay from 'delay';
import got from '../source';
import {createServer} from './helpers/server';

let s;
const reqDelay = 160;

test.before('setup', async () => {
	s = await createServer();

	s.on('/', async (req, res) => {
		await delay(reqDelay);
		res.statusCode = 200;
		res.end('OK');
	});

	await s.listen(s.port);
});

test('timeout option (ETIMEDOUT)', async t => {
	await t.throws(
		got(s.url, {
			timeout: 0,
			retry: 0
		}),
		{
			code: 'ETIMEDOUT'
		}
	);
});

test('timeout option (ESOCKETTIMEDOUT)', async t => {
	await t.throws(
		got(s.url, {
			timeout: reqDelay,
			retry: 0
		}),
		{
			code: 'ESOCKETTIMEDOUT'
		}
	);
});

test('timeout option as object (ETIMEDOUT)', async t => {
	await t.throws(
		got(s.url, {
			timeout: {socket: reqDelay * 2.5, request: 0},
			retry: 0
		}),
		{
			code: 'ETIMEDOUT'
		}
	);
});

test('timeout option as object (ESOCKETTIMEDOUT)', async t => {
	await t.throws(
		got(s.url, {
			timeout: {socket: reqDelay * 1.5, request: reqDelay},
			retry: 0
		}),
		{
			code: 'ESOCKETTIMEDOUT'
		}
	);
});

test('socket timeout', async t => {
	await t.throws(
		got(s.url, {
			timeout: {socket: reqDelay / 20},
			retry: 0
		}),
		{
			code: 'ESOCKETTIMEDOUT'
		}
	);
});

test.todo('connection timeout');

test('request timeout', async t => {
	await t.throws(
		got(s.url, {
			timeout: {request: reqDelay},
			retry: 0
		}),
		{
			code: 'ESOCKETTIMEDOUT'
		}
	);
});

test('retries on timeout (ESOCKETTIMEDOUT)', async t => {
	let tried = false;

	await t.throws(got(s.url, {
		timeout: reqDelay,
		retry: {
			retries: () => {
				if (tried) {
					return 0;
				}

				tried = true;
				return 1;
			}
		}
	}), {
		code: 'ESOCKETTIMEDOUT'
	});

	t.true(tried);
});

test('retries on timeout (ETIMEDOUT)', async t => {
	let tried = false;

	await t.throws(got(s.url, {
		timeout: 0,
		retry: {
			retries: () => {
				if (tried) {
					return 0;
				}

				tried = true;
				return 1;
			}
		}
	}), {code: 'ETIMEDOUT'});

	t.true(tried);
});

test('timeout with streams', async t => {
	const stream = got.stream(s.url, {
		timeout: 0,
		retry: 0
	});
	await t.throws(pEvent(stream, 'response'), {code: 'ETIMEDOUT'});
});

test('no error emitted when timeout is not breached', async t => {
	const stream = got.stream(s.url, {
		retry: 0,
		timeout: {
			request: reqDelay * 2
		}
	});
	stream.on('error', err => {
		t.fail(`error was emitted: ${err}`);
	});
	await getStream(stream);
	await delay(reqDelay * 3);
	t.pass();
});<commit_msg>Test that timeout error is not erroneously emitted using promise interface (#529)<commit_after>import getStream from 'get-stream';
import test from 'ava';
import pEvent from 'p-event';
import delay from 'delay';
import got from '../source';
import {createServer} from './helpers/server';

let s;
const reqDelay = 160;

test.before('setup', async () => {
	s = await createServer();

	s.on('/', async (req, res) => {
		await delay(reqDelay);
		res.statusCode = 200;
		res.end('OK');
	});

	await s.listen(s.port);
});

test('timeout option (ETIMEDOUT)', async t => {
	await t.throws(
		got(s.url, {
			timeout: 0,
			retry: 0
		}),
		{
			code: 'ETIMEDOUT'
		}
	);
});

test('timeout option (ESOCKETTIMEDOUT)', async t => {
	await t.throws(
		got(s.url, {
			timeout: reqDelay,
			retry: 0
		}),
		{
			code: 'ESOCKETTIMEDOUT'
		}
	);
});

test('timeout option as object (ETIMEDOUT)', async t => {
	await t.throws(
		got(s.url, {
			timeout: {socket: reqDelay * 2.5, request: 0},
			retry: 0
		}),
		{
			code: 'ETIMEDOUT'
		}
	);
});

test('timeout option as object (ESOCKETTIMEDOUT)', async t => {
	await t.throws(
		got(s.url, {
			timeout: {socket: reqDelay * 1.5, request: reqDelay},
			retry: 0
		}),
		{
			code: 'ESOCKETTIMEDOUT'
		}
	);
});

test('socket timeout', async t => {
	await t.throws(
		got(s.url, {
			timeout: {socket: reqDelay / 20},
			retry: 0
		}),
		{
			code: 'ESOCKETTIMEDOUT'
		}
	);
});

test.todo('connection timeout');

test('request timeout', async t => {
	await t.throws(
		got(s.url, {
			timeout: {request: reqDelay},
			retry: 0
		}),
		{
			code: 'ESOCKETTIMEDOUT'
		}
	);
});

test('retries on timeout (ESOCKETTIMEDOUT)', async t => {
	let tried = false;

	await t.throws(got(s.url, {
		timeout: reqDelay,
		retry: {
			retries: () => {
				if (tried) {
					return 0;
				}

				tried = true;
				return 1;
			}
		}
	}), {
		code: 'ESOCKETTIMEDOUT'
	});

	t.true(tried);
});

test('retries on timeout (ETIMEDOUT)', async t => {
	let tried = false;

	await t.throws(got(s.url, {
		timeout: 0,
		retry: {
			retries: () => {
				if (tried) {
					return 0;
				}

				tried = true;
				return 1;
			}
		}
	}), {code: 'ETIMEDOUT'});

	t.true(tried);
});

test('timeout with streams', async t => {
	const stream = got.stream(s.url, {
		timeout: 0,
		retry: 0
	});
	await t.throws(pEvent(stream, 'response'), {code: 'ETIMEDOUT'});
});

test('no error emitted when timeout is not breached (stream)', async t => {
	const stream = got.stream(s.url, {
		retry: 0,
		timeout: {
			request: reqDelay * 2
		}
	});
	stream.on('error', err => {
		t.fail(`error was emitted: ${err}`);
	});
	await getStream(stream);
	await delay(reqDelay * 3);
	t.pass();
});

test('no error emitted when timeout is not breached (promise)', async t => {
	await got(s.url, {
		retry: 0,
		timeout: {
			request: reqDelay * 2
		}
	}).on('request', req => {
		// 'error' events are not emitted by the Promise interface, so attach
		// directly to the request object
		req.on('error', err => {
			t.fail(`error was emitted: ${err}`);
		});
	});
	await delay(reqDelay * 3);
	t.pass();
});
{'fraction_non_alphanumeric': 0.1874231512383629, 'fraction_numerical': 0.011593184612682241, 'mean_word_length': 3.0526690391459073, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2830747', 'n_tokens_mistral': 2530, 'n_tokens_neox': 2244, 'n_words': 547}