Exemplo n.º 1
0
  /**
   * The error from the user provided Observer is not handled by the subscribe method try/catch.
   *
   * <p>It is handled by the AtomicObserver that wraps the provided Observer.
   *
   * <p>Result: Passes (if AtomicObserver functionality exists)
   */
  @Test
  public void testCustomObservableWithErrorInObserverAsynchronous() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger count = new AtomicInteger();
    final AtomicReference<Throwable> error = new AtomicReference<>();

    // FIXME custom built???
    Observable.just("1", "2", "three", "4")
        .subscribeOn(Schedulers.newThread())
        .safeSubscribe(
            new Observer<String>() {
              @Override
              public void onComplete() {
                System.out.println("completed");
                latch.countDown();
              }

              @Override
              public void onError(Throwable e) {
                error.set(e);
                System.out.println("error");
                e.printStackTrace();
                latch.countDown();
              }

              @Override
              public void onNext(String v) {
                int num = Integer.parseInt(v);
                System.out.println(num);
                // doSomething(num);
                count.incrementAndGet();
              }
            });

    // wait for async sequence to complete
    latch.await();

    assertEquals(2, count.get());
    assertNotNull(error.get());
    if (!(error.get() instanceof NumberFormatException)) {
      fail("It should be a NumberFormatException");
    }
  }