Skip to content

Commit

Permalink
[scheduler] Improve naive fallback version used in non-DOM environments
Browse files Browse the repository at this point in the history
Added some tests for the non-DOM version of Scheduler that is used
as a fallback, e.g. Jest. The tests use Jest's fake timers API:

- `jest.runAllTimers(ms)` flushes all scheduled work, as expected
- `jest.advanceTimersByTime(ms)` flushes only callbacks that expire
within the given milliseconds.

These capabilities should be sufficient for most product tests. Because
jest's fake timers do not override performance.now or Date.now, we
assume time is constant. This means Scheduler's internal time will not
be aligned with other code that reads from `performance.now`. For finer
control, the user can override `window._sched` like we do in our tests.
We will likely publish a Jest package that has this built in.
  • Loading branch information
acdlite committed Sep 27, 2018
1 parent 469005d commit 3d90277
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 7 deletions.
39 changes: 32 additions & 7 deletions packages/scheduler/src/Scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,17 +474,42 @@ if (
typeof window === 'undefined' ||
typeof window.addEventListener !== 'function'
) {
// If this accidentally gets imported in a non-browser environment, fallback
// to a naive implementation.
var timeoutID = -1;
requestHostCallback = function(callback, absoluteTimeout) {
timeoutID = setTimeout(callback, 0, true);
// If Scheduler runs in a non-DOM environment, it falls back to a naive
// implementation using setTimeout. This only meant to be used for testing
// purposes, like with jest's fake timer API.
var _callback = null;
var _currentTime = -1;
function flushCallback(didTimeout, ms) {
if (_callback !== null) {
var cb = _callback;
_callback = null;
try {
_currentTime = ms;
cb(didTimeout);
} finally {
_currentTime = -1;
}
}
}
requestHostCallback = function(cb, ms) {
if (_currentTime !== -1) {
// Protect against re-entrancy. Jest's fake timer queue flushes timer
// events synchronously.
setTimeout(requestHostCallback, 0, cb, ms);
} else {
_callback = cb;
setTimeout(flushCallback, ms, true, ms);
setTimeout(flushCallback, maxSigned31BitInt, false, maxSigned31BitInt);
}
};
cancelHostCallback = function() {
clearTimeout(timeoutID);
_callback = null;
};
getFrameDeadline = function() {
return 0;
return Infinity;
};
getCurrentTime = function() {
return _currentTime === -1 ? 0 : _currentTime;
};
} else if (window._schedMock) {
// Dynamic injection, only for testing purposes.
Expand Down
145 changes: 145 additions & 0 deletions packages/scheduler/src/__tests__/SchedulerNoDOM-test.internal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* 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
*/

'use strict';

let scheduleCallback;
let runWithPriority;
let ImmediatePriority;
let InteractivePriority;

describe('SchedulerNoDOM', () => {
// If Scheduler runs in a non-DOM environment, it falls back to a naive
// implementation using setTimeout. This only meant to be used for testing
// purposes, like with jest's fake timer API.
beforeEach(() => {
jest.useFakeTimers();
jest.resetModules();
// Delete addEventListener to force us into the fallback mode.
window.addEventListener = undefined;
const Scheduler = require('scheduler');
scheduleCallback = Scheduler.unstable_scheduleCallback;
runWithPriority = Scheduler.unstable_runWithPriority;
ImmediatePriority = Scheduler.unstable_ImmediatePriority;
InteractivePriority = Scheduler.unstable_InteractivePriority;
});

it('runAllTimers only flushes some callbacks', () => {
let log = [];
scheduleCallback(() => {
log.push('A');
});
scheduleCallback(() => {
log.push('B');
});
scheduleCallback(() => {
log.push('C');
});
expect(log).toEqual([]);
jest.runAllTimers();
expect(log).toEqual(['A', 'B', 'C']);
});

it('executes callbacks in order of priority', () => {
let log = [];

scheduleCallback(() => {
log.push('A');
});
scheduleCallback(() => {
log.push('B');
});
runWithPriority(InteractivePriority, () => {
scheduleCallback(() => {
log.push('C');
});
scheduleCallback(() => {
log.push('D');
});
});

expect(log).toEqual([]);
jest.runAllTimers();
expect(log).toEqual(['C', 'D', 'A', 'B']);
});

it('advanceTimersByTime expires callbacks incrementally', () => {
let log = [];

scheduleCallback(() => {
log.push('A');
});
scheduleCallback(() => {
log.push('B');
});
runWithPriority(InteractivePriority, () => {
scheduleCallback(() => {
log.push('C');
});
scheduleCallback(() => {
log.push('D');
});
});

expect(log).toEqual([]);
jest.advanceTimersByTime(249);
expect(log).toEqual([]);
jest.advanceTimersByTime(1);
expect(log).toEqual(['C', 'D']);

log = [];

jest.runAllTimers();
expect(log).toEqual(['A', 'B']);
});

it('calls immediate callbacks immediately', () => {
let log = [];

runWithPriority(ImmediatePriority, () => {
scheduleCallback(() => {
log.push('A');
scheduleCallback(() => {
log.push('B');
});
});
});

expect(log).toEqual(['A', 'B']);
});

it('handles errors', () => {
let log = [];

expect(() => {
runWithPriority(ImmediatePriority, () => {
scheduleCallback(() => {
log.push('A');
throw new Error('Oops A');
});
scheduleCallback(() => {
log.push('B');
});
scheduleCallback(() => {
log.push('C');
throw new Error('Oops C');
});
});
}).toThrow('Oops A');

expect(log).toEqual(['A']);

log = [];

// B and C flush in a subsequent event. That way, the second error is not
// swallowed.
expect(() => jest.runAllTimers()).toThrow('Oops C');
expect(log).toEqual(['B', 'C']);
});
});

0 comments on commit 3d90277

Please sign in to comment.