forked from nuxt/nuxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.test.js
More file actions
165 lines (128 loc) · 4.69 KB
/
timer.test.js
File metadata and controls
165 lines (128 loc) · 4.69 KB
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
162
163
164
165
import { timeout, waitFor, Timer } from '../src/timer'
describe('util: timer', () => {
test('timeout (promise)', async () => {
const result = await timeout(Promise.resolve('time not run out'), 100)
expect(result).toEqual('time not run out')
})
test('timeout (async function)', async () => {
const result = await timeout(async () => {
await waitFor(10)
return 'time not run out'
}, 100)
expect(result).toEqual('time not run out')
})
test('timeout (timeout in 100ms)', async () => {
const call = timeout(waitFor(200), 100, 'timeout test 100ms')
await expect(call).rejects.toThrow('timeout test 100ms')
})
test('timeout (async timeout in 100ms)', async () => {
const call = timeout(async () => {
await waitFor(500)
}, 100, 'timeout test 100ms')
await expect(call).rejects.toThrow('timeout test 100ms')
})
test('waitFor', async () => {
const delay = 100
const s = process.hrtime()
await waitFor(delay)
const t = process.hrtime(s)
// Node.js makes no guarantees about the exact timing of when callbacks will fire
// HTML5 specifies a minimum delay of 4ms for timeouts
// although arbitrary, use this value to determine our lower limit
expect((t[0] * 1e9 + t[1]) / 1e6).not.toBeLessThan(delay - 4)
await waitFor()
})
describe('util: timer Timer', () => {
beforeAll(() => {
// jest.spyOn()
})
test('should construct Timer', () => {
const timer = new Timer()
expect(timer._times).toBeInstanceOf(Map)
})
test('should create new time record', () => {
const timer = new Timer()
timer.hrtime = jest.fn(() => 'hrtime')
const time = timer.start('test', 'test Timer')
expect(timer.hrtime).toBeCalledTimes(1)
expect(time).toEqual({ description: 'test Timer', name: 'test', start: 'hrtime' })
})
test('should stop and remove time record', () => {
const timer = new Timer()
timer.hrtime = jest.fn(() => 'hrtime')
timer.start('test', 'test Timer')
const time = timer.end('test')
expect(timer._times.size).toEqual(0)
expect(timer.hrtime).toBeCalledTimes(2)
expect(timer.hrtime).nthCalledWith(2, 'hrtime')
expect(time).toEqual({ description: 'test Timer', name: 'test', duration: 'hrtime', start: 'hrtime' })
})
test('should be quiet if end with nonexistent time', () => {
const timer = new Timer()
const time = timer.end('test')
expect(time).toBeUndefined()
})
test('should use bigint hrtime if supports', () => {
const timer = new Timer()
const hrtime = process.hrtime
process.hrtime = {
bigint: jest.fn(() => 'bigint hrtime')
}
const time = timer.hrtime()
expect(time).toEqual('bigint hrtime')
expect(process.hrtime.bigint).toBeCalledTimes(1)
process.hrtime = hrtime
})
if (typeof BigInt !== 'undefined') {
test('should calculate duration with bigint hrtime', () => {
const timer = new Timer()
const hrtime = process.hrtime
process.hrtime = {
bigint: jest.fn()
.mockReturnValueOnce(BigInt(100000000))
.mockReturnValueOnce(BigInt(213000000))
}
let time = timer.hrtime()
time = timer.hrtime(time)
expect(time).toEqual(BigInt(113))
expect(process.hrtime.bigint).toBeCalledTimes(2)
process.hrtime = hrtime
})
}
test('should use hrtime if bigint it not supported', () => {
const timer = new Timer()
const hrtime = process.hrtime
process.hrtime = jest.fn(() => 'hrtime')
process.hrtime.bigint = undefined
const time = timer.hrtime()
expect(time).toEqual('hrtime')
expect(process.hrtime).toBeCalledTimes(1)
process.hrtime = hrtime
})
test('should calculate duration with hrtime', () => {
const timer = new Timer()
const hrtime = process.hrtime
process.hrtime = jest.fn()
.mockReturnValueOnce([1, 500000])
.mockReturnValueOnce([2, 600000])
process.hrtime.bigint = undefined
let time = timer.hrtime()
time = timer.hrtime(time)
expect(time).toEqual(2000.6)
expect(process.hrtime).toBeCalledTimes(2)
expect(process.hrtime).nthCalledWith(1)
expect(process.hrtime).nthCalledWith(2, [1, 500000])
process.hrtime = hrtime
})
test('should clear all times', () => {
const timer = new Timer()
timer.hrtime = jest.fn(() => 'hrtime')
timer.start('time-1', 'test time-1')
timer.start('time-2', 'test time-2')
timer.start('time-3', 'test time-3')
expect(timer._times.size).toEqual(3)
timer.clear()
expect(timer._times.size).toEqual(0)
})
})
})