|
16 | 16 | package rx.internal.operators;
|
17 | 17 |
|
18 | 18 | import static org.mockito.Matchers.any;
|
19 |
| -import static org.mockito.Mockito.mock; |
20 |
| -import static org.mockito.Mockito.never; |
21 |
| -import static org.mockito.Mockito.times; |
22 |
| -import static org.mockito.Mockito.verify; |
| 19 | +import static org.mockito.Mockito.*; |
23 | 20 |
|
24 |
| -import java.util.Arrays; |
25 |
| -import java.util.List; |
| 21 | +import java.util.*; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | +import java.util.concurrent.atomic.AtomicInteger; |
26 | 24 |
|
27 |
| -import org.junit.Test; |
| 25 | +import org.junit.*; |
28 | 26 |
|
29 | 27 | import rx.Observable;
|
30 | 28 | import rx.Observer;
|
31 | 29 | import rx.exceptions.TestException;
|
32 |
| -import rx.functions.Func0; |
33 |
| -import rx.functions.Func1; |
34 |
| -import rx.functions.Func2; |
| 30 | +import rx.functions.*; |
| 31 | +import rx.observers.TestSubscriber; |
| 32 | +import rx.schedulers.Schedulers; |
35 | 33 |
|
36 | 34 | public class OperatorFlatMapTest {
|
37 | 35 | @Test
|
@@ -312,4 +310,110 @@ public void testFlatMapTransformsMergeException() {
|
312 | 310 | verify(o, never()).onNext(any());
|
313 | 311 | verify(o, never()).onCompleted();
|
314 | 312 | }
|
| 313 | + |
| 314 | + private static <T> Observable<T> compose(Observable<T> source, final AtomicInteger subscriptionCount, final int m) { |
| 315 | + return source.doOnSubscribe(new Action0() { |
| 316 | + @Override |
| 317 | + public void call() { |
| 318 | + if (subscriptionCount.getAndIncrement() >= m) { |
| 319 | + Assert.fail("Too many subscriptions! " + subscriptionCount.get()); |
| 320 | + } |
| 321 | + } |
| 322 | + }).doOnCompleted(new Action0() { |
| 323 | + @Override |
| 324 | + public void call() { |
| 325 | + if (subscriptionCount.decrementAndGet() < 0) { |
| 326 | + Assert.fail("Too many unsubscriptionss! " + subscriptionCount.get()); |
| 327 | + } |
| 328 | + } |
| 329 | + }); |
| 330 | + } |
| 331 | + |
| 332 | + @Test |
| 333 | + public void testFlatMapMaxConcurrent() { |
| 334 | + final int m = 4; |
| 335 | + final AtomicInteger subscriptionCount = new AtomicInteger(); |
| 336 | + Observable<Integer> source = Observable.range(1, 10).flatMap(new Func1<Integer, Observable<Integer>>() { |
| 337 | + @Override |
| 338 | + public Observable<Integer> call(Integer t1) { |
| 339 | + return compose(Observable.range(t1 * 10, 2), subscriptionCount, m) |
| 340 | + .subscribeOn(Schedulers.computation()); |
| 341 | + } |
| 342 | + }, m); |
| 343 | + |
| 344 | + TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); |
| 345 | + |
| 346 | + source.subscribe(ts); |
| 347 | + |
| 348 | + ts.awaitTerminalEvent(); |
| 349 | + ts.assertNoErrors(); |
| 350 | + Set<Integer> expected = new HashSet<Integer>(Arrays.asList( |
| 351 | + 10, 11, 20, 21, 30, 31, 40, 41, 50, 51, 60, 61, 70, 71, 80, 81, 90, 91, 100, 101 |
| 352 | + )); |
| 353 | + Assert.assertEquals(expected.size(), ts.getOnNextEvents().size()); |
| 354 | + Assert.assertTrue(expected.containsAll(ts.getOnNextEvents())); |
| 355 | + } |
| 356 | + @Test |
| 357 | + public void testFlatMapSelectorMaxConcurrent() { |
| 358 | + final int m = 4; |
| 359 | + final AtomicInteger subscriptionCount = new AtomicInteger(); |
| 360 | + Observable<Integer> source = Observable.range(1, 10).flatMap(new Func1<Integer, Observable<Integer>>() { |
| 361 | + @Override |
| 362 | + public Observable<Integer> call(Integer t1) { |
| 363 | + return compose(Observable.range(t1 * 10, 2), subscriptionCount, m) |
| 364 | + .subscribeOn(Schedulers.computation()); |
| 365 | + } |
| 366 | + }, new Func2<Integer, Integer, Integer>() { |
| 367 | + @Override |
| 368 | + public Integer call(Integer t1, Integer t2) { |
| 369 | + return t1 * 1000 + t2; |
| 370 | + } |
| 371 | + }, m); |
| 372 | + |
| 373 | + TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); |
| 374 | + |
| 375 | + source.subscribe(ts); |
| 376 | + |
| 377 | + ts.awaitTerminalEvent(); |
| 378 | + ts.assertNoErrors(); |
| 379 | + Set<Integer> expected = new HashSet<Integer>(Arrays.asList( |
| 380 | + 1010, 1011, 2020, 2021, 3030, 3031, 4040, 4041, 5050, 5051, |
| 381 | + 6060, 6061, 7070, 7071, 8080, 8081, 9090, 9091, 10100, 10101 |
| 382 | + )); |
| 383 | + Assert.assertEquals(expected.size(), ts.getOnNextEvents().size()); |
| 384 | + System.out.println("--> testFlatMapSelectorMaxConcurrent: " + ts.getOnNextEvents()); |
| 385 | + Assert.assertTrue(expected.containsAll(ts.getOnNextEvents())); |
| 386 | + } |
| 387 | + @Test |
| 388 | + public void testFlatMapTransformsMaxConcurrentNormal() { |
| 389 | + final int m = 2; |
| 390 | + final AtomicInteger subscriptionCount = new AtomicInteger(); |
| 391 | + Observable<Integer> onNext = |
| 392 | + compose(Observable.from(Arrays.asList(1, 2, 3)).observeOn(Schedulers.computation()), subscriptionCount, m) |
| 393 | + .subscribeOn(Schedulers.computation()); |
| 394 | + Observable<Integer> onCompleted = compose(Observable.from(Arrays.asList(4)), subscriptionCount, m) |
| 395 | + .subscribeOn(Schedulers.computation()); |
| 396 | + Observable<Integer> onError = Observable.from(Arrays.asList(5)); |
| 397 | + |
| 398 | + Observable<Integer> source = Observable.from(Arrays.asList(10, 20, 30)); |
| 399 | + |
| 400 | + @SuppressWarnings("unchecked") |
| 401 | + Observer<Object> o = mock(Observer.class); |
| 402 | + TestSubscriber<Object> ts = new TestSubscriber<Object>(o); |
| 403 | + |
| 404 | + source.flatMap(just(onNext), just(onError), just0(onCompleted), m).subscribe(ts); |
| 405 | + |
| 406 | + ts.awaitTerminalEvent(1, TimeUnit.SECONDS); |
| 407 | + ts.assertNoErrors(); |
| 408 | + ts.assertTerminalEvent(); |
| 409 | + |
| 410 | + verify(o, times(3)).onNext(1); |
| 411 | + verify(o, times(3)).onNext(2); |
| 412 | + verify(o, times(3)).onNext(3); |
| 413 | + verify(o).onNext(4); |
| 414 | + verify(o).onCompleted(); |
| 415 | + |
| 416 | + verify(o, never()).onNext(5); |
| 417 | + verify(o, never()).onError(any(Throwable.class)); |
| 418 | + } |
315 | 419 | }
|
0 commit comments