-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimer.spec.js
102 lines (98 loc) · 3.41 KB
/
timer.spec.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
const Timer = require("@src/timer");
function createPromiseHandler () {
let resolve, reject, promise;
promise = new Promise((_resolve, _reject) => { [resolve, reject] = [_resolve, _reject];});
return {
resolve, reject, promise
};
}
function createProfiler () {
let startTime, stopTime, durationTime;
return {
start (){
startTime = process.hrtime.bigint();
},
stop (){
stopTime = process.hrtime.bigint();
},
duration () {
if(!durationTime) {
durationTime = (stopTime - startTime) / 1000000n;
}
return durationTime;
},
log() {
console.log("time to complete : " + this.duration());
}
};
}
describe("timer", () => {
it("should accept number and callback", () => {
return new Promise(resolve => {
const timer = new Timer(0, (count) => {
expect(count).toEqual(0);
resolve();
});
timer.start();
});
});
it("should receive 0 tick for totalTime 45 millisecond", () => {
const promiseHandler = createPromiseHandler();
const profiler = createProfiler();
let count = 0;
new Timer(45, () => {
profiler.stop();
expect(count).toEqual(0);
profiler.log();
expect(profiler.duration()).toBeLessThan(50);
expect(profiler.duration()).toBeGreaterThanOrEqual(45);
promiseHandler.resolve();
}).on("tick", () => { count += 1;}).start();
profiler.start();
return promiseHandler.promise;
});
it("should receive 1 tick for totalTime 50 millisecond", () => {
const promiseHandler = createPromiseHandler();
const profiler = createProfiler();
let count = 0;
new Timer(50, () => {
profiler.stop();
expect(count).toEqual(1);
profiler.log();
expect(profiler.duration()).toBeLessThan(60);
expect(profiler.duration()).toBeGreaterThanOrEqual(50);
promiseHandler.resolve();
}).on("tick", () => { count += 1;}).start();
profiler.start();
return promiseHandler.promise;
});
it("should receive 10 tick for totalTime 500 millisecond", () => {
const promiseHandler = createPromiseHandler();
const profiler = createProfiler();
let count = 0;
new Timer(500, () => {
profiler.stop();
expect(count).toEqual(10);
profiler.log();
expect(profiler.duration()).toBeLessThan(600);
expect(profiler.duration()).toBeGreaterThanOrEqual(500);
promiseHandler.resolve();
}).on("tick", () => { count += 1;}).start();
profiler.start();
return promiseHandler.promise;
});
it("should receive 10 tick for totalTime 549 millisecond", () => {
const promiseHandler = createPromiseHandler();
const profiler = createProfiler();
let count = 0;
new Timer(549, () => {
profiler.stop();
expect(count).toEqual(10);
expect(profiler.duration()).toBeLessThan(650);
expect(profiler.duration()).toBeGreaterThanOrEqual(500);
promiseHandler.resolve();
}).on("tick", () => { count += 1;}).start();
profiler.start();
return promiseHandler.promise;
});
});