@Test(timeout = 1000)
  public void testSameSourceMultipleIterators() {
    TestScheduler scheduler = new TestScheduler();

    NbpBlockingObservable<Long> source =
        NbpObservable.interval(1, TimeUnit.SECONDS, scheduler).take(10).toBlocking();

    Iterable<Long> iter = source.latest();

    for (int j = 0; j < 3; j++) {
      Iterator<Long> it = iter.iterator();

      // only 9 because take(10) will immediately call onCompleted when receiving the 10th item
      // which onCompleted will overwrite the previous value
      for (int i = 0; i < 9; i++) {
        scheduler.advanceTimeBy(1, TimeUnit.SECONDS);

        Assert.assertEquals(true, it.hasNext());

        Assert.assertEquals(Long.valueOf(i), it.next());
      }

      scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
      Assert.assertEquals(false, it.hasNext());
    }
  }
  @Test(timeout = 1000, expected = RuntimeException.class)
  public void testNextThrows() {
    TestScheduler scheduler = new TestScheduler();

    NbpBlockingObservable<Long> source =
        NbpObservable.<Long>error(new RuntimeException("Forced failure!"))
            .subscribeOn(scheduler)
            .toBlocking();

    Iterable<Long> iter = source.latest();
    Iterator<Long> it = iter.iterator();

    scheduler.advanceTimeBy(1, TimeUnit.SECONDS);

    it.next();
  }
  @Test(timeout = 1000, expected = NoSuchElementException.class)
  public void testSimpleJustNext() {
    TestScheduler scheduler = new TestScheduler();

    NbpBlockingObservable<Long> source =
        NbpObservable.interval(1, TimeUnit.SECONDS, scheduler).take(10).toBlocking();

    Iterable<Long> iter = source.latest();

    Iterator<Long> it = iter.iterator();

    // only 9 because take(10) will immediately call onCompleted when receiving the 10th item
    // which onCompleted will overwrite the previous value
    for (int i = 0; i < 10; i++) {
      scheduler.advanceTimeBy(1, TimeUnit.SECONDS);

      Assert.assertEquals(Long.valueOf(i), it.next());
    }
  }
コード例 #4
0
  @Test
  public void timestampWithScheduler2() {
    TestScheduler scheduler = new TestScheduler();

    PublishSubject<Integer> source = PublishSubject.create();
    Observable<Timed<Integer>> m = source.timestamp(scheduler);
    m.subscribe(observer);

    source.onNext(1);
    source.onNext(2);
    scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS);
    scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS);
    source.onNext(3);

    InOrder inOrder = inOrder(observer);

    inOrder.verify(observer, times(1)).onNext(new Timed<>(1, 0, TimeUnit.MILLISECONDS));
    inOrder.verify(observer, times(1)).onNext(new Timed<>(2, 0, TimeUnit.MILLISECONDS));
    inOrder.verify(observer, times(1)).onNext(new Timed<>(3, 200, TimeUnit.MILLISECONDS));

    verify(observer, never()).onError(any(Throwable.class));
    verify(observer, never()).onComplete();
  }