예제 #1
0
  @Test
  public void testFlatMapTransformsException() {
    Observable<Integer> onNext = Observable.from(Arrays.asList(1, 2, 3));
    Observable<Integer> onCompleted = Observable.from(Arrays.asList(4));
    Observable<Integer> onError = Observable.from(Arrays.asList(5));

    Observable<Integer> source =
        Observable.concat(
            Observable.from(Arrays.asList(10, 20, 30)),
            Observable.<Integer>error(new RuntimeException("Forced failure!")));

    @SuppressWarnings("unchecked")
    Observer<Object> o = mock(Observer.class);

    source.flatMap(just(onNext), just(onError), just0(onCompleted)).subscribe(o);

    verify(o, times(3)).onNext(1);
    verify(o, times(3)).onNext(2);
    verify(o, times(3)).onNext(3);
    verify(o).onNext(5);
    verify(o).onCompleted();
    verify(o, never()).onNext(4);

    verify(o, never()).onError(any(Throwable.class));
  }
예제 #2
0
 private Observable<Transaction> withTransactions(
     Observable<? extends Product> productObservable) {
   return productObservable
       .flatMap(
           p -> TransactionFixtures.randomTransactions(p.getCustomerId(), p, rn.nextInt(12) + 3))
       .flatMap(transaction -> transactionService.createTransactionObservable(transaction))
       .last();
 }
예제 #3
0
 private <T extends Aggregate> Observable<T> loadFromMongo(
     final Class<T> aggregateClass, final JsonObject query) {
   final Observable<T> newAggregate = makeNewAggregateOf(aggregateClass);
   return newAggregate.flatMap(
       aggregate ->
           getPersistableEventList(query)
               .flatMap(
                   persistableEvents -> {
                     persistableEvents.stream().forEach(applyEvent(aggregate));
                     return Observable.just(aggregate);
                   }));
 }
예제 #4
0
 @Override
 public Observable<?> call(Observable<? extends Throwable> attempts) {
   return attempts.flatMap(
       new Func1<Throwable, Observable<?>>() {
         @Override
         public Observable<?> call(Throwable throwable) {
           if (++retryCount < maxRetries) {
             return Observable.timer(retryDelayMillis, TimeUnit.MILLISECONDS);
           }
           return Observable.error(throwable);
         }
       });
 }
예제 #5
0
  @Test
  public void testFlatMapTransformsOnNextFuncThrows() {
    Observable<Integer> onCompleted = Observable.from(Arrays.asList(4));
    Observable<Integer> onError = Observable.from(Arrays.asList(5));

    Observable<Integer> source = Observable.from(Arrays.asList(10, 20, 30));

    @SuppressWarnings("unchecked")
    Observer<Object> o = mock(Observer.class);

    source.flatMap(funcThrow(1, onError), just(onError), just0(onCompleted)).subscribe(o);

    verify(o).onError(any(TestException.class));
    verify(o, never()).onNext(any());
    verify(o, never()).onCompleted();
  }
예제 #6
0
 @Override
 public void createInterface(
     final UUID hostId,
     final String name,
     final MAC mac,
     final int mtu,
     final InetAddress[] addresses) {
   Observable<StateKey> o = backend.stateStore().getKey(Host.class, hostId, HostKey());
   o.flatMap(
           new Func1<StateKey, Observable<StateResult>>() {
             @Override
             public Observable<StateResult> call(StateKey stateKey) {
               SingleValueKey k = (SingleValueKey) stateKey;
               State.HostState.Builder bldr = newBuilder();
               if (k.value().nonEmpty()) {
                 try {
                   TextFormat.merge(k.value().get(), bldr);
                 } catch (Exception e) {
                   return Observable.error(e);
                 }
               }
               String hostState =
                   bldr.addInterfaces(
                           Interface.newBuilder()
                               .setName(name)
                               .setMac(mac.toString())
                               .setType(Interface.Type.PHYSICAL)
                               .setMtu(mtu)
                               .addAllAddresses(toIpAdresses(addresses))
                               .build())
                       .build()
                       .toString();
               return backend.stateStore().addValue(Host.class, hostId, HostKey(), hostState);
             }
           })
       .toBlocking()
       .first();
 }
예제 #7
0
  @Test
  public void testFlatMapTransformsMaxConcurrentNormal() {
    final int m = 2;
    final AtomicInteger subscriptionCount = new AtomicInteger();
    Observable<Integer> onNext =
        compose(
                Observable.from(Arrays.asList(1, 2, 3)).observeOn(Schedulers.computation()),
                subscriptionCount,
                m)
            .subscribeOn(Schedulers.computation());
    Observable<Integer> onCompleted =
        compose(Observable.from(Arrays.asList(4)), subscriptionCount, m)
            .subscribeOn(Schedulers.computation());
    Observable<Integer> onError = Observable.from(Arrays.asList(5));

    Observable<Integer> source = Observable.from(Arrays.asList(10, 20, 30));

    @SuppressWarnings("unchecked")
    Observer<Object> o = mock(Observer.class);
    TestSubscriber<Object> ts = new TestSubscriber<Object>(o);

    source.flatMap(just(onNext), just(onError), just0(onCompleted), m).subscribe(ts);

    ts.awaitTerminalEvent(1, TimeUnit.SECONDS);
    ts.assertNoErrors();
    ts.assertTerminalEvent();

    verify(o, times(3)).onNext(1);
    verify(o, times(3)).onNext(2);
    verify(o, times(3)).onNext(3);
    verify(o).onNext(4);
    verify(o).onCompleted();

    verify(o, never()).onNext(5);
    verify(o, never()).onError(any(Throwable.class));
  }
예제 #8
0
 Observable<UserId> initializeUser(Observable<UserId> userIDs) {
   return userIDs
       .flatMap(userId -> createUser(userId, branchDatabase.randomExistingID()))
       .map(user -> user.getId());
 }
예제 #9
0
 @Override
 public Observable<WebPageEntity> parse(WebPageEntity webPageEntity) {
   Observable<DownloadResult> pages =
       client.get(webPageEntity.getUrl(), new DocumentCompletionHandler(webPageEntity));
   return pages.flatMap(this::parseDocument).doOnNext(e -> this.parseResultCounter.inc());
 }