forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSchedulerPostTaskOnly-test.js
275 lines (235 loc) · 6.64 KB
/
SchedulerPostTaskOnly-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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
* @jest-environment node
*/
/* eslint-disable no-for-of-loops/no-for-of-loops */
'use strict';
let Scheduler;
let runtime;
let performance;
let cancelCallback;
let scheduleCallback;
let NormalPriority;
describe('SchedulerPostTaskOnly', () => {
beforeEach(() => {
if (!process.env.IS_BUILD) {
jest.resetModules();
// Un-mock scheduler
jest.mock('scheduler', () =>
require.requireActual('scheduler/unstable_post_task_only'),
);
runtime = installMockBrowserRuntime();
performance = window.performance;
Scheduler = require('scheduler');
cancelCallback = Scheduler.unstable_cancelCallback;
scheduleCallback = Scheduler.unstable_scheduleCallback;
NormalPriority = Scheduler.unstable_NormalPriority;
}
});
afterEach(() => {
if (!process.env.IS_BUILD) {
if (!runtime.isLogEmpty()) {
throw Error('Test exited without clearing log.');
}
}
});
function installMockBrowserRuntime() {
let hasPendingTask = false;
let timerIDCounter = 0;
let eventLog = [];
// Mock window functions
const window = {};
global.window = window;
// TODO: Scheduler no longer requires these methods to be polyfilled. But
// maybe we want to continue warning if they don't exist, to preserve the
// option to rely on it in the future?
window.requestAnimationFrame = window.cancelAnimationFrame = () => {};
let currentTime = 0;
window.performance = {
now() {
return currentTime;
},
};
window.setTimeout = (cb, delay) => {
const id = timerIDCounter++;
log(`Set Timer`);
// TODO
return id;
};
window.clearTimeout = id => {
// TODO
};
// Mock browser scheduler.
const scheduler = {};
global.scheduler = scheduler;
let nextTask;
scheduler.postTask = function(callback) {
if (hasPendingTask) {
throw Error('Task already scheduled');
}
log('Post Task');
hasPendingTask = true;
nextTask = callback;
};
function ensureLogIsEmpty() {
if (eventLog.length !== 0) {
throw Error('Log is not empty. Call assertLog before continuing.');
}
}
function advanceTime(ms) {
currentTime += ms;
}
function fireNextTask() {
ensureLogIsEmpty();
if (!hasPendingTask) {
throw Error('No task was scheduled');
}
hasPendingTask = false;
log('Task Event');
// If there's a continuation, it will call postTask again
// which will set nextTask. That means we need to clear
// nextTask before the invocation, otherwise we would
// delete the continuation task.
const task = nextTask;
nextTask = null;
task();
}
function log(val) {
eventLog.push(val);
}
function isLogEmpty() {
return eventLog.length === 0;
}
function assertLog(expected) {
const actual = eventLog;
eventLog = [];
expect(actual).toEqual(expected);
}
return {
advanceTime,
fireNextTask,
log,
isLogEmpty,
assertLog,
};
}
// @gate source
it('task that finishes before deadline', () => {
scheduleCallback(NormalPriority, () => {
runtime.log('Task');
});
runtime.assertLog(['Post Task']);
runtime.fireNextTask();
runtime.assertLog(['Task Event', 'Task']);
});
// @gate source
it('task with continuation', () => {
scheduleCallback(NormalPriority, () => {
runtime.log('Task');
while (!Scheduler.unstable_shouldYield()) {
runtime.advanceTime(1);
}
runtime.log(`Yield at ${performance.now()}ms`);
return () => {
runtime.log('Continuation');
};
});
runtime.assertLog(['Post Task']);
runtime.fireNextTask();
runtime.assertLog(['Task Event', 'Task', 'Yield at 5ms', 'Post Task']);
runtime.fireNextTask();
runtime.assertLog(['Task Event', 'Continuation']);
});
// @gate source
it('multiple tasks', () => {
scheduleCallback(NormalPriority, () => {
runtime.log('A');
});
scheduleCallback(NormalPriority, () => {
runtime.log('B');
});
runtime.assertLog(['Post Task']);
runtime.fireNextTask();
runtime.assertLog(['Task Event', 'A', 'B']);
});
// @gate source
it('multiple tasks with a yield in between', () => {
scheduleCallback(NormalPriority, () => {
runtime.log('A');
runtime.advanceTime(4999);
});
scheduleCallback(NormalPriority, () => {
runtime.log('B');
});
runtime.assertLog(['Post Task']);
runtime.fireNextTask();
runtime.assertLog([
'Task Event',
'A',
// Ran out of time. Post a continuation event.
'Post Task',
]);
runtime.fireNextTask();
runtime.assertLog(['Task Event', 'B']);
});
// @gate source
it('cancels tasks', () => {
const task = scheduleCallback(NormalPriority, () => {
runtime.log('Task');
});
runtime.assertLog(['Post Task']);
cancelCallback(task);
runtime.assertLog([]);
});
// @gate source
it('throws when a task errors then continues in a new event', () => {
scheduleCallback(NormalPriority, () => {
runtime.log('Oops!');
throw Error('Oops!');
});
scheduleCallback(NormalPriority, () => {
runtime.log('Yay');
});
runtime.assertLog(['Post Task']);
expect(() => runtime.fireNextTask()).toThrow('Oops!');
runtime.assertLog(['Task Event', 'Oops!', 'Post Task']);
runtime.fireNextTask();
runtime.assertLog(['Task Event', 'Yay']);
});
// @gate source
it('schedule new task after queue has emptied', () => {
scheduleCallback(NormalPriority, () => {
runtime.log('A');
});
runtime.assertLog(['Post Task']);
runtime.fireNextTask();
runtime.assertLog(['Task Event', 'A']);
scheduleCallback(NormalPriority, () => {
runtime.log('B');
});
runtime.assertLog(['Post Task']);
runtime.fireNextTask();
runtime.assertLog(['Task Event', 'B']);
});
// @gate source
it('schedule new task after a cancellation', () => {
const handle = scheduleCallback(NormalPriority, () => {
runtime.log('A');
});
runtime.assertLog(['Post Task']);
cancelCallback(handle);
runtime.fireNextTask();
runtime.assertLog(['Task Event']);
scheduleCallback(NormalPriority, () => {
runtime.log('B');
});
runtime.assertLog(['Post Task']);
runtime.fireNextTask();
runtime.assertLog(['Task Event', 'B']);
});
});