OperationNext { public static <T> Iterable<T> next(final Observable<? extends T> items) { return new Iterable<T>() { @Override public Iterator<T> iterator() { NextObserver<T> nextObserver = new NextObserver<T>(); final NextIterator<T> nextIterator = new NextIterator<T>(nextObserver); items.materialize().subscribe(nextObserver); return nextIterator; } }; }  static Iterable<T> next(final Observable<? extends T> items);  }
@Test public void testNext() { Subject<String, String> obs = PublishSubject.create(); Iterator<String> it = next(obs).iterator(); fireOnNextInNewThread(obs, "one"); assertTrue(it.hasNext()); assertEquals("one", it.next()); fireOnNextInNewThread(obs, "two"); assertTrue(it.hasNext()); assertEquals("two", it.next()); obs.onCompleted(); assertFalse(it.hasNext()); try { it.next(); fail("At the end of an iterator should throw a NoSuchElementException"); } catch (NoSuchElementException e) { } assertFalse(it.hasNext()); try { it.next(); fail("At the end of an iterator should throw a NoSuchElementException"); } catch (NoSuchElementException e) { } }
@Test public void testNextWithError() { Subject<String, String> obs = PublishSubject.create(); Iterator<String> it = next(obs).iterator(); fireOnNextInNewThread(obs, "one"); assertTrue(it.hasNext()); assertEquals("one", it.next()); fireOnErrorInNewThread(obs); try { it.hasNext(); fail("Expected an TestException"); } catch (TestException e) { } assertErrorAfterObservableFail(it); }
@Test public void testNextWithEmpty() { Observable<String> obs = Observable.<String> empty().observeOn(Schedulers.newThread()); Iterator<String> it = next(obs).iterator(); assertFalse(it.hasNext()); try { it.next(); fail("At the end of an iterator should throw a NoSuchElementException"); } catch (NoSuchElementException e) { } assertFalse(it.hasNext()); try { it.next(); fail("At the end of an iterator should throw a NoSuchElementException"); } catch (NoSuchElementException e) { } }
@Test public void testOnError() throws Throwable { Subject<String, String> obs = PublishSubject.create(); Iterator<String> it = next(obs).iterator(); obs.onError(new TestException()); try { it.hasNext(); fail("Expected an TestException"); } catch (TestException e) { } assertErrorAfterObservableFail(it); }
@Test public void testOnErrorInNewThread() { Subject<String, String> obs = PublishSubject.create(); Iterator<String> it = next(obs).iterator(); fireOnErrorInNewThread(obs); try { it.hasNext(); fail("Expected an TestException"); } catch (TestException e) { } assertErrorAfterObservableFail(it); }
@Test public void testNextWithOnlyUsingNextMethod() { Subject<String, String> obs = PublishSubject.create(); Iterator<String> it = next(obs).iterator(); fireOnNextInNewThread(obs, "one"); assertEquals("one", it.next()); fireOnNextInNewThread(obs, "two"); assertEquals("two", it.next()); obs.onCompleted(); try { it.next(); fail("At the end of an iterator should throw a NoSuchElementException"); } catch (NoSuchElementException e) { } }
@Test public void testNextWithCallingHasNextMultipleTimes() { Subject<String, String> obs = PublishSubject.create(); Iterator<String> it = next(obs).iterator(); fireOnNextInNewThread(obs, "one"); assertTrue(it.hasNext()); assertTrue(it.hasNext()); assertTrue(it.hasNext()); assertTrue(it.hasNext()); assertEquals("one", it.next()); obs.onCompleted(); try { it.next(); fail("At the end of an iterator should throw a NoSuchElementException"); } catch (NoSuchElementException e) { } }
@Test public void testNoBufferingOrBlockingOfSequence() throws Throwable { final CountDownLatch finished = new CountDownLatch(1); final int COUNT = 30; final CountDownLatch timeHasPassed = new CountDownLatch(COUNT); final AtomicBoolean running = new AtomicBoolean(true); final AtomicInteger count = new AtomicInteger(0); final Observable<Integer> obs = Observable.create(new Observable.OnSubscribeFunc<Integer>() { @Override public Subscription onSubscribe(final Observer<? super Integer> o) { new Thread(new Runnable() { @Override public void run() { try { while (running.get()) { o.onNext(count.incrementAndGet()); timeHasPassed.countDown(); } o.onCompleted(); } catch (Throwable e) { o.onError(e); } finally { finished.countDown(); } } }).start(); return Subscriptions.empty(); } }); Iterator<Integer> it = next(obs).iterator(); assertTrue(it.hasNext()); int a = it.next(); assertTrue(it.hasNext()); int b = it.next(); assertTrue("a and b should be different", a != b); timeHasPassed.await(8000, TimeUnit.MILLISECONDS); assertTrue(it.hasNext()); int c = it.next(); assertTrue("c should not just be the next in sequence", c != (b + 1)); assertTrue("expected that c [" + c + "] is higher than or equal to " + COUNT, c >= COUNT); assertTrue(it.hasNext()); int d = it.next(); assertTrue(d > c); running.set(false); finished.await(); assertFalse(it.hasNext()); System.out.println("a: " + a + " b: " + b + " c: " + c); }
@Test(timeout = 8000) public void testSingleSourceManyIterators() throws InterruptedException { BlockingObservable<Long> source = Observable.interval(200, TimeUnit.MILLISECONDS).take(10).toBlockingObservable(); Iterable<Long> iter = source.next(); for (int j = 0; j < 3; j++) { Iterator<Long> it = iter.iterator(); for (int i = 0; i < 9; i++) { Assert.assertEquals(true, it.hasNext()); Assert.assertEquals(Long.valueOf(i), it.next()); } Thread.sleep(400); Assert.assertEquals(false, it.hasNext()); } }