Пример #1
0
  @Test
  @SuppressWarnings("unchecked")
  public void testSimple() throws InterruptedException {
    final ExecutorService exec = Executors.newCachedThreadPool();
    try {
      final CountDownLatch ready = new CountDownLatch(1);

      Func0<Future<Observable<Integer>>> func =
          new Func0<Future<Observable<Integer>>>() {
            @Override
            public Future<Observable<Integer>> call() {
              return exec.submit(
                  new Callable<Observable<Integer>>() {
                    @Override
                    public Observable<Integer> call() throws Exception {
                      if (!ready.await(1000, TimeUnit.MILLISECONDS)) {
                        throw new IllegalStateException("Not started in time");
                      }
                      return Observable.from(1);
                    }
                  });
            }
          };

      Observable<Integer> result = Async.deferFuture(func, Schedulers.threadPoolForComputation());

      final Observer<Integer> observer = mock(Observer.class);

      final CountDownLatch done = new CountDownLatch(1);

      result.subscribe(new OperationStartFutureTest.MockHelper<Integer>(observer, done));

      ready.countDown();

      if (!done.await(1000, TimeUnit.MILLISECONDS)) {
        fail("Not completed in time!");
      }

      InOrder inOrder = inOrder(observer);

      inOrder.verify(observer).onNext(1);
      inOrder.verify(observer).onCompleted();
      verify(observer, never()).onError(any(Throwable.class));
    } finally {
      exec.shutdown();
    }
  }
Пример #2
0
  @Test
  @SuppressWarnings("unchecked")
  public void testSimpleFactoryThrows() {
    Func0<Future<Observable<Integer>>> func =
        new Func0<Future<Observable<Integer>>>() {

          @Override
          public Future<Observable<Integer>> call() {
            throw new OperationStartFutureTest.CustomException();
          }
        };

    Observable<Integer> result = Async.deferFuture(func);

    final Observer<Object> observer = mock(Observer.class);
    result.subscribe(observer);

    verify(observer, never()).onNext(any());
    verify(observer, never()).onCompleted();
    verify(observer).onError(any(OperationStartFutureTest.CustomException.class));
  }