Пример #1
0
  @Test
  public void testAlreadyUnsubscribedInterleavesWithClient() {
    ReplaySubject<Integer> source = ReplaySubject.create();

    Subscriber<Integer> done = Subscribers.empty();
    done.unsubscribe();

    @SuppressWarnings("unchecked")
    Observer<Integer> o = mock(Observer.class);
    InOrder inOrder = inOrder(o);

    Observable<Integer> result = source.publish().refCount();

    result.subscribe(o);

    source.onNext(1);

    result.subscribe(done);

    source.onNext(2);
    source.onCompleted();

    inOrder.verify(o).onNext(1);
    inOrder.verify(o).onNext(2);
    inOrder.verify(o).onCompleted();
    verify(o, never()).onError(any(Throwable.class));
  }
    @Override
    public void subscribe(final Subscriber<? super String> observer) {
      observer.onSubscribe(EmptySubscription.INSTANCE);
      t =
          new Thread(
              new Runnable() {

                @Override
                public void run() {
                  for (String s : valuesToReturn) {
                    if (s == null) {
                      System.out.println("throwing exception");
                      try {
                        Thread.sleep(100);
                      } catch (Throwable e) {

                      }
                      observer.onError(new NullPointerException());
                      return;
                    } else {
                      observer.onNext(s);
                    }
                  }
                  System.out.println("subscription complete");
                  observer.onComplete();
                }
              });
      t.start();
    }
Пример #3
0
  @Test
  public void testAlreadyUnsubscribedClient() {
    Subscriber<Integer> done = Subscribers.empty();
    done.unsubscribe();

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

    Observable<Integer> result = Observable.just(1).publish().refCount();

    result.subscribe(done);

    result.subscribe(o);

    verify(o).onNext(1);
    verify(o).onCompleted();
    verify(o, never()).onError(any(Throwable.class));
  }
 @Override
 public void subscribe(Subscriber<? super String> observer) {
   observer.onSubscribe(EmptySubscription.INSTANCE);
   boolean errorThrown = false;
   for (String s : valuesToReturn) {
     if (s == null) {
       System.out.println("throwing exception");
       observer.onError(new NullPointerException());
       errorThrown = true;
       // purposefully not returning here so it will continue calling onNext
       // so that we also test that we handle bad sequences like this
     } else {
       observer.onNext(s);
     }
   }
   if (!errorThrown) {
     observer.onComplete();
   }
 }
    @Override
    public void subscribe(final Subscriber<? super String> observer) {
      observer.onSubscribe(EmptySubscription.INSTANCE);
      t =
          new Thread(
              new Runnable() {

                @Override
                public void run() {
                  observer.onNext("hello");
                  observer.onComplete();
                }
              });
      t.start();
    }
    @Override
    public void subscribe(final Subscriber<? super String> observer) {
      observer.onSubscribe(EmptySubscription.INSTANCE);
      t =
          new Thread(
              new Runnable() {

                @Override
                public void run() {
                  try {
                    Thread.sleep(100);
                  } catch (InterruptedException e) {
                    observer.onError(e);
                  }
                  observer.onNext("hello");
                  observer.onComplete();
                }
              });
      t.start();
    }
 @Override
 public void subscribe(Subscriber<? super String> observer) {
   observer.onSubscribe(EmptySubscription.INSTANCE);
   observer.onNext("hello");
   observer.onComplete();
 }