Skip to content

Commit

Permalink
fix(timeout): fix absolute timeout behavior
Browse files Browse the repository at this point in the history
- fix absolute timeout behavior
- add, replace test case related absolute timeout to marble test
  • Loading branch information
kwonoj authored and benlesh committed Oct 6, 2015
1 parent 164fa4b commit 8ec06cf
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 48 deletions.
26 changes: 24 additions & 2 deletions spec/operators/timeout-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down Expand Up @@ -54,4 +54,26 @@ describe('Observable.prototype.timeout()', function () {
expectObservable(e1.timeout(50, value, rxTestScheduler))
.toBe(expected, {a: 'a', b: 'b', c: 'c'}, value);
});
});

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);
});
});
49 changes: 16 additions & 33 deletions spec/operators/timeoutWith-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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---|');
Expand Down Expand Up @@ -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--|');
Expand All @@ -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);
});
});
});
8 changes: 3 additions & 5 deletions src/operators/timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()) : <number>due;

let waitFor = absoluteTimeout ? (+due - scheduler.now()) : <number>due;
return this.lift(new TimeoutOperator(waitFor, absoluteTimeout, errorToSend, scheduler));
}

Expand Down Expand Up @@ -52,8 +51,7 @@ class TimeoutSubscriber<T> extends Subscriber<T> {
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();
}
}
Expand Down Expand Up @@ -86,4 +84,4 @@ class TimeoutSubscriber<T> extends Subscriber<T> {
notifyTimeout() {
this.error(this.errorToSend || new Error('timeout'));
}
}
}
12 changes: 4 additions & 8 deletions src/operators/timeoutWith.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ export default function timeoutWith(due: number|Date,
withObservable: Observable<any>,
scheduler: Scheduler = immediate) {
let absoluteTimeout = isDate(due);
let waitFor = absoluteTimeout ? (+due - Date.now()) : <number>due;

return this.lift(
new TimeoutWithOperator(waitFor, absoluteTimeout, withObservable, scheduler)
);
let waitFor = absoluteTimeout ? (+due - scheduler.now()) : <number>due;
return this.lift(new TimeoutWithOperator(waitFor, absoluteTimeout, withObservable, scheduler));
}

class TimeoutWithOperator<T, R> implements Operator<T, R> {
Expand Down Expand Up @@ -59,8 +56,7 @@ class TimeoutWithSubscriber<T, R> extends OuterSubscriber<T, R> {
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();
}
}
Expand Down Expand Up @@ -101,4 +97,4 @@ class TimeoutWithSubscriber<T, R> extends OuterSubscriber<T, R> {
this.timedOut = true;
this.add(this.timeoutSubscription = subscribeToResult(this, withObservable));
}
}
}

0 comments on commit 8ec06cf

Please sign in to comment.