From 8ec06cfc784260003c8dea33264c6da404e618d9 Mon Sep 17 00:00:00 2001 From: kwonoj Date: Thu, 1 Oct 2015 23:41:40 -0700 Subject: [PATCH] fix(timeout): fix absolute timeout behavior - fix absolute timeout behavior - add, replace test case related absolute timeout to marble test --- spec/operators/timeout-spec.js | 26 ++++++++++++++-- spec/operators/timeoutWith-spec.js | 49 ++++++++++-------------------- src/operators/timeout.ts | 8 ++--- src/operators/timeoutWith.ts | 12 +++----- 4 files changed, 47 insertions(+), 48 deletions(-) diff --git a/spec/operators/timeout-spec.js b/spec/operators/timeout-spec.js index e5ad41dc79..fc825726e5 100644 --- a/spec/operators/timeout-spec.js +++ b/spec/operators/timeout-spec.js @@ -24,7 +24,7 @@ describe('Observable.prototype.timeout()', function () { var e1 = hot('--a--b--c--d--e--|'); var expected = '--a--b--c--d--e--|'; - var timeoutValue = new Date(Date.now() + (expected.length + 2) * 10); + var timeoutValue = new Date(rxTestScheduler.now() + (expected.length + 2) * 10); expectObservable(e1.timeout(timeoutValue, null, rxTestScheduler)).toBe(expected); }); @@ -54,4 +54,26 @@ describe('Observable.prototype.timeout()', function () { expectObservable(e1.timeout(50, value, rxTestScheduler)) .toBe(expected, {a: 'a', b: 'b', c: 'c'}, value); }); -}); \ No newline at end of file + + it('should timeout at a specified Date', function () { + var e1 = Observable.never(); + var expected = '----------#'; + + expectObservable(e1.timeout(new Date(rxTestScheduler.now() + 100), null, rxTestScheduler)).toBe(expected, null, defaultTimeoutError); + }); + + it('should timeout specified Date with default error while source emits', function () { + var e1 = hot('--a--b--c--d--e--|'); + var expected = '--a--b--c-#'; + + expectObservable(e1.timeout(new Date(rxTestScheduler.now() + 100), null, rxTestScheduler)).toBe(expected, {a: 'a', b: 'b', c: 'c'}, defaultTimeoutError); + }); + + it('should timeout specified Date with passed error while source emits', function () { + var value = 'hello'; + var e1 = hot('--a--b--c--d--e--|'); + var expected = '--a--b--c-#'; + + expectObservable(e1.timeout(new Date(rxTestScheduler.now() + 100), value, rxTestScheduler)).toBe(expected, {a: 'a', b: 'b', c: 'c'}, value); + }); +}); diff --git a/spec/operators/timeoutWith-spec.js b/spec/operators/timeoutWith-spec.js index 62c8cc2749..233e4d612a 100644 --- a/spec/operators/timeoutWith-spec.js +++ b/spec/operators/timeoutWith-spec.js @@ -11,22 +11,13 @@ describe('Observable.prototype.timeoutWith()', function () { expectObservable(e1.timeoutWith(50, e2, rxTestScheduler)).toBe(expected); }); - it('should timeout at a specified date then subscribe to the passed observable', function (done) { - var expected = ['x', 'y', 'z']; + it('should timeout at a specified date then subscribe to the passed observable', function () { var e1 = Observable.never(); - var e2 = Observable.fromArray(expected); - - var res = []; - e1.timeoutWith(new Date(Date.now() + 100), e2) - .subscribe(function (x) { - res.push(x); - }, function (x) { - throw 'should not be called'; - }, function () { - expect(res).toEqual(expected); - done(); - }); - }, 2000); + var e2 = cold('--x--y--z--|'); + var expected = '------------x--y--z--|'; + + expectObservable(e1.timeoutWith(new Date(rxTestScheduler.now() + 100), e2, rxTestScheduler)).toBe(expected); + }); it('should timeout after a specified period between emit then subscribe to the passed observable when source emits', function () { var e1 = hot('---a---b------c---|'); @@ -92,21 +83,13 @@ describe('Observable.prototype.timeoutWith()', function () { expectObservable(e1.timeoutWith(50, e2, rxTestScheduler)).toBe(expected); }); - it('should timeout after specified Date then subscribe to the passed observable', function (done) { - var e1 = Observable.interval(40).take(5); - var e2 = Observable.of(100); - - var res = []; - e1.timeoutWith(new Date(Date.now() + 100), e2) - .subscribe(function (x) { - res.push(x); - }, function (x) { - throw 'should not be called'; - }, function () { - expect(res).toEqual([0, 1, 100]); - done(); - }); - }, 2000); + it('should timeout after specified Date then subscribe to the passed observable', function () { + var e1 = hot('--a--b--c--d--e--|'); + var e2 = cold('--z--|'); + var expected = '--a--b---z--|'; + + expectObservable(e1.timeoutWith(new Date(rxTestScheduler.now() + 70), e2, rxTestScheduler)).toBe(expected); + }); it('should not timeout if source completes within specified Date', function () { var e1 = hot('--a--b--c--d--e--|'); @@ -126,11 +109,11 @@ describe('Observable.prototype.timeoutWith()', function () { expectObservable(e1.timeoutWith(new Date(Date.now() + 100), e2, rxTestScheduler)).toBe(expected); }); - it('should timeout specified Date after specified Date then never completes if other source does not complete', function () { + it('should timeout specified Date after specified Date then never completes if other source does not complete', function () { var e1 = hot('---a---b---c---d---e---|'); var e2 = cold('-'); var expected = '---a---b--'; - expectObservable(e1.timeoutWith(new Date(Date.now() + 100), e2, rxTestScheduler)).toBe(expected); + expectObservable(e1.timeoutWith(new Date(rxTestScheduler.now() + 100), e2, rxTestScheduler)).toBe(expected); }); -}); \ No newline at end of file +}); diff --git a/src/operators/timeout.ts b/src/operators/timeout.ts index f31550f4ce..f75bbc5a4c 100644 --- a/src/operators/timeout.ts +++ b/src/operators/timeout.ts @@ -10,8 +10,7 @@ export default function timeout(due: number|Date, errorToSend: any = null, scheduler: Scheduler = immediate) { let absoluteTimeout = isDate(due); - let waitFor = absoluteTimeout ? (+due - Date.now()) : due; - + let waitFor = absoluteTimeout ? (+due - scheduler.now()) : due; return this.lift(new TimeoutOperator(waitFor, absoluteTimeout, errorToSend, scheduler)); } @@ -52,8 +51,7 @@ class TimeoutSubscriber extends Subscriber { private static dispatchTimeout(state: any): void { const source = state.subscriber; const currentIndex = state.index; - - if (!source.completed && source.previousIndex === currentIndex) { + if (!source.hasCompleted && source.previousIndex === currentIndex) { source.notifyTimeout(); } } @@ -86,4 +84,4 @@ class TimeoutSubscriber extends Subscriber { notifyTimeout() { this.error(this.errorToSend || new Error('timeout')); } -} \ No newline at end of file +} diff --git a/src/operators/timeoutWith.ts b/src/operators/timeoutWith.ts index 73dddf9888..80c6429bd3 100644 --- a/src/operators/timeoutWith.ts +++ b/src/operators/timeoutWith.ts @@ -13,11 +13,8 @@ export default function timeoutWith(due: number|Date, withObservable: Observable, scheduler: Scheduler = immediate) { let absoluteTimeout = isDate(due); - let waitFor = absoluteTimeout ? (+due - Date.now()) : due; - - return this.lift( - new TimeoutWithOperator(waitFor, absoluteTimeout, withObservable, scheduler) - ); + let waitFor = absoluteTimeout ? (+due - scheduler.now()) : due; + return this.lift(new TimeoutWithOperator(waitFor, absoluteTimeout, withObservable, scheduler)); } class TimeoutWithOperator implements Operator { @@ -59,8 +56,7 @@ class TimeoutWithSubscriber extends OuterSubscriber { private static dispatchTimeout(state: any): void { const source = state.subscriber; const currentIndex = state.index; - - if (!source.completed && source.previousIndex === currentIndex) { + if (!source.hasCompleted && source.previousIndex === currentIndex) { source.handleTimeout(); } } @@ -101,4 +97,4 @@ class TimeoutWithSubscriber extends OuterSubscriber { this.timedOut = true; this.add(this.timeoutSubscription = subscribeToResult(this, withObservable)); } -} \ No newline at end of file +}