-
Notifications
You must be signed in to change notification settings - Fork 40
/
test.js
123 lines (109 loc) · 3.15 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* 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('wait was too long: ' + diff + ' > ' + epsilon);
} else if (diff < -epsilon) {
assert.fail('wait was too long: ' + diff + ' < ' + -epsilon);
}
}
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 zero', function () {
var sleepTime = 0;
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('works for zero', function () {
var sleepTime = 0;
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);
});
});
});
describe('error', function () {
it('should throw a valid error', function () {
try {
sleep.msleep(Infinity);
}
catch (e) {
assert.equal(e.message, 'Expected number of miliseconds');
}
});
});