-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
eventloop.test.js
93 lines (87 loc) · 1.78 KB
/
eventloop.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
import * as eventloop from './eventloop.js'
import * as t from './testing.js'
import * as promise from './promise.js'
/**
* @param {t.TestCase} _tc
*/
export const testEventloopOrder = _tc => {
let currI = 0
for (let i = 0; i < 10; i++) {
const bi = i
eventloop.enqueue(() => {
t.assert(currI++ === bi)
})
}
eventloop.enqueue(() => {
t.assert(currI === 10)
})
t.assert(currI === 0)
return promise.all([
promise.createEmpty(resolve => eventloop.enqueue(resolve)),
promise.until(0, () => currI === 10)
])
}
/**
* @param {t.TestCase} _tc
*/
export const testTimeout = async _tc => {
let set = false
const timeout = eventloop.timeout(0, () => {
set = true
})
timeout.destroy()
await promise.create(resolve => {
eventloop.timeout(10, resolve)
})
t.assert(set === false)
}
/**
* @param {t.TestCase} _tc
*/
export const testInterval = async _tc => {
let set = false
const timeout = eventloop.interval(1, () => {
set = true
})
timeout.destroy()
let i = 0
eventloop.interval(1, () => {
i++
})
await promise.until(0, () => i > 2)
t.assert(set === false)
t.assert(i > 1)
}
/**
* @param {t.TestCase} _tc
*/
export const testAnimationFrame = async _tc => {
let x = false
eventloop.animationFrame(() => { x = true })
await promise.until(0, () => x)
t.assert(x)
}
/**
* @param {t.TestCase} _tc
*/
export const testIdleCallback = async _tc => {
await promise.create(resolve => {
eventloop.idleCallback(resolve)
})
}
/**
* @param {t.TestCase} _tc
*/
export const testDebouncer = async _tc => {
const debounce = eventloop.createDebouncer(10)
let calls = 0
debounce(() => {
calls++
})
debounce(() => {
calls++
})
t.assert(calls === 0)
await promise.wait(20)
t.assert(calls === 1)
}