-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #609 from akarnokd/OperationTimer3
Operation Timer 3.0
- Loading branch information
Showing
4 changed files
with
176 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 64 additions & 52 deletions
116
rxjava-core/src/main/java/rx/operators/OperationTimer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,86 @@ | ||
/** | ||
* 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. | ||
*/ | ||
/** | ||
* 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 java.util.concurrent.TimeUnit; | ||
|
||
import rx.Observable.OnSubscribeFunc; | ||
import rx.Observer; | ||
import rx.Scheduler; | ||
import rx.Subscription; | ||
import rx.schedulers.Schedulers; | ||
import rx.subscriptions.Subscriptions; | ||
import rx.util.functions.Action0; | ||
|
||
/** | ||
* Operation Timer with several overloads. | ||
* | ||
* @see <a href='http://msdn.microsoft.com/en-us/library/system.reactive.linq.observable.timer.aspx'>MSDN Observable.Timer</a> | ||
*/ | ||
public final class OperationTimer { | ||
|
||
public static OnSubscribeFunc<Void> timer(long interval, TimeUnit unit) { | ||
return timer(interval, unit, Schedulers.threadPoolForComputation()); | ||
} | ||
|
||
public static OnSubscribeFunc<Void> timer(final long delay, final TimeUnit unit, final Scheduler scheduler) { | ||
return new OnSubscribeFunc<Void>() { | ||
@Override | ||
public Subscription onSubscribe(Observer<? super Void> observer) { | ||
return new Timer(delay, unit, scheduler, observer).start(); | ||
} | ||
}; | ||
} | ||
|
||
private static class Timer { | ||
private final long period; | ||
private final TimeUnit unit; | ||
private OperationTimer() { throw new IllegalStateException("No instances!"); } | ||
|
||
/** | ||
* Emit a single 0L after the specified time elapses. | ||
*/ | ||
public static class TimerOnce implements OnSubscribeFunc<Long> { | ||
private final Scheduler scheduler; | ||
private final Observer<? super Void> observer; | ||
|
||
private Timer(long period, TimeUnit unit, Scheduler scheduler, Observer<? super Void> observer) { | ||
this.period = period; | ||
this.unit = unit; | ||
private final long dueTime; | ||
private final TimeUnit dueUnit; | ||
public TimerOnce(long dueTime, TimeUnit unit, Scheduler scheduler) { | ||
this.scheduler = scheduler; | ||
this.observer = observer; | ||
this.dueTime = dueTime; | ||
this.dueUnit = unit; | ||
} | ||
|
||
public Subscription start() { | ||
final Subscription s = scheduler.schedule(new Action0() { | ||
|
||
@Override | ||
public Subscription onSubscribe(final Observer<? super Long> t1) { | ||
return scheduler.schedule(new Action0() { | ||
@Override | ||
public void call() { | ||
observer.onNext(null); | ||
observer.onCompleted(); | ||
t1.onNext(0L); | ||
t1.onCompleted(); | ||
} | ||
}, period, unit); | ||
|
||
return Subscriptions.create(new Action0() { | ||
|
||
}, dueTime, dueUnit); | ||
} | ||
} | ||
/** | ||
* Emit 0L after the initial period and ever increasing number after each period. | ||
*/ | ||
public static class TimerPeriodically implements OnSubscribeFunc<Long> { | ||
private final Scheduler scheduler; | ||
private final long initialDelay; | ||
private final long period; | ||
private final TimeUnit unit; | ||
public TimerPeriodically(long initialDelay, long period, TimeUnit unit, Scheduler scheduler) { | ||
this.scheduler = scheduler; | ||
this.initialDelay = initialDelay; | ||
this.period = period; | ||
this.unit = unit; | ||
} | ||
|
||
@Override | ||
public Subscription onSubscribe(final Observer<? super Long> t1) { | ||
return scheduler.schedulePeriodically(new Action0() { | ||
long count; | ||
@Override | ||
public void call() { | ||
s.unsubscribe(); | ||
t1.onNext(count++); | ||
} | ||
}); | ||
}, | ||
initialDelay, period, unit | ||
); | ||
} | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
rxjava-core/src/test/java/rx/operators/OperationTimerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* 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 java.util.concurrent.TimeUnit; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.InOrder; | ||
import org.mockito.Mock; | ||
import static org.mockito.Mockito.*; | ||
import org.mockito.MockitoAnnotations; | ||
import rx.Observable; | ||
import rx.Observer; | ||
import rx.Subscription; | ||
import rx.schedulers.TestScheduler; | ||
|
||
public class OperationTimerTest { | ||
@Mock | ||
Observer<Object> observer; | ||
TestScheduler s; | ||
@Before | ||
public void before() { | ||
MockitoAnnotations.initMocks(this); | ||
s = new TestScheduler(); | ||
} | ||
@Test | ||
public void testTimerOnce() { | ||
Observable.timer(100, TimeUnit.MILLISECONDS, s).subscribe(observer); | ||
s.advanceTimeBy(100, TimeUnit.MILLISECONDS); | ||
|
||
verify(observer, times(1)).onNext(0L); | ||
verify(observer, times(1)).onCompleted(); | ||
verify(observer, never()).onError(any(Throwable.class)); | ||
} | ||
@Test | ||
public void testTimerPeriodically() { | ||
Subscription c = Observable.timer(100, 100, TimeUnit.MILLISECONDS, s).subscribe(observer); | ||
s.advanceTimeBy(100, TimeUnit.MILLISECONDS); | ||
|
||
InOrder inOrder = inOrder(observer); | ||
inOrder.verify(observer, times(1)).onNext(0L); | ||
|
||
s.advanceTimeBy(100, TimeUnit.MILLISECONDS); | ||
inOrder.verify(observer, times(1)).onNext(1L); | ||
|
||
s.advanceTimeBy(100, TimeUnit.MILLISECONDS); | ||
inOrder.verify(observer, times(1)).onNext(2L); | ||
|
||
s.advanceTimeBy(100, TimeUnit.MILLISECONDS); | ||
inOrder.verify(observer, times(1)).onNext(3L); | ||
|
||
c.unsubscribe(); | ||
s.advanceTimeBy(100, TimeUnit.MILLISECONDS); | ||
inOrder.verify(observer, never()).onNext(any()); | ||
|
||
verify(observer, never()).onCompleted(); | ||
verify(observer, never()).onError(any(Throwable.class)); | ||
} | ||
} |