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

Implemented the delay operator #384

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
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
60 changes: 60 additions & 0 deletions rxjava-core/src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
Expand All @@ -35,6 +36,7 @@
import rx.operators.OperationCombineLatest;
import rx.operators.OperationConcat;
import rx.operators.OperationDefer;
import rx.operators.OperationDelay;
import rx.operators.OperationDematerialize;
import rx.operators.OperationDistinctUntilChanged;
import rx.operators.OperationDistinct;
Expand Down Expand Up @@ -3551,6 +3553,64 @@ public Observable<T> scan(Func2<T, T, T> accumulator) {
return create(OperationScan.scan(this, accumulator));
}

/**
* Returns an Observable that emits the results of shifting the items emitted by the source
* Observable by a specified delay. Only errors emitted by the source Observable are not delayed.
* @param delay
* the delay to shift the source by
* @param unit
* the {@link TimeUnit} in which <code>period</code> is defined
* @return the source Observable, but shifted by the specified delay
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229810%28v=vs.103%29.aspx">MSDN: Observable.Delay</a>
*/
public Observable<T> delay(long delay, TimeUnit unit) {
return create(OperationDelay.delay(this, delay, unit));
}

/**
* Returns an Observable that emits the results of shifting the items emitted by the source
* Observable by a specified delay. Only errors emitted by the source Observable are not delayed.
* @param delay
* the delay to shift the source by
* @param unit
* the {@link TimeUnit} in which <code>period</code> is defined
* @param scheduler
* the {@link Scheduler} to use for delaying
* @return the source Observable, but shifted by the specified delay
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229280(v=vs.103).aspx">MSDN: Observable.Delay</a>
*/
public Observable<T> delay(long delay, TimeUnit unit, Scheduler scheduler) {
return create(OperationDelay.delay(this, delay, unit, scheduler));
}

/**
* Returns an Observable that emits the results of shifting the items emitted by the source
* Observable by a delay specified by the due time at which to begin emitting.
* Only errors emitted by the source Observable are not delayed.
* @param dueTime
* the due time at which to start emitting
* @return the source Observable, but shifted by the specified delay
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229677(v=vs.103).aspx">MSDN: Observable.Delay</a>
*/
public Observable<T> delay(Date dueTime) {
return create(OperationDelay.delay(this, dueTime));
}

/**
* Returns an Observable that emits the results of shifting the items emitted by the source
* Observable by a delay specified by the due time at which to begin emitting.
* Only errors emitted by the source Observable are not delayed.
* @param dueTime
* the due time at which to start emitting
* @param scheduler
* the {@link Scheduler} to use for delaying
* @return the source Observable, but shifted by the specified delay
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229250(v=vs.103).aspx">MSDN: Observable.Delay</a>
*/
public Observable<T> delay(Date dueTime, Scheduler scheduler) {
return create(OperationDelay.delay(this, dueTime, scheduler));
}

/**
* Returns an Observable that emits the results of sampling the items emitted by the source
* Observable at a specified time interval.
Expand Down
314 changes: 314 additions & 0 deletions rxjava-core/src/main/java/rx/operators/OperationDelay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,314 @@
/**
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.operators;

import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.initMocks;
import static rx.Observable.interval;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;
import org.mockito.Mock;

import rx.Observable;
import rx.Observable.OnSubscribeFunc;
import rx.Observer;
import rx.Scheduler;
import rx.Subscription;
import rx.concurrency.Schedulers;
import rx.concurrency.TestScheduler;
import rx.util.functions.Action0;
import rx.util.functions.Func1;

/**
* Returns an Observable that emits the results of shifting the items emitted by the source
* Observable by a specified delay.
*/
public final class OperationDelay {

/**
* Delays the observable sequence by the given time interval.
*/
public static <T> OnSubscribeFunc<T> delay(final Observable<? extends T> source, long delay, TimeUnit unit) {
return delay(source, delay, unit, Schedulers.executor(Executors.newSingleThreadScheduledExecutor()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to use Schedulers.threadPoolForComputation() so we reuse the existing ScheduledExecutorService rather than creating a new one for every single call to delay.

}

/**
* Delays the observable sequence by a time interval so that it starts at the given due time.
*/
public static <T> OnSubscribeFunc<T> delay(final Observable<? extends T> source, Date dueTime) {
return delay(source, dueTime, Schedulers.executor(Executors.newSingleThreadScheduledExecutor()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for this default Scheduler.

}

/**
* Delays the observable sequence by a time interval so that it starts at the given due time.
*/
public static <T> OnSubscribeFunc<T> delay(final Observable<? extends T> source, Date dueTime, final Scheduler scheduler) {
long scheduledTime = dueTime.getTime();
long delay = scheduledTime - scheduler.now();
if (delay < 0L) {
delay = 0L;
}
return new Delay<T>(source, delay, TimeUnit.MILLISECONDS, scheduler);
}

/**
* Delays the observable sequence by the given time interval.
*/
public static <T> OnSubscribeFunc<T> delay(final Observable<? extends T> source, final long period, final TimeUnit unit, final Scheduler scheduler) {
return new Delay<T>(source, period, unit, scheduler);
}

private static class Delay<T> implements OnSubscribeFunc<T> {
private final Observable<? extends T> source;
private final long delay;
private final TimeUnit unit;
private final Scheduler scheduler;

private Delay(Observable<? extends T> source, long delay, TimeUnit unit, Scheduler scheduler) {
this.source = source;
this.delay = delay;
this.unit = unit;
this.scheduler = scheduler;
}

@Override
public Subscription onSubscribe(final Observer<? super T> observer) {
return source.subscribe(new Observer<T>() {
private AtomicBoolean errorOccurred = new AtomicBoolean();

@Override
public void onCompleted() {
if (!errorOccurred.get()) {
scheduler.schedule(new Action0() {
@Override
public void call() {
observer.onCompleted();
}
}, delay, unit);
}
}

@Override
public void onError(Throwable e) {
// errors get propagated without delay
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see why we want to return the error immediately, but that should only happen if there are no onNext notifications queued to happen. If there are, the onError needs to wait, not interleave or cause them to be dropped.

errorOccurred.set(true);
observer.onError(e);
}

@Override
public void onNext(final T value) {
if (!errorOccurred.get()) {
scheduler.schedule(new Action0() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be non-deterministic as each onNext is scheduled independently so the thread scheduling can happen in any order, thus this can break the ordering contract.

We went through a similar exercise getting ObserveOn working correctly. I think if this re-uses ScheduledObserver it would simplify this implementation and leverage the same sequential scheduling logic: https://github.com/Netflix/RxJava/blob/master/rxjava-core/src/main/java/rx/operators/ScheduledObserver.java

I think the ScheduledObserver could just take a Scheduler that composes around whatever is passed in and adds the delay time.

That way we get the enqueue behavior: https://github.com/Netflix/RxJava/blob/master/rxjava-core/src/main/java/rx/operators/ScheduledObserver.java#L53

I believe the same behavior for onError needs to exist as well (not immediate delivery) so it happens in the correct order, otherwise onNext events can be missed or attempt to come after onError has been sent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a look at ScheduledObserver.

Concerning delaying onError: Is that really useful? - Rx.NET doesn't seem to delay onError. Here it says:

It is worth noting that Delay will not time-shift OnError notifications. These will be propagated immediately.

I agree, though, that this complicates things.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a delayed scheduler and tried it out toegether with the ScheduledObserver. However, the ScheduledObserver doesn't work well with delays because it waits for each action to end before scheduling a new one.

This means that two events that are 1 second apart will not stay 1 second apart if using a longer delay of 5 seconds, for example.

So we need another solution here, unfortunately.

@Override
public void call() {
try {
observer.onNext(value);
} catch (Throwable t) {
errorOccurred.set(true);
observer.onError(t);
}
}
}, delay, unit);
}
}
});
}
}

public static class UnitTest {
@Mock
private Observer<Long> observer;
@Mock
private Observer<Long> observer2;

private TestScheduler scheduler;

@Before
public void before() {
initMocks(this);
scheduler = new TestScheduler();
}

@Test
public void testDelay() {
Observable<Long> source = interval(1L, TimeUnit.SECONDS, scheduler).take(3);
Observable<Long> delayed = Observable.create(OperationDelay.delay(source, 500L, TimeUnit.MILLISECONDS, scheduler));
delayed.subscribe(observer);

InOrder inOrder = inOrder(observer);
scheduler.advanceTimeTo(1499L, TimeUnit.MILLISECONDS);
verify(observer, never()).onNext(anyLong());
verify(observer, never()).onCompleted();
verify(observer, never()).onError(any(Throwable.class));

scheduler.advanceTimeTo(1500L, TimeUnit.MILLISECONDS);
inOrder.verify(observer, times(1)).onNext(0L);
inOrder.verify(observer, never()).onNext(anyLong());
verify(observer, never()).onCompleted();
verify(observer, never()).onError(any(Throwable.class));

scheduler.advanceTimeTo(2400L, TimeUnit.MILLISECONDS);
inOrder.verify(observer, never()).onNext(anyLong());
verify(observer, never()).onCompleted();
verify(observer, never()).onError(any(Throwable.class));

scheduler.advanceTimeTo(2500L, TimeUnit.MILLISECONDS);
inOrder.verify(observer, times(1)).onNext(1L);
inOrder.verify(observer, never()).onNext(anyLong());
verify(observer, never()).onCompleted();
verify(observer, never()).onError(any(Throwable.class));

scheduler.advanceTimeTo(3400L, TimeUnit.MILLISECONDS);
inOrder.verify(observer, never()).onNext(anyLong());
verify(observer, never()).onCompleted();
verify(observer, never()).onError(any(Throwable.class));

scheduler.advanceTimeTo(3500L, TimeUnit.MILLISECONDS);
inOrder.verify(observer, times(1)).onNext(2L);
verify(observer, times(1)).onCompleted();
verify(observer, never()).onError(any(Throwable.class));
}

@Test
public void testDelayWithDueTime() {
Observable<Long> source = interval(1L, TimeUnit.SECONDS, scheduler).first();
Observable<Long> delayed = Observable.create(OperationDelay.delay(source, new Date(1500L), scheduler));
delayed.subscribe(observer);

InOrder inOrder = inOrder(observer);

scheduler.advanceTimeTo(2499L, TimeUnit.MILLISECONDS);
verify(observer, never()).onNext(anyLong());
verify(observer, never()).onCompleted();

scheduler.advanceTimeTo(2500L, TimeUnit.MILLISECONDS);
inOrder.verify(observer, times(1)).onNext(0L);
inOrder.verify(observer, times(1)).onCompleted();

verify(observer, never()).onError(any(Throwable.class));
}

@Test
public void testLongDelay() {
Observable<Long> source = interval(1L, TimeUnit.SECONDS, scheduler).take(3);
Observable<Long> delayed = Observable.create(OperationDelay.delay(source, 5L, TimeUnit.SECONDS, scheduler));
delayed.subscribe(observer);

InOrder inOrder = inOrder(observer);

scheduler.advanceTimeTo(5999L, TimeUnit.MILLISECONDS);
verify(observer, never()).onNext(anyLong());
verify(observer, never()).onCompleted();
verify(observer, never()).onError(any(Throwable.class));

scheduler.advanceTimeTo(6000L, TimeUnit.MILLISECONDS);
inOrder.verify(observer, times(1)).onNext(0L);
scheduler.advanceTimeTo(6999L, TimeUnit.MILLISECONDS);
inOrder.verify(observer, never()).onNext(anyLong());
scheduler.advanceTimeTo(7000L, TimeUnit.MILLISECONDS);
inOrder.verify(observer, times(1)).onNext(1L);
scheduler.advanceTimeTo(7999L, TimeUnit.MILLISECONDS);
inOrder.verify(observer, never()).onNext(anyLong());
scheduler.advanceTimeTo(8000L, TimeUnit.MILLISECONDS);
inOrder.verify(observer, times(1)).onNext(2L);
inOrder.verify(observer, times(1)).onCompleted();
inOrder.verify(observer, never()).onNext(anyLong());
inOrder.verify(observer, never()).onCompleted();
verify(observer, never()).onError(any(Throwable.class));
}

@Test
public void testDelayWithError() {
Observable<Long> source = interval(1L, TimeUnit.SECONDS, scheduler).map(new Func1<Long, Long>() {
@Override
public Long call(Long value) {
if (value == 1L) {
throw new RuntimeException("error!");
}
return value;
}
});
Observable<Long> delayed = Observable.create(OperationDelay.delay(source, 1L, TimeUnit.SECONDS, scheduler));
delayed.subscribe(observer);

InOrder inOrder = inOrder(observer);

scheduler.advanceTimeTo(1999L, TimeUnit.MILLISECONDS);
verify(observer, never()).onNext(anyLong());
verify(observer, never()).onCompleted();
verify(observer, never()).onError(any(Throwable.class));

scheduler.advanceTimeTo(2000L, TimeUnit.MILLISECONDS);
inOrder.verify(observer, times(1)).onError(any(Throwable.class));
inOrder.verify(observer, never()).onNext(anyLong());
verify(observer, never()).onCompleted();

scheduler.advanceTimeTo(5000L, TimeUnit.MILLISECONDS);
inOrder.verify(observer, never()).onNext(anyLong());
inOrder.verify(observer, never()).onError(any(Throwable.class));
verify(observer, never()).onCompleted();
}

@Test
public void testDelayWithMultipleSubscriptions() {
Observable<Long> source = interval(1L, TimeUnit.SECONDS, scheduler).take(3);
Observable<Long> delayed = Observable.create(OperationDelay.delay(source, 500L, TimeUnit.MILLISECONDS, scheduler));
delayed.subscribe(observer);
delayed.subscribe(observer2);

InOrder inOrder = inOrder(observer);
InOrder inOrder2 = inOrder(observer2);

scheduler.advanceTimeTo(1499L, TimeUnit.MILLISECONDS);
verify(observer, never()).onNext(anyLong());
verify(observer2, never()).onNext(anyLong());

scheduler.advanceTimeTo(1500L, TimeUnit.MILLISECONDS);
inOrder.verify(observer, times(1)).onNext(0L);
inOrder2.verify(observer2, times(1)).onNext(0L);

scheduler.advanceTimeTo(2499L, TimeUnit.MILLISECONDS);
inOrder.verify(observer, never()).onNext(anyLong());
inOrder2.verify(observer2, never()).onNext(anyLong());

scheduler.advanceTimeTo(2500L, TimeUnit.MILLISECONDS);
inOrder.verify(observer, times(1)).onNext(1L);
inOrder2.verify(observer2, times(1)).onNext(1L);

verify(observer, never()).onCompleted();
verify(observer2, never()).onCompleted();

scheduler.advanceTimeTo(3500L, TimeUnit.MILLISECONDS);
inOrder.verify(observer, times(1)).onNext(2L);
inOrder2.verify(observer2, times(1)).onNext(2L);
inOrder.verify(observer, never()).onNext(anyLong());
inOrder2.verify(observer2, never()).onNext(anyLong());
inOrder.verify(observer, times(1)).onCompleted();
inOrder2.verify(observer2, times(1)).onCompleted();

verify(observer, never()).onError(any(Throwable.class));
verify(observer2, never()).onError(any(Throwable.class));
}
}
}
Loading