From 4fb924ce7481fda8c6bb75842a6b738fee64761b Mon Sep 17 00:00:00 2001 From: ehmicky Date: Fri, 10 May 2019 08:36:48 -0700 Subject: [PATCH] Fix randomly failing CI tests (#231) --- fixtures/delay | 7 +------ fixtures/sleeper | 4 ---- test.js | 54 +++++++++++------------------------------------- 3 files changed, 13 insertions(+), 52 deletions(-) delete mode 100755 fixtures/sleeper diff --git a/fixtures/delay b/fixtures/delay index 103bc5ce9..ed89fd8dd 100755 --- a/fixtures/delay +++ b/fixtures/delay @@ -1,9 +1,4 @@ #!/usr/bin/env node 'use strict'; -setTimeout(() => { - console.log('delay completed'); - process.exit(Number(process.argv[3] || 0)); -}, Number(process.argv[2])); - -console.log('delay started'); +setTimeout(() => {}, Number(process.argv[2])); diff --git a/fixtures/sleeper b/fixtures/sleeper deleted file mode 100755 index ab9343b88..000000000 --- a/fixtures/sleeper +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash - -sleep 5 -echo "ok" diff --git a/test.js b/test.js index c95b31d01..855138ff7 100644 --- a/test.js +++ b/test.js @@ -322,9 +322,7 @@ if (process.platform !== 'win32') { test('error.killed is true if process was killed directly', async t => { const cp = execa('forever'); - setTimeout(() => { - cp.kill(); - }, 100); + cp.kill(); const error = await t.throwsAsync(cp, {message: /was killed with SIGTERM/}); t.true(error.killed); @@ -334,9 +332,7 @@ test('error.killed is true if process was killed directly', async t => { test('error.killed is false if process was killed indirectly', async t => { const cp = execa('forever'); - setTimeout(() => { - process.kill(cp.pid, 'SIGINT'); - }, 100); + process.kill(cp.pid, 'SIGINT'); // `process.kill()` is emulated by Node.js on Windows const message = process.platform === 'win32' ? /failed with exit code 1/ : /was killed with SIGINT/; @@ -352,9 +348,7 @@ if (process.platform === 'darwin') { t.end(); }); - setTimeout(() => { - process.kill(cp.pid, 'SIGINT'); - }, 100); + process.kill(cp.pid, 'SIGINT'); }); } @@ -362,9 +356,7 @@ if (process.platform !== 'win32') { test('error.signal is SIGINT', async t => { const cp = execa('forever'); - setTimeout(() => { - process.kill(cp.pid, 'SIGINT'); - }, 100); + process.kill(cp.pid, 'SIGINT'); const error = await t.throwsAsync(cp, {message: /was killed with SIGINT/}); t.is(error.signal, 'SIGINT'); @@ -373,16 +365,14 @@ if (process.platform !== 'win32') { test('error.signal is SIGTERM', async t => { const cp = execa('forever'); - setTimeout(() => { - process.kill(cp.pid, 'SIGTERM'); - }, 100); + process.kill(cp.pid, 'SIGTERM'); const error = await t.throwsAsync(cp, {message: /was killed with SIGTERM/}); t.is(error.signal, 'SIGTERM'); }); test('custom error.signal', async t => { - const error = await t.throwsAsync(execa('delay', ['3000', '0'], {killSignal: 'SIGHUP', timeout: 1500, message: TIMEOUT_REGEXP})); + const error = await t.throwsAsync(execa('forever', {killSignal: 'SIGHUP', timeout: 1, message: TIMEOUT_REGEXP})); t.is(error.signal, 'SIGHUP'); }); } @@ -405,41 +395,21 @@ test('error.code is 2', code, 2); test('error.code is 3', code, 3); test('error.code is 4', code, 4); -test.serial('timeout will kill the process early', async t => { - const time = Date.now(); - const error = await t.throwsAsync(execa('delay', ['60000', '0'], {timeout: 500, message: TIMEOUT_REGEXP})); - const diff = Date.now() - time; - - t.true(error.timedOut); - t.not(error.exitCode, 22); - t.true(diff < 4000); -}); - -test.serial('timeout will kill the process early (sleep)', async t => { - const time = Date.now(); - const error = await t.throwsAsync(execa('sleeper', [], {timeout: 500, message: TIMEOUT_REGEXP})); - const diff = Date.now() - time; - +test('timeout kills the process if it times out', async t => { + const error = await t.throwsAsync(execa('forever', {timeout: 1, message: TIMEOUT_REGEXP})); t.true(error.timedOut); - t.not(error.stdout, 'ok'); - t.true(diff < 4000); }); -test('timeout will not kill the process early', async t => { - const error = await t.throwsAsync(execa('delay', ['2000', '22'], {timeout: 30000}), {code: 22, message: getExitRegExp('22')}); +test('timeout does not kill the process if it does not time out', async t => { + const error = await execa('delay', ['500'], {timeout: 1e8}); t.false(error.timedOut); }); -test('timedOut will be false if no timeout was set and zero exit code', async t => { - const result = await execa('delay', ['1000', '0']); +test('timedOut is false if no timeout was set', async t => { + const result = await execa('noop'); t.false(result.timedOut); }); -test('timedOut will be false if no timeout was set and non-zero exit code', async t => { - const error = await t.throwsAsync(execa('delay', ['1000', '3']), {message: getExitRegExp('3')}); - t.false(error.timedOut); -}); - async function errorMessage(t, expected, ...args) { await t.throwsAsync(execa('exit', args), {message: expected}); }