1
0
Fork 0
spacetac/src/common/Timer.spec.ts

116 lines
2.4 KiB
TypeScript
Raw Normal View History

2019-11-21 22:14:27 +00:00
testing("Timer", test => {
let clock = test.clock();
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
test.case("schedules and cancels future calls", check => {
let timer = new Timer();
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
let called: any[] = [];
let callback = (item: any) => called.push(item);
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
let s1 = timer.schedule(50, () => callback(1));
let s2 = timer.schedule(150, () => callback(2));
let s3 = timer.schedule(250, () => callback(3));
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
check.equals(called, []);
clock.forward(100);
check.equals(called, [1]);
timer.cancel(s1);
check.equals(called, [1]);
clock.forward(100);
check.equals(called, [1, 2]);
timer.cancel(s3);
clock.forward(100);
check.equals(called, [1, 2]);
clock.forward(1000);
check.equals(called, [1, 2]);
});
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
test.case("may cancel all scheduled", check => {
let timer = new Timer();
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
let called: any[] = [];
let callback = (item: any) => called.push(item);
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
timer.schedule(150, () => callback(1));
timer.schedule(50, () => callback(2));
timer.schedule(500, () => callback(3));
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
check.equals(called, []);
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
clock.forward(100);
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
check.equals(called, [2]);
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
clock.forward(100);
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
check.equals(called, [2, 1]);
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
timer.cancelAll();
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
clock.forward(1000);
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
check.equals(called, [2, 1]);
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
timer.schedule(50, () => callback(4));
timer.schedule(150, () => callback(5));
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
clock.forward(100);
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
check.equals(called, [2, 1, 4]);
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
timer.cancelAll(true);
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
clock.forward(100);
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
check.equals(called, [2, 1, 4]);
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
timer.schedule(50, () => callback(6));
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
clock.forward(100);
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
check.equals(called, [2, 1, 4]);
});
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
test.case("may switch to synchronous mode", check => {
let timer = new Timer(true);
let called: any[] = [];
let callback = (item: any) => called.push(item);
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
timer.schedule(50, () => callback(1));
check.equals(called, [1]);
});
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
test.acase("sleeps asynchronously", async check => {
let timer = new Timer();
let x = 1;
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
let promise = timer.sleep(500).then(() => {
x++;
});
check.equals(x, 1);
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
clock.forward(300);
check.equals(x, 1);
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
clock.forward(300);
check.equals(x, 1);
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
await promise;
check.equals(x, 2);
});
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
test.case("gives current time in milliseconds", check => {
check.equals(Timer.nowMs(), 0);
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
clock.forward(5);
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
check.equals(Timer.nowMs(), 5);
let t = Timer.nowMs();
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
clock.forward(10);
2019-05-06 17:14:12 +00:00
2019-11-21 22:14:27 +00:00
check.equals(Timer.nowMs(), 15);
check.equals(Timer.fromMs(t), 10);
});
});