コード例 #1
0
ファイル: ObservableTests.java プロジェクト: spoon-bot/RxJava
 /**
  * https://github.com/ReactiveX/RxJava/issues/198
  *
  * <p>Rx Design Guidelines 5.2
  *
  * <p>"when calling the Subscribe method that only has an onNext argument, the OnError behavior
  * will be to rethrow the exception on the thread that the message comes out from the Observable.
  * The OnCompleted behavior in this case is to do nothing."
  *
  * @throws InterruptedException
  */
 @Test
 @Ignore("Subscribers can't throw")
 public void testErrorThrownWithoutErrorHandlerAsynchronous() throws InterruptedException {
   final CountDownLatch latch = new CountDownLatch(1);
   final AtomicReference<Throwable> exception = new AtomicReference<>();
   Observable.create(
           observer -> {
             new Thread(
                     () -> {
                       try {
                         observer.onError(new Error("failure"));
                       } catch (Throwable e) {
                         // without an onError handler it has to just throw on whatever thread
                         // invokes it
                         exception.set(e);
                       }
                       latch.countDown();
                     })
                 .start();
           })
       .subscribe();
   // wait for exception
   latch.await(3000, TimeUnit.MILLISECONDS);
   assertNotNull(exception.get());
   assertEquals("failure", exception.get().getMessage());
 }
コード例 #2
0
ファイル: ObservableTests.java プロジェクト: spoon-bot/RxJava
  @Test
  public void testPublishLast() throws InterruptedException {
    final AtomicInteger count = new AtomicInteger();
    ConnectableObservable<String> connectable =
        Observable.<String>create(
                observer -> {
                  observer.onSubscribe(EmptySubscription.INSTANCE);
                  count.incrementAndGet();
                  new Thread(
                          () -> {
                            observer.onNext("first");
                            observer.onNext("last");
                            observer.onComplete();
                          })
                      .start();
                })
            .takeLast(1)
            .publish();

    // subscribe once
    final CountDownLatch latch = new CountDownLatch(1);
    connectable.subscribe(
        value -> {
          assertEquals("last", value);
          latch.countDown();
        });

    // subscribe twice
    connectable.subscribe();

    Disposable subscription = connectable.connect();
    assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
    assertEquals(1, count.get());
    subscription.dispose();
  }
  @Test(timeout = 1000L)
  public void testSynchronousError() {
    final Observable<Observable<String>> o1 = Observable.error(new RuntimeException("unit test"));

    final CountDownLatch latch = new CountDownLatch(1);
    Observable.mergeDelayError(o1)
        .subscribe(
            new Observer<String>() {
              @Override
              public void onComplete() {
                fail("Expected onError path");
              }

              @Override
              public void onError(Throwable e) {
                latch.countDown();
              }

              @Override
              public void onNext(String s) {
                fail("Expected onError path");
              }
            });

    try {
      latch.await();
    } catch (InterruptedException ex) {
      fail("interrupted");
    }
  }
コード例 #4
0
  @Test
  public void isDone_shouldReturnTrue_whenTheThreadIsAlive_givenAFutureReturnedByExecute()
      throws Exception {
    CountDownLatch latch = new CountDownLatch(1);

    Future<?> result = asyncExecutor.execute(suppressCheckedExceptions(latch::await));

    try {
      assertFalse(result.isDone());
    } finally {
      latch.countDown(); // don't want a thread waiting forever...
    }
  }
コード例 #5
0
  @Test
  public void execute_shouldCreateAndStartAThreadWithTheProvidedRunnableAndReturnAFuture()
      throws Exception {
    CountDownLatch latch = new CountDownLatch(1);

    Future<?> result = asyncExecutor.execute(latch::countDown);
    latch.await(5, TimeUnit.SECONDS); // give the new thread time to start to running

    verify(factory).newThread(any()); // The provided runnable will be wrapped, not passed directly
    assertEquals(0, latch.getCount()); // but the runnable will be executed
    assertNotNull(result);
    assertNotNull(createdThread);
    assertNotEquals(Thread.State.NEW, createdThread.getState());
  }
コード例 #6
0
ファイル: ObservableTests.java プロジェクト: spoon-bot/RxJava
  @Test
  public void testReplay() throws InterruptedException {
    final AtomicInteger counter = new AtomicInteger();
    ConnectableObservable<String> o =
        Observable.<String>create(
                observer -> {
                  observer.onSubscribe(EmptySubscription.INSTANCE);
                  new Thread(
                          new Runnable() {

                            @Override
                            public void run() {
                              counter.incrementAndGet();
                              observer.onNext("one");
                              observer.onComplete();
                            }
                          })
                      .start();
                })
            .replay();

    // we connect immediately and it will emit the value
    Disposable s = o.connect();
    try {

      // we then expect the following 2 subscriptions to get that same value
      final CountDownLatch latch = new CountDownLatch(2);

      // subscribe once
      o.subscribe(
          v -> {
            assertEquals("one", v);
            latch.countDown();
          });

      // subscribe again
      o.subscribe(
          v -> {
            assertEquals("one", v);
            latch.countDown();
          });

      if (!latch.await(1000, TimeUnit.MILLISECONDS)) {
        fail("subscriptions did not receive values");
      }
      assertEquals(1, counter.get());
    } finally {
      s.dispose();
    }
  }
コード例 #7
0
ファイル: ObservableTests.java プロジェクト: spoon-bot/RxJava
  /**
   * 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");
    }
  }
コード例 #8
0
ファイル: ObservableTests.java プロジェクト: spoon-bot/RxJava
  @Test
  public void testCacheWithCapacity() throws InterruptedException {
    final AtomicInteger counter = new AtomicInteger();
    Observable<String> o =
        Observable.<String>create(
                observer -> {
                  observer.onSubscribe(EmptySubscription.INSTANCE);
                  new Thread(
                          () -> {
                            counter.incrementAndGet();
                            observer.onNext("one");
                            observer.onComplete();
                          })
                      .start();
                })
            .cache(1);

    // we then expect the following 2 subscriptions to get that same value
    final CountDownLatch latch = new CountDownLatch(2);

    // subscribe once
    o.subscribe(
        v -> {
          assertEquals("one", v);
          latch.countDown();
        });

    // subscribe again
    o.subscribe(
        v -> {
          assertEquals("one", v);
          latch.countDown();
        });

    if (!latch.await(1000, TimeUnit.MILLISECONDS)) {
      fail("subscriptions did not receive values");
    }
    assertEquals(1, counter.get());
  }