Beispiel #1
0
 @Test
 public void onlyFirstShouldSubscribeAndLastUnsubscribe() {
   final AtomicInteger subscriptionCount = new AtomicInteger();
   final AtomicInteger unsubscriptionCount = new AtomicInteger();
   Observable<Integer> observable =
       Observable.create(
           new Observable.OnSubscribeFunc<Integer>() {
             @Override
             public Subscription onSubscribe(Observer<? super Integer> observer) {
               subscriptionCount.incrementAndGet();
               return Subscriptions.create(
                   new Action0() {
                     @Override
                     public void call() {
                       unsubscriptionCount.incrementAndGet();
                     }
                   });
             }
           });
   Observable<Integer> refCounted = observable.publish().refCount();
   @SuppressWarnings("unchecked")
   Observer<Integer> observer = mock(Observer.class);
   Subscription first = refCounted.subscribe(observer);
   assertEquals(1, subscriptionCount.get());
   Subscription second = refCounted.subscribe(observer);
   assertEquals(1, subscriptionCount.get());
   first.unsubscribe();
   assertEquals(0, unsubscriptionCount.get());
   second.unsubscribe();
   assertEquals(1, unsubscriptionCount.get());
 }