|
15 | 15 | */
|
16 | 16 | package rx.internal.operators;
|
17 | 17 |
|
18 |
| -import static org.mockito.Mockito.mock; |
19 |
| -import static org.mockito.Mockito.times; |
20 |
| -import static org.mockito.Mockito.verify; |
| 18 | +import static org.junit.Assert.assertFalse; |
| 19 | +import static org.junit.Assert.assertTrue; |
| 20 | +import static org.mockito.Mockito.*; |
| 21 | + |
| 22 | +import java.util.Arrays; |
21 | 23 |
|
22 | 24 | import org.junit.Test;
|
23 | 25 |
|
24 | 26 | import rx.Observable;
|
25 | 27 | import rx.Observer;
|
26 | 28 | import rx.Subscriber;
|
27 | 29 | import rx.Subscription;
|
| 30 | +import rx.observers.TestSubscriber; |
| 31 | +import rx.subjects.PublishSubject; |
28 | 32 |
|
29 | 33 | public class OperatorTakeUntilTest {
|
30 | 34 |
|
@@ -188,4 +192,98 @@ public void call(Subscriber<? super String> observer) {
|
188 | 192 | observer.add(s);
|
189 | 193 | }
|
190 | 194 | }
|
| 195 | + |
| 196 | + @Test |
| 197 | + public void testUntilFires() { |
| 198 | + PublishSubject<Integer> source = PublishSubject.create(); |
| 199 | + PublishSubject<Integer> until = PublishSubject.create(); |
| 200 | + |
| 201 | + TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); |
| 202 | + |
| 203 | + source.takeUntil(until).unsafeSubscribe(ts); |
| 204 | + |
| 205 | + assertTrue(source.hasObservers()); |
| 206 | + assertTrue(until.hasObservers()); |
| 207 | + |
| 208 | + source.onNext(1); |
| 209 | + |
| 210 | + ts.assertReceivedOnNext(Arrays.asList(1)); |
| 211 | + until.onNext(1); |
| 212 | + |
| 213 | + ts.assertReceivedOnNext(Arrays.asList(1)); |
| 214 | + ts.assertNoErrors(); |
| 215 | + ts.assertTerminalEvent(); |
| 216 | + |
| 217 | + assertFalse("Source still has observers", source.hasObservers()); |
| 218 | + assertFalse("Until still has observers", until.hasObservers()); |
| 219 | + assertFalse("TestSubscriber is unsubscribed", ts.isUnsubscribed()); |
| 220 | + } |
| 221 | + @Test |
| 222 | + public void testMainCompletes() { |
| 223 | + PublishSubject<Integer> source = PublishSubject.create(); |
| 224 | + PublishSubject<Integer> until = PublishSubject.create(); |
| 225 | + |
| 226 | + TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); |
| 227 | + |
| 228 | + source.takeUntil(until).unsafeSubscribe(ts); |
| 229 | + |
| 230 | + assertTrue(source.hasObservers()); |
| 231 | + assertTrue(until.hasObservers()); |
| 232 | + |
| 233 | + source.onNext(1); |
| 234 | + source.onCompleted(); |
| 235 | + |
| 236 | + ts.assertReceivedOnNext(Arrays.asList(1)); |
| 237 | + ts.assertNoErrors(); |
| 238 | + ts.assertTerminalEvent(); |
| 239 | + |
| 240 | + assertFalse("Source still has observers", source.hasObservers()); |
| 241 | + assertFalse("Until still has observers", until.hasObservers()); |
| 242 | + assertFalse("TestSubscriber is unsubscribed", ts.isUnsubscribed()); |
| 243 | + } |
| 244 | + @Test |
| 245 | + public void testDownstreamUnsubscribes() { |
| 246 | + PublishSubject<Integer> source = PublishSubject.create(); |
| 247 | + PublishSubject<Integer> until = PublishSubject.create(); |
| 248 | + |
| 249 | + TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); |
| 250 | + |
| 251 | + source.takeUntil(until).take(1).unsafeSubscribe(ts); |
| 252 | + |
| 253 | + assertTrue(source.hasObservers()); |
| 254 | + assertTrue(until.hasObservers()); |
| 255 | + |
| 256 | + source.onNext(1); |
| 257 | + |
| 258 | + ts.assertReceivedOnNext(Arrays.asList(1)); |
| 259 | + ts.assertNoErrors(); |
| 260 | + ts.assertTerminalEvent(); |
| 261 | + |
| 262 | + assertFalse("Source still has observers", source.hasObservers()); |
| 263 | + assertFalse("Until still has observers", until.hasObservers()); |
| 264 | + assertFalse("TestSubscriber is unsubscribed", ts.isUnsubscribed()); |
| 265 | + } |
| 266 | + public void testBackpressure() { |
| 267 | + PublishSubject<Integer> until = PublishSubject.create(); |
| 268 | + |
| 269 | + TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 270 | + @Override |
| 271 | + public void onStart() { |
| 272 | + requestMore(0); |
| 273 | + } |
| 274 | + }; |
| 275 | + |
| 276 | + Observable.range(1, 10).takeUntil(until).unsafeSubscribe(ts); |
| 277 | + |
| 278 | + assertTrue(until.hasObservers()); |
| 279 | + |
| 280 | + ts.requestMore(1); |
| 281 | + |
| 282 | + ts.assertReceivedOnNext(Arrays.asList(1)); |
| 283 | + ts.assertNoErrors(); |
| 284 | + assertTrue("TestSubscriber completed", ts.getOnCompletedEvents().isEmpty()); |
| 285 | + |
| 286 | + assertFalse("Until still has observers", until.hasObservers()); |
| 287 | + assertFalse("TestSubscriber is unsubscribed", ts.isUnsubscribed()); |
| 288 | + } |
191 | 289 | }
|
0 commit comments