public void testUnSubscribeForScheduler(Scheduler scheduler) throws InterruptedException { final AtomicInteger countReceived = new AtomicInteger(); final AtomicInteger countGenerated = new AtomicInteger(); final SafeObservableSubscription s = new SafeObservableSubscription(); final CountDownLatch latch = new CountDownLatch(1); s.wrap( Observable.interval(50, TimeUnit.MILLISECONDS) .map( new Func1<Long, Long>() { @Override public Long call(Long aLong) { System.out.println("generated " + aLong); countGenerated.incrementAndGet(); return aLong; } }) .subscribeOn(scheduler) .observeOn(scheduler) .subscribe( new Observer<Long>() { @Override public void onCompleted() { System.out.println("--- completed"); } @Override public void onError(Throwable e) { System.out.println("--- onError"); } @Override public void onNext(Long args) { if (countReceived.incrementAndGet() == 2) { s.unsubscribe(); latch.countDown(); } System.out.println("==> Received " + args); } })); latch.await(1000, TimeUnit.MILLISECONDS); System.out.println("----------- it thinks it is finished ------------------ "); Thread.sleep(100); assertEquals(2, countGenerated.get()); }