forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSchedulerPostTask-test.js
More file actions
301 lines (270 loc) · 7.73 KB
/
SchedulerPostTask-test.js
File metadata and controls
301 lines (270 loc) · 7.73 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**
* 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 ImmediatePriority;
let NormalPriority;
let UserBlockingPriority;
let LowPriority;
let IdlePriority;
// The Scheduler postTask implementation uses a new postTask browser API to
// schedule work on the main thread. This test suite mocks all browser methods
// used in our implementation. It assumes as little as possible about the order
// and timing of events.
describe('SchedulerPostTask', () => {
beforeEach(() => {
jest.resetModules();
// Un-mock scheduler
jest.mock('scheduler', () =>
require.requireActual('scheduler/unstable_post_task'),
);
runtime = installMockBrowserRuntime();
performance = window.performance;
Scheduler = require('scheduler');
cancelCallback = Scheduler.unstable_cancelCallback;
scheduleCallback = Scheduler.unstable_scheduleCallback;
ImmediatePriority = Scheduler.unstable_ImmediatePriority;
UserBlockingPriority = Scheduler.unstable_UserBlockingPriority;
NormalPriority = Scheduler.unstable_NormalPriority;
LowPriority = Scheduler.unstable_LowPriority;
IdlePriority = Scheduler.unstable_IdlePriority;
});
afterEach(() => {
if (!runtime.isLogEmpty()) {
throw Error('Test exited without clearing log.');
}
});
function installMockBrowserRuntime() {
let taskQueue = new Map();
let eventLog = [];
// Mock window functions
const window = {};
global.window = window;
let idCounter = 0;
let currentTime = 0;
window.performance = {
now() {
return currentTime;
},
};
// Note: setTimeout is used to report errors and nothing else.
window.setTimeout = cb => {
try {
cb();
} catch (error) {
runtime.log(`Error: ${error.message}`);
}
};
// Mock browser scheduler.
const scheduler = {};
global.scheduler = scheduler;
scheduler.postTask = function(callback, {priority, signal}) {
const id = idCounter++;
log(
`Post Task ${id} [${priority === undefined ? '<default>' : priority}]`,
);
const controller = signal._controller;
return new Promise((resolve, reject) => {
taskQueue.set(controller, {id, callback, resolve, reject});
});
};
global.TaskController = class TaskController {
constructor() {
this.signal = {_controller: this};
}
abort() {
const task = taskQueue.get(this);
if (task !== undefined) {
taskQueue.delete(this);
const reject = task.reject;
reject(new Error('Aborted'));
}
}
};
function ensureLogIsEmpty() {
if (eventLog.length !== 0) {
throw Error('Log is not empty. Call assertLog before continuing.');
}
}
function advanceTime(ms) {
currentTime += ms;
}
function flushTasks() {
ensureLogIsEmpty();
// 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 prevTaskQueue = taskQueue;
taskQueue = new Map();
for (const [, {id, callback, resolve}] of prevTaskQueue) {
log(`Task ${id} Fired`);
callback(false);
resolve();
}
}
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,
flushTasks,
log,
isLogEmpty,
assertLog,
};
}
it('task that finishes before deadline', () => {
scheduleCallback(NormalPriority, () => {
runtime.log('A');
});
runtime.assertLog(['Post Task 0 [user-visible]']);
runtime.flushTasks();
runtime.assertLog(['Task 0 Fired', 'A']);
});
it('task with continuation', () => {
scheduleCallback(NormalPriority, () => {
runtime.log('A');
while (!Scheduler.unstable_shouldYield()) {
runtime.advanceTime(1);
}
runtime.log(`Yield at ${performance.now()}ms`);
return () => {
runtime.log('Continuation');
};
});
runtime.assertLog(['Post Task 0 [user-visible]']);
runtime.flushTasks();
runtime.assertLog([
'Task 0 Fired',
'A',
'Yield at 5ms',
'Post Task 1 [user-visible]',
]);
runtime.flushTasks();
runtime.assertLog(['Task 1 Fired', 'Continuation']);
});
it('multiple tasks', () => {
scheduleCallback(NormalPriority, () => {
runtime.log('A');
});
scheduleCallback(NormalPriority, () => {
runtime.log('B');
});
runtime.assertLog([
'Post Task 0 [user-visible]',
'Post Task 1 [user-visible]',
]);
runtime.flushTasks();
runtime.assertLog(['Task 0 Fired', 'A', 'Task 1 Fired', 'B']);
});
it('cancels tasks', () => {
const task = scheduleCallback(NormalPriority, () => {
runtime.log('A');
});
runtime.assertLog(['Post Task 0 [user-visible]']);
cancelCallback(task);
runtime.flushTasks();
runtime.assertLog([]);
});
it('an error in one task does not affect execution of other tasks', () => {
scheduleCallback(NormalPriority, () => {
throw Error('Oops!');
});
scheduleCallback(NormalPriority, () => {
runtime.log('Yay');
});
runtime.assertLog([
'Post Task 0 [user-visible]',
'Post Task 1 [user-visible]',
]);
runtime.flushTasks();
runtime.assertLog(['Task 0 Fired', 'Error: Oops!', 'Task 1 Fired', 'Yay']);
});
it('schedule new task after queue has emptied', () => {
scheduleCallback(NormalPriority, () => {
runtime.log('A');
});
runtime.assertLog(['Post Task 0 [user-visible]']);
runtime.flushTasks();
runtime.assertLog(['Task 0 Fired', 'A']);
scheduleCallback(NormalPriority, () => {
runtime.log('B');
});
runtime.assertLog(['Post Task 1 [user-visible]']);
runtime.flushTasks();
runtime.assertLog(['Task 1 Fired', 'B']);
});
it('schedule new task after a cancellation', () => {
const handle = scheduleCallback(NormalPriority, () => {
runtime.log('A');
});
runtime.assertLog(['Post Task 0 [user-visible]']);
cancelCallback(handle);
runtime.flushTasks();
runtime.assertLog([]);
scheduleCallback(NormalPriority, () => {
runtime.log('B');
});
runtime.assertLog(['Post Task 1 [user-visible]']);
runtime.flushTasks();
runtime.assertLog(['Task 1 Fired', 'B']);
});
it('schedules tasks at different priorities', () => {
scheduleCallback(ImmediatePriority, () => {
runtime.log('A');
});
scheduleCallback(UserBlockingPriority, () => {
runtime.log('B');
});
scheduleCallback(NormalPriority, () => {
runtime.log('C');
});
scheduleCallback(LowPriority, () => {
runtime.log('D');
});
scheduleCallback(IdlePriority, () => {
runtime.log('E');
});
runtime.assertLog([
'Post Task 0 [user-blocking]',
'Post Task 1 [user-blocking]',
'Post Task 2 [user-visible]',
'Post Task 3 [user-visible]',
'Post Task 4 [background]',
]);
runtime.flushTasks();
runtime.assertLog([
'Task 0 Fired',
'A',
'Task 1 Fired',
'B',
'Task 2 Fired',
'C',
'Task 3 Fired',
'D',
'Task 4 Fired',
'E',
]);
});
});