/* globals describe, it */
var sleep = require('./');
var assert = require('assert');
var child_process = require('child_process');

function assertApproxEqual(val1, val2) {
  // We require accuracy to the nearest N millisecond.
  // Windows Sleep is not super accurate (depends on scheduling policy) so use a high value.
  var epsilon = 100;
  var diff = val1 - val2;
  if (diff > epsilon) {
    assert.fail(diff, epsilon, 'wait was too long');
  } else if (diff < -epsilon) {
    assert.fail(diff, epsilon, 'wait was too short');
  }
}

describe('sleep', function () {
  it('works for normal input', function () {
    var sleepTime = 1;
    var start = new Date();
    sleep.sleep(sleepTime);
    var end = new Date();
    assertApproxEqual(end - start, sleepTime * 1000);
  });

  it('works for zero', function () {
    var sleepTime = 0;
    var start = new Date();
    sleep.sleep(sleepTime);
    var end = new Date();
    assertApproxEqual(end - start, sleepTime * 1000);
  });

  it('does not allow negative numbers', function () {
    assert.throws(function () {
      sleep.sleep(-1);
    });
  });

  it('works with child_process', function () {
    var sleepTime = 1;
    child_process.exec('echo', function (err, stdout, stderr) { });
    var start = new Date();
    sleep.sleep(sleepTime);
    var end = new Date();
    assertApproxEqual(end - start, sleepTime * 1000);
  });
});

describe('usleep', function () {
  it('works for values smaller than a second', function () {
    var sleepTime = 250;
    var start = new Date();
    sleep.usleep(sleepTime);
    var end = new Date();
    assertApproxEqual(end - start, sleepTime / 1000);
  });

  it('works for values larger than a second', function () {
    this.timeout(4000); // necessary for mocha to not complain
    var sleepTime = 3000000;
    var start = new Date();
    sleep.usleep(sleepTime);
    var end = new Date();
    assertApproxEqual(end - start, sleepTime / 1000);
  });

  it('does not allow negative numbers', function () {
    assert.throws(function () {
      sleep.usleep(-100);
    });
  });
});

describe('msleep', function () {
  it('works for normal input', function() {
    var sleepTime = 1;
    var start = new Date();
    sleep.msleep(sleepTime);
    var end = new Date();
    assertApproxEqual(end - start, sleepTime);
  });

  it('does not allow negative numbers', function () {
    assert.throws(function () {
      sleep.msleep(-100);
    });
  });

  it('does not allow decimal numbers', function () {
    assert.throws(function () {
      sleep.msleep(1.5);
    });
  });
});

@@ -398,39 +398,24 @@
 assert.fail(
-diff, epsilon, 
 'wait was to
@@ -420,17 +420,44 @@
 too long
-'
+: ' + diff + ' %3E ' + epsilon
 );%0A  %7D e
@@ -503,43 +503,55 @@
 ail(
-diff, epsilon, 'wait was too short'
+'wait was too long: ' + diff + ' %3C ' + -epsilon
 );%0A 

Fix deprecated assert.fail usage