Skip to content

Commit

Permalink
Merge pull request #3704 from cartant/run-finally
Browse files Browse the repository at this point in the history
 fix(TestScheduler): restore run changes upon error
  • Loading branch information
benlesh authored May 21, 2018
2 parents 294fa2f + 27cb9b6 commit bc49e40
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
21 changes: 21 additions & 0 deletions spec/schedulers/TestScheduler-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from 'chai';
import { hot, cold, expectObservable, expectSubscriptions, time } from '../helpers/marble-testing';
import { AsyncScheduler } from 'rxjs/internal/scheduler/AsyncScheduler';
import { TestScheduler } from 'rxjs/testing';
import { Observable, NEVER, EMPTY, Subject, of, concat, merge, Notification } from 'rxjs';
import { delay, debounceTime, concatMap } from 'rxjs/operators';
Expand Down Expand Up @@ -441,5 +442,25 @@ describe('TestScheduler', () => {
done();
});
});

it('should restore changes upon thrown errors', () => {
const testScheduler = new TestScheduler(assertDeepEquals);

const frameTimeFactor = TestScheduler['frameTimeFactor'];
const maxFrames = testScheduler.maxFrames;
const runMode = testScheduler['runMode'];
const delegate = AsyncScheduler.delegate;

try {
testScheduler.run(() => {
throw new Error('kaboom!');
});
} catch { /* empty */ }

expect(TestScheduler['frameTimeFactor']).to.equal(frameTimeFactor);
expect(testScheduler.maxFrames).to.equal(maxFrames);
expect(testScheduler['runMode']).to.equal(runMode);
expect(AsyncScheduler.delegate).to.equal(delegate);
});
});
});
19 changes: 10 additions & 9 deletions src/internal/testing/TestScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,14 +378,15 @@ export class TestScheduler extends VirtualTimeScheduler {
expectObservable: this.expectObservable.bind(this),
expectSubscriptions: this.expectSubscriptions.bind(this),
};
const ret = callback(helpers);
this.flush();

TestScheduler.frameTimeFactor = prevFrameTimeFactor;
this.maxFrames = prevMaxFrames;
this.runMode = false;
AsyncScheduler.delegate = undefined;

return ret;
try {
const ret = callback(helpers);
this.flush();
return ret;
} finally {
TestScheduler.frameTimeFactor = prevFrameTimeFactor;
this.maxFrames = prevMaxFrames;
this.runMode = false;
AsyncScheduler.delegate = undefined;
}
}
}

0 comments on commit bc49e40

Please sign in to comment.