Skip to content

Commit af5aa6e

Browse files
author
jmhofer
committed
Added a unit test. Fixed the implementation. Maybe still a bit naive
when it comes to intervals that are too small to keep up with?
1 parent d00814e commit af5aa6e

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

rxjava-core/src/main/java/rx/operators/OperationInterval.java

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,24 @@
1515
*/
1616
package rx.operators;
1717

18-
import static org.junit.Assert.*;
1918
import static org.mockito.Matchers.*;
2019
import static org.mockito.Mockito.*;
21-
import static rx.operators.Tester.UnitTest.*;
2220

2321
import java.util.concurrent.TimeUnit;
22+
import java.util.concurrent.atomic.AtomicBoolean;
2423

24+
import org.junit.Before;
2525
import org.junit.Test;
26+
import org.mockito.InOrder;
2627

28+
import rx.Observable;
2729
import rx.Observer;
2830
import rx.Scheduler;
2931
import rx.Subscription;
3032
import rx.concurrency.Schedulers;
31-
import rx.util.functions.Func0;
33+
import rx.concurrency.TestScheduler;
34+
import rx.subscriptions.Subscriptions;
35+
import rx.util.functions.Action0;
3236
import rx.util.functions.Func1;
3337

3438
/**
@@ -57,6 +61,7 @@ private static class Interval implements Func1<Observer<Long>, Subscription> {
5761
private final Scheduler scheduler;
5862

5963
private long currentValue;
64+
private final AtomicBoolean complete = new AtomicBoolean();
6065

6166
private Interval(long interval, TimeUnit unit, Scheduler scheduler) {
6267
this.interval = interval;
@@ -66,18 +71,69 @@ private Interval(long interval, TimeUnit unit, Scheduler scheduler) {
6671

6772
@Override
6873
public Subscription call(final Observer<Long> observer) {
69-
return scheduler.schedule(new Func0<Subscription>() {
70-
@Override
71-
public Subscription call() {
74+
scheduler.schedule(new IntervalAction(observer), interval, unit);
75+
return Subscriptions.create(new Action0() {
76+
@Override
77+
public void call() {
78+
complete.set(true);
79+
}
80+
});
81+
}
82+
83+
private class IntervalAction implements Action0 {
84+
private final Observer<Long> observer;
85+
86+
private IntervalAction(Observer<Long> observer) {
87+
this.observer = observer;
88+
}
89+
90+
@Override
91+
public void call() {
92+
if (complete.get()) {
93+
observer.onCompleted();
94+
} else {
7295
observer.onNext(currentValue);
7396
currentValue++;
74-
return Interval.this.call(observer);
97+
scheduler.schedule(this, interval, unit);
7598
}
76-
}, interval, unit);
99+
}
77100
}
78101
}
79102

80103
public static class UnitTest {
81-
// TODO
104+
private TestScheduler scheduler;
105+
private Observer<Long> observer;
106+
107+
@Before
108+
@SuppressWarnings("unchecked") // due to mocking
109+
public void before() {
110+
scheduler = new TestScheduler();
111+
observer = mock(Observer.class);
112+
}
113+
114+
@Test
115+
public void testInterval() {
116+
Observable<Long> w = Observable.create(OperationInterval.interval(1, TimeUnit.SECONDS, scheduler));
117+
Subscription sub = w.subscribe(observer);
118+
119+
verify(observer, never()).onNext(0L);
120+
verify(observer, never()).onCompleted();
121+
verify(observer, never()).onError(any(Exception.class));
122+
123+
scheduler.advanceTimeTo(2, TimeUnit.SECONDS);
124+
125+
InOrder inOrder = inOrder(observer);
126+
inOrder.verify(observer, times(1)).onNext(0L);
127+
inOrder.verify(observer, times(1)).onNext(1L);
128+
inOrder.verify(observer, never()).onNext(2L);
129+
verify(observer, never()).onCompleted();
130+
verify(observer, never()).onError(any(Exception.class));
131+
132+
sub.unsubscribe();
133+
scheduler.advanceTimeTo(4, TimeUnit.SECONDS);
134+
verify(observer, never()).onNext(2L);
135+
verify(observer, times(1)).onCompleted();
136+
verify(observer, never()).onError(any(Exception.class));
137+
}
82138
}
83139
}

rxjava-core/src/test/java/rx/concurrency/TestScheduler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ private TimedAction(long time, Func0<Subscription> action) {
3535
this.time = time;
3636
this.action = action;
3737
}
38+
39+
@Override
40+
public String toString() {
41+
return String.format("TimedAction(time = %d, action = %s)", time, action.toString());
42+
}
3843
}
3944

4045
private static class CompareActionsByTime implements Comparator<TimedAction> {

0 commit comments

Comments
 (0)