|
15 | 15 | */
|
16 | 16 | package rx.operators;
|
17 | 17 |
|
18 |
| -import static org.junit.Assert.*; |
19 | 18 | import static org.mockito.Matchers.*;
|
20 | 19 | import static org.mockito.Mockito.*;
|
21 |
| -import static rx.operators.Tester.UnitTest.*; |
22 | 20 |
|
23 | 21 | import java.util.concurrent.Executors;
|
24 | 22 | import java.util.concurrent.TimeUnit;
|
25 | 23 | import java.util.concurrent.atomic.AtomicBoolean;
|
26 | 24 | import java.util.concurrent.atomic.AtomicReference;
|
27 | 25 |
|
| 26 | +import org.junit.Before; |
28 | 27 | import org.junit.Test;
|
| 28 | +import org.mockito.InOrder; |
29 | 29 |
|
30 | 30 | import rx.Observable;
|
31 | 31 | import rx.Observer;
|
32 | 32 | import rx.Scheduler;
|
33 | 33 | import rx.Subscription;
|
34 | 34 | import rx.concurrency.Schedulers;
|
| 35 | +import rx.concurrency.TestScheduler; |
35 | 36 | import rx.subscriptions.Subscriptions;
|
36 | 37 | import rx.util.functions.Action0;
|
37 | 38 | import rx.util.functions.Func1;
|
@@ -78,7 +79,7 @@ public Subscription call(final Observer<T> observer) {
|
78 | 79 | public void onCompleted() { /* the clock never completes */ }
|
79 | 80 |
|
80 | 81 | @Override
|
81 |
| - public void onError(Exception e) { /* the clock has no errors */ } |
| 82 | + public void onError(@SuppressWarnings("unused") Exception e) { /* the clock has no errors */ } |
82 | 83 |
|
83 | 84 | @Override
|
84 | 85 | public void onNext(@SuppressWarnings("unused") Long tick) {
|
@@ -119,6 +120,77 @@ public void call() {
|
119 | 120 | }
|
120 | 121 |
|
121 | 122 | public static class UnitTest {
|
122 |
| - // TODO |
| 123 | + private TestScheduler scheduler; |
| 124 | + private Observer<Long> observer; |
| 125 | + |
| 126 | + @Before |
| 127 | + @SuppressWarnings("unchecked") // due to mocking |
| 128 | + public void before() { |
| 129 | + scheduler = new TestScheduler(); |
| 130 | + observer = mock(Observer.class); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void testSample() { |
| 135 | + Observable<Long> source = Observable.create(new Func1<Observer<Long>, Subscription>() { |
| 136 | + @Override |
| 137 | + public Subscription call(final Observer<Long> observer1) { |
| 138 | + scheduler.schedule(new Action0() { |
| 139 | + @Override |
| 140 | + public void call() { |
| 141 | + observer1.onNext(1L); |
| 142 | + } |
| 143 | + }, 1, TimeUnit.SECONDS); |
| 144 | + scheduler.schedule(new Action0() { |
| 145 | + @Override |
| 146 | + public void call() { |
| 147 | + observer1.onNext(2L); |
| 148 | + } |
| 149 | + }, 2, TimeUnit.SECONDS); |
| 150 | + scheduler.schedule(new Action0() { |
| 151 | + @Override |
| 152 | + public void call() { |
| 153 | + observer1.onCompleted(); |
| 154 | + } |
| 155 | + }, 3, TimeUnit.SECONDS); |
| 156 | + |
| 157 | + return Subscriptions.empty(); |
| 158 | + } |
| 159 | + }); |
| 160 | + |
| 161 | + Observable<Long> sampled = Observable.create(OperationSample.sample(source, 400L, TimeUnit.MILLISECONDS, scheduler)); |
| 162 | + sampled.subscribe(observer); |
| 163 | + |
| 164 | + InOrder inOrder = inOrder(observer); |
| 165 | + |
| 166 | + scheduler.advanceTimeTo(800L, TimeUnit.MILLISECONDS); |
| 167 | + verify(observer, never()).onNext(any(Long.class)); |
| 168 | + verify(observer, never()).onCompleted(); |
| 169 | + verify(observer, never()).onError(any(Exception.class)); |
| 170 | + |
| 171 | + scheduler.advanceTimeTo(1200L, TimeUnit.MILLISECONDS); |
| 172 | + inOrder.verify(observer, times(1)).onNext(1L); |
| 173 | + verify(observer, never()).onNext(2L); |
| 174 | + verify(observer, never()).onCompleted(); |
| 175 | + verify(observer, never()).onError(any(Exception.class)); |
| 176 | + |
| 177 | + scheduler.advanceTimeTo(1600L, TimeUnit.MILLISECONDS); |
| 178 | + inOrder.verify(observer, times(1)).onNext(1L); |
| 179 | + verify(observer, never()).onNext(2L); |
| 180 | + verify(observer, never()).onCompleted(); |
| 181 | + verify(observer, never()).onError(any(Exception.class)); |
| 182 | + |
| 183 | + scheduler.advanceTimeTo(2000L, TimeUnit.MILLISECONDS); |
| 184 | + inOrder.verify(observer, never()).onNext(1L); |
| 185 | + inOrder.verify(observer, times(1)).onNext(2L); |
| 186 | + verify(observer, never()).onCompleted(); |
| 187 | + verify(observer, never()).onError(any(Exception.class)); |
| 188 | + |
| 189 | + scheduler.advanceTimeTo(3000L, TimeUnit.MILLISECONDS); |
| 190 | + inOrder.verify(observer, never()).onNext(1L); |
| 191 | + inOrder.verify(observer, times(2)).onNext(2L); |
| 192 | + verify(observer, times(1)).onCompleted(); |
| 193 | + verify(observer, never()).onError(any(Exception.class)); |
| 194 | + } |
123 | 195 | }
|
124 | 196 | }
|
0 commit comments