Exemplo n.º 1
0
  @Test
  public void testSwitchWhenOuterCompleteBeforeInner() {
    Observable<Observable<String>> source =
        Observable.create(
            new Observable.OnSubscribeFunc<Observable<String>>() {
              @Override
              public Subscription onSubscribe(Observer<? super Observable<String>> observer) {
                publishNext(
                    observer,
                    50,
                    Observable.create(
                        new Observable.OnSubscribeFunc<String>() {
                          @Override
                          public Subscription onSubscribe(Observer<? super String> observer) {
                            publishNext(observer, 70, "one");
                            publishNext(observer, 100, "two");
                            publishCompleted(observer, 200);
                            return Subscriptions.empty();
                          }
                        }));
                publishCompleted(observer, 60);

                return Subscriptions.empty();
              }
            });

    Observable<String> sampled = Observable.create(OperationSwitch.switchDo(source));
    sampled.subscribe(observer);

    InOrder inOrder = inOrder(observer);

    scheduler.advanceTimeTo(350, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(2)).onNext(anyString());
    inOrder.verify(observer, times(1)).onCompleted();
  }
Exemplo n.º 2
0
  @Test
  public void testSwitchIssue737() {
    // https://github.com/Netflix/RxJava/issues/737
    Observable<Observable<String>> source =
        Observable.create(
            new Observable.OnSubscribeFunc<Observable<String>>() {
              @Override
              public Subscription onSubscribe(Observer<? super Observable<String>> observer) {
                publishNext(
                    observer,
                    0,
                    Observable.create(
                        new Observable.OnSubscribeFunc<String>() {
                          @Override
                          public Subscription onSubscribe(Observer<? super String> observer) {
                            publishNext(observer, 10, "1-one");
                            publishNext(observer, 20, "1-two");
                            // The following events will be ignored
                            publishNext(observer, 30, "1-three");
                            publishCompleted(observer, 40);
                            return Subscriptions.empty();
                          }
                        }));
                publishNext(
                    observer,
                    25,
                    Observable.create(
                        new Observable.OnSubscribeFunc<String>() {
                          @Override
                          public Subscription onSubscribe(Observer<? super String> observer) {
                            publishNext(observer, 10, "2-one");
                            publishNext(observer, 20, "2-two");
                            publishNext(observer, 30, "2-three");
                            publishCompleted(observer, 40);
                            return Subscriptions.empty();
                          }
                        }));
                publishCompleted(observer, 30);
                return Subscriptions.empty();
              }
            });

    Observable<String> sampled = Observable.create(OperationSwitch.switchDo(source));
    sampled.subscribe(observer);

    scheduler.advanceTimeTo(1000, TimeUnit.MILLISECONDS);

    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onNext("1-one");
    inOrder.verify(observer, times(1)).onNext("1-two");
    inOrder.verify(observer, times(1)).onNext("2-one");
    inOrder.verify(observer, times(1)).onNext("2-two");
    inOrder.verify(observer, times(1)).onNext("2-three");
    inOrder.verify(observer, times(1)).onCompleted();
    inOrder.verifyNoMoreInteractions();
  }
Exemplo n.º 3
0
  @Test
  public void testSwitchWithSubsequenceError() {
    Observable<Observable<String>> source =
        Observable.create(
            new Observable.OnSubscribeFunc<Observable<String>>() {
              @Override
              public Subscription onSubscribe(Observer<? super Observable<String>> observer) {
                publishNext(
                    observer,
                    50,
                    Observable.create(
                        new Observable.OnSubscribeFunc<String>() {
                          @Override
                          public Subscription onSubscribe(Observer<? super String> observer) {
                            publishNext(observer, 50, "one");
                            publishNext(observer, 100, "two");
                            return Subscriptions.empty();
                          }
                        }));

                publishNext(
                    observer,
                    130,
                    Observable.create(
                        new Observable.OnSubscribeFunc<String>() {
                          @Override
                          public Subscription onSubscribe(Observer<? super String> observer) {
                            publishError(observer, 0, new TestException());
                            return Subscriptions.empty();
                          }
                        }));

                publishNext(
                    observer,
                    150,
                    Observable.create(
                        new Observable.OnSubscribeFunc<String>() {
                          @Override
                          public Subscription onSubscribe(Observer<? super String> observer) {
                            publishNext(observer, 50, "three");
                            return Subscriptions.empty();
                          }
                        }));

                return Subscriptions.empty();
              }
            });

    Observable<String> sampled = Observable.create(OperationSwitch.switchDo(source));
    sampled.subscribe(observer);

    InOrder inOrder = inOrder(observer);

    scheduler.advanceTimeTo(90, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, never()).onNext(anyString());
    verify(observer, never()).onCompleted();
    verify(observer, never()).onError(any(Throwable.class));

    scheduler.advanceTimeTo(125, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(1)).onNext("one");
    verify(observer, never()).onCompleted();
    verify(observer, never()).onError(any(Throwable.class));

    scheduler.advanceTimeTo(250, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, never()).onNext("three");
    verify(observer, never()).onCompleted();
    verify(observer, times(1)).onError(any(TestException.class));
  }