Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(VirtualTimeScheduler): rework flush so it won't lose actions #4433

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 55 additions & 8 deletions spec/schedulers/VirtualTimeScheduler-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { expect } from 'chai';
import * as Rx from 'rxjs/Rx';
import { SchedulerAction, Subscription, VirtualAction, VirtualTimeScheduler } from 'rxjs';
import { SchedulerAction, VirtualAction, VirtualTimeScheduler } from 'rxjs';

/** @test {VirtualTimeScheduler} */
describe('VirtualTimeScheduler', () => {
Expand Down Expand Up @@ -67,7 +66,7 @@ describe('VirtualTimeScheduler', () => {
let count = 0;
const expected = [100, 200, 300];

v.schedule<string>(function(this: SchedulerAction<string>, state: string) {
v.schedule<string>(function (this: SchedulerAction<string>, state: string) {
if (++count === 3) {
return;
}
Expand All @@ -82,12 +81,60 @@ describe('VirtualTimeScheduler', () => {

it('should not execute virtual actions that have been rescheduled before flush', () => {
const v = new VirtualTimeScheduler();
let messages: string[] = [];
let action: VirtualAction<string> = <VirtualAction<string>> v.schedule(function(state: string) {
messages.push(state);
}, 10, 'first message');
action = <VirtualAction<string>> action.schedule('second message' , 10);
const messages: string[] = [];

const action: VirtualAction<string> = <VirtualAction<string>> v.schedule(
state => messages.push(state),
10,
'first message'
);

action.schedule('second message', 10);
v.flush();

expect(messages).to.deep.equal(['second message']);
});

it('should execute only those virtual actions that fall into the maxFrames timespan', function () {
const MAX_FRAMES = 50;
const v = new VirtualTimeScheduler(VirtualAction, MAX_FRAMES);
const messages: string[] = ['first message', 'second message', 'third message'];

const actualMessages: string[] = [];

messages.forEach(function (message: string, index: number): void {
baizulin marked this conversation as resolved.
Show resolved Hide resolved
v.schedule(
(state: string) => actualMessages.push(state),
index * MAX_FRAMES,
message
);
});

v.flush();

expect(actualMessages).to.deep.equal(messages.slice(0, 2));
baizulin marked this conversation as resolved.
Show resolved Hide resolved
expect(v.actions.map(a => a.state)).to.deep.equal(messages.slice(2));
});

it('should pick up actions execution where it left off after reaching previous maxFrames limit', function () {
const MAX_FRAMES = 50;
const v = new VirtualTimeScheduler(VirtualAction, MAX_FRAMES);
const messages: string[] = ['first message', 'second message', 'third message'];

const actualMessages: string[] = [];

messages.forEach(function (message: string, index: number): void {
baizulin marked this conversation as resolved.
Show resolved Hide resolved
v.schedule(
state => actualMessages.push(state),
index * MAX_FRAMES,
message
);
});

v.flush();
v.maxFrames = 2 * MAX_FRAMES;
v.flush();

expect(actualMessages).to.deep.equal(messages);
});
});
5 changes: 4 additions & 1 deletion src/internal/scheduler/VirtualTimeScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ export class VirtualTimeScheduler extends AsyncScheduler {
const {actions, maxFrames} = this;
let error: any, action: AsyncAction<any>;

while ((action = actions.shift()) && (this.frame = action.delay) <= maxFrames) {
while ((action = actions[0]) && action.delay <= maxFrames) {
baizulin marked this conversation as resolved.
Show resolved Hide resolved
actions.shift();
this.frame = action.delay;

if (error = action.execute(action.state, action.delay)) {
break;
}
Expand Down