/** * Converts this {@code Observable} into an {@link Iterable}. * <p> * <img width="640" height="315" src="https://github.com/ReactiveX/RxJava/wiki/images/rx-operators/B.toIterable.png" alt=""> * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code blockingIterable} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * * @return an {@link Iterable} version of this {@code Observable} * @see <a href="http://reactivex.io/documentation/operators/to.html">ReactiveX documentation: To</a> */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Iterable<T> blockingIterable() { return blockingIterable(bufferSize()); }
/** * Converts this {@code Observable} into an {@link Iterable}. * <p> * <img width="640" height="315" src="https://github.com/ReactiveX/RxJava/wiki/images/rx-operators/B.toIterable.png" alt=""> * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code blockingIterable} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * * @return an {@link Iterable} version of this {@code Observable} * @see <a href="http://reactivex.io/documentation/operators/to.html">ReactiveX documentation: To</a> */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Iterable<T> blockingIterable() { return blockingIterable(bufferSize()); }
@Override public Integer apply(Integer v) throws Exception { Observable.just(1).delay(10, TimeUnit.SECONDS).blockingIterable().iterator().next(); return v; } })
Iterator<T> it = blockingIterable().iterator(); while (it.hasNext()) { try {
Iterator<T> it = blockingIterable().iterator(); while (it.hasNext()) { try {
@Test public void testRangeWithOverflow5() { assertFalse(Observable.range(Integer.MIN_VALUE, 0).blockingIterable().iterator().hasNext()); }
@Test public void testRangeWithOverflow5() { assertFalse(Observable.rangeLong(Long.MIN_VALUE, 0).blockingIterable().iterator().hasNext()); }
@Test(expected = TestException.class) public void testToIteratorWithException() { Observable<String> obs = Observable.unsafeCreate(new ObservableSource<String>() { @Override public void subscribe(Observer<? super String> observer) { observer.onSubscribe(Disposables.empty()); observer.onNext("one"); observer.onError(new TestException()); } }); Iterator<String> it = obs.blockingIterable().iterator(); assertEquals(true, it.hasNext()); assertEquals("one", it.next()); assertEquals(true, it.hasNext()); it.next(); }
@Ignore("subscribe() should not throw") @Test(expected = TestException.class) public void testExceptionThrownFromOnSubscribe() { Iterable<String> strings = Observable.unsafeCreate(new ObservableSource<String>() { @Override public void subscribe(Observer<? super String> observer) { throw new TestException("intentional"); } }).blockingIterable(); for (String string : strings) { // never reaches here System.out.println(string); } }
@Test public void testToIterator() { Observable<String> obs = Observable.just("one", "two", "three"); Iterator<String> it = obs.blockingIterable().iterator(); assertEquals(true, it.hasNext()); assertEquals("one", it.next()); assertEquals(true, it.hasNext()); assertEquals("two", it.next()); assertEquals(true, it.hasNext()); assertEquals("three", it.next()); assertEquals(false, it.hasNext()); }
@Test public void testMaxConcurrent() { for (int times = 0; times < 100; times++) { int observableCount = 100; // Test maxConcurrent from 2 to 12 int maxConcurrent = 2 + (times % 10); AtomicInteger subscriptionCount = new AtomicInteger(0); List<Observable<String>> os = new ArrayList<Observable<String>>(); List<SubscriptionCheckObservable> scos = new ArrayList<SubscriptionCheckObservable>(); for (int i = 0; i < observableCount; i++) { SubscriptionCheckObservable sco = new SubscriptionCheckObservable(subscriptionCount, maxConcurrent); scos.add(sco); os.add(Observable.unsafeCreate(sco)); } Iterator<String> iter = Observable.merge(os, maxConcurrent).blockingIterable().iterator(); List<String> actual = new ArrayList<String>(); while (iter.hasNext()) { actual.add(iter.next()); } // System.out.println("actual: " + actual); assertEquals(5 * observableCount, actual.size()); for (SubscriptionCheckObservable sco : scos) { assertFalse(sco.failed); } } }
@Test public void testMergeALotOfSourcesOneByOneSynchronously() { int n = 10000; List<Observable<Integer>> sourceList = new ArrayList<Observable<Integer>>(n); for (int i = 0; i < n; i++) { sourceList.add(Observable.just(i)); } Iterator<Integer> it = Observable.merge(Observable.fromIterable(sourceList), 1).blockingIterable().iterator(); int j = 0; while (it.hasNext()) { assertEquals((Integer)j, it.next()); j++; } assertEquals(j, n); } @Test
@Test public void testMergeALotOfSourcesOneByOneSynchronouslyTakeHalf() { int n = 10000; List<Observable<Integer>> sourceList = new ArrayList<Observable<Integer>>(n); for (int i = 0; i < n; i++) { sourceList.add(Observable.just(i)); } Iterator<Integer> it = Observable.merge(Observable.fromIterable(sourceList), 1).take(n / 2).blockingIterable().iterator(); int j = 0; while (it.hasNext()) { assertEquals((Integer)j, it.next()); j++; } assertEquals(j, n / 2); }
@Test public void testWhenMaxConcurrentIsOne() { for (int i = 0; i < 100; i++) { List<Observable<String>> os = new ArrayList<Observable<String>>(); os.add(Observable.just("one", "two", "three", "four", "five").subscribeOn(Schedulers.newThread())); os.add(Observable.just("one", "two", "three", "four", "five").subscribeOn(Schedulers.newThread())); os.add(Observable.just("one", "two", "three", "four", "five").subscribeOn(Schedulers.newThread())); List<String> expected = Arrays.asList("one", "two", "three", "four", "five", "one", "two", "three", "four", "five", "one", "two", "three", "four", "five"); Iterator<String> iter = Observable.merge(os, 1).blockingIterable().iterator(); List<String> actual = new ArrayList<String>(); while (iter.hasNext()) { actual.add(iter.next()); } assertEquals(expected, actual); } }