Skip to content

Commit

Permalink
fix(VirtualTimeScheduler): rework flush so it won't lose actions (#4433)
Browse files Browse the repository at this point in the history
* fix(VirtualTimeScheduler): rework flush so it won't lose actions

Previously VirtualTimeScheduler.flush would lose the action that was on a verge of maxFrames limit which rendered testing of observables that are ticking indefinitely impossible. After the fix the user can set maxFrames, flush, make assertions and repeat the process as many times as needed.

* fix(VirtualTimeScheduler): address PR #4433 notes
  • Loading branch information
baizulin authored and benlesh committed Jan 30, 2019
1 parent f28955f commit d068bc9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
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((message, index) => {
v.schedule(
(state: string) => actualMessages.push(state),
index * MAX_FRAMES,
message
);
});

v.flush();

expect(actualMessages).to.deep.equal(['first message', 'second message']);
expect(v.actions.map(a => a.state)).to.deep.equal(['third message']);
});

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((message, index) => {
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) {
actions.shift();
this.frame = action.delay;

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

0 comments on commit d068bc9

Please sign in to comment.