From 58cc36225165220ecf687d574e4b1427a50caacd Mon Sep 17 00:00:00 2001 From: Timothy J Fontaine Date: Tue, 13 May 2014 14:49:59 -0700 Subject: [PATCH] test: rewrite spawnsync test The spawnsync test was written wrong, the timeout can never fire before the sync process has returned, the delta is immaterial and times when it was succeeding are not reliable cases. Instead verify that the timeout doesn't fire while the sync process is happening. --- test/simple/test-child-process-spawnsync.js | 24 +++++++-------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/test/simple/test-child-process-spawnsync.js b/test/simple/test-child-process-spawnsync.js index e3304dc99c..a8c1f592bd 100644 --- a/test/simple/test-child-process-spawnsync.js +++ b/test/simple/test-child-process-spawnsync.js @@ -27,30 +27,22 @@ var spawnSync = require('child_process').spawnSync; var TIMER = 100; var SLEEP = 1000; -var start = Date.now(); var timeout = 0; setTimeout(function() { - console.log('timer fired'); - timeout = Date.now(); + timeout = process.hrtime(start); + assert.ok(stop, 'timer should not fire before process exits'); + assert.strictEqual(timeout[0], 1, 'timer should take as long as sleep'); }, TIMER); console.log('sleep started'); +var start = process.hrtime(); var ret = spawnSync('sleep', ['1']); -console.log('sleep exited'); +var stop = process.hrtime(start); +assert.strictEqual(ret.status, 0, 'exit status should be zero'); +console.log('sleep exited', stop); +assert.strictEqual(stop[0], 1, 'sleep should not take longer or less than 1 second'); // Error test when command does not exist var ret_err = spawnSync('command_does_not_exist'); assert.strictEqual(ret_err.error.code, 'ENOENT'); - -process.on('exit', function() { - assert.strictEqual(ret.status, 0); - - var delta = Date.now() - start; - - var expected_timeout = start + TIMER; - var tdlta = timeout - expected_timeout; - - assert(delta > SLEEP); - assert(tdlta > TIMER && tdlta < SLEEP); -});