-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
161 lines (141 loc) · 3.52 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* eslint no-console: 0 */
import { timedAsync, ensureDelay, time, sleep } from './index';
const someSlowTask = async () => {
await sleep(550);
return 1;
};
const someSlowTaskException = async () => {
await sleep(550);
throw new Error('expected error');
};
const someQuickTask = async () => {
await sleep(200);
return 1;
};
const someQuickTaskException = async () => {
await sleep(200);
throw new Error('expected error');
};
const someMediumTask = async () => {
await sleep(300);
return 1;
};
const someMediumTaskException = async () => {
await sleep(300);
throw new Error('expected error');
};
async function testTimedAsync(fn, debugLabel, expected) {
const state = {
wasSlow: false,
wasFast: false,
wasError: false,
result: null
};
console.time(debugLabel);
console.timeStamp(debugLabel);
try {
state.result = await timedAsync(fn, {
slowTime: 500,
slow: () => {
console.timeLog(debugLabel);
console.timeStamp(debugLabel);
state.wasSlow = true;
if (!expected.wasSlow) {
throw new Error('Unexpected slow callback');
}
},
fastTime: 250,
fast: () => {
console.timeLog(debugLabel);
console.timeStamp(debugLabel);
state.wasFast = true;
if (!expected.wasFast) {
throw new Error('Unexpected fast callback');
}
}
});
} catch (e) {
if (!expected.wasError) {
console.error('Unexpected exception');
throw e;
}
state.wasError = true;
}
console.timeEnd(debugLabel);
console.timeStamp(debugLabel);
assertDeepEqual(state, expected);
return state;
}
function assertEqual(a, b, msg) {
if (typeof msg === 'undefined') {
msg = `Operation returned wrong result.
Expected: ${b}
Actual: ${a}`;
}
console.assert(a === b, msg);
if (a !== b) console.trace();
}
function assertDeepEqual(a, b) {
for (const key of Object.keys(a)) {
assertEqual(a[key], b[key], `Property ${key} does not match.
Expected: ${b[key]}
Actual: ${a[key]}`);
}
}
async function test () {
await testTimedAsync(someSlowTask, 'slow', {
result: 1,
wasError: false,
wasSlow: true,
wasFast: false,
});
await testTimedAsync(someSlowTaskException, 'slow exception', {
result: null,
wasError: true,
wasSlow: true,
wasFast: false,
});
await testTimedAsync(someQuickTask, 'quick', {
result: 1,
wasError: false,
wasSlow: false,
wasFast: true,
});
// TODO check if changing API is ok here
await testTimedAsync(someQuickTaskException, 'quick exception', {
result: null,
wasError: true,
wasSlow: false,
wasFast: true,
});
await testTimedAsync(someMediumTask, 'medium', {
result: 1,
wasError: false,
wasSlow: false,
wasFast: false,
});
await testTimedAsync(someMediumTaskException, 'medium exception', {
result: null,
wasError: true,
wasSlow: false,
wasFast: false,
});
// Test usage with promise instead of function
await testTimedAsync(someQuickTask(), 'quick', {
result: 1,
wasError: false,
wasSlow: false,
wasFast: true,
});
// Test usage with new API
let slowCallbackCalls = 0;
await ensureDelay(someQuickTask(), 500)
.after(300, () => slowCallbackCalls++)
.after(400, () => slowCallbackCalls++);
assertEqual(slowCallbackCalls, 2);
// Test time() API
const [result, t] = await time(someQuickTask());
assertEqual(result, 1);
console.assert(t >= 200, `time did not return correct time: ${t}`);
}
test();