@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()); }
@Test public void testDefer() throws Throwable { Supplier<Observable<String>> factory = mock(Supplier.class); Observable<String> firstObservable = Observable.just("one", "two"); Observable<String> secondObservable = Observable.just("three", "four"); when(factory.get()).thenReturn(firstObservable, secondObservable); Observable<String> deferred = Observable.defer(factory); verifyZeroInteractions(factory); Subscriber<String> firstObserver = TestHelper.mockSubscriber(); deferred.subscribe(firstObserver); verify(factory, times(1)).get(); verify(firstObserver, times(1)).onNext("one"); verify(firstObserver, times(1)).onNext("two"); verify(firstObserver, times(0)).onNext("three"); verify(firstObserver, times(0)).onNext("four"); verify(firstObserver, times(1)).onComplete(); Subscriber<String> secondObserver = TestHelper.mockSubscriber(); deferred.subscribe(secondObserver); verify(factory, times(2)).get(); verify(secondObserver, times(0)).onNext("one"); verify(secondObserver, times(0)).onNext("two"); verify(secondObserver, times(1)).onNext("three"); verify(secondObserver, times(1)).onNext("four"); verify(secondObserver, times(1)).onComplete(); }
@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)); }
@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)); }
@Test public void testAssert() { Observable<Integer> oi = Observable.from(Arrays.asList(1, 2)); TestSubscriber<Integer> o = new TestSubscriber<Integer>(); oi.subscribe(o); o.assertReceivedOnNext(Arrays.asList(1, 2)); assertEquals(2, o.getOnNextEvents().size()); o.assertTerminalEvent(); }
@Test public void testIgnoreElements() { Observable<Integer> observable = Observable.just(1, 2, 3).ignoreElements(); Subscriber<Object> observer = TestHelper.mockSubscriber(); observable.subscribe(observer); verify(observer, never()).onNext(any(Integer.class)); verify(observer, never()).onError(any(Throwable.class)); verify(observer, times(1)).onComplete(); }
@Test public void testContainsWithEmptyObservable() { Observable<Boolean> observable = Observable.<String>empty().contains("a"); Subscriber<Object> observer = TestHelper.mockSubscriber(); observable.subscribe(observer); verify(observer, times(1)).onNext(false); verify(observer, never()).onNext(true); verify(observer, never()).onError(org.mockito.Matchers.any(Throwable.class)); verify(observer, times(1)).onComplete(); }
@Test public void testCacheWithCapacity() throws InterruptedException { final AtomicInteger counter = new AtomicInteger(); Observable<String> o = Observable.<String>create( observer -> { observer.onSubscribe(EmptySubscription.INSTANCE); new Thread( () -> { counter.incrementAndGet(); observer.onNext("one"); observer.onComplete(); }) .start(); }) .cache(1); // we then expect the following 2 subscriptions to get that same value final CountDownLatch latch = new CountDownLatch(2); // subscribe once o.subscribe( v -> { assertEquals("one", v); latch.countDown(); }); // subscribe again o.subscribe( v -> { assertEquals("one", v); latch.countDown(); }); if (!latch.await(1000, TimeUnit.MILLISECONDS)) { fail("subscriptions did not receive values"); } assertEquals(1, counter.get()); }
@Test public void testMaterializeDematerializeChaining() { Observable<Integer> obs = Observable.just(1); Observable<Integer> chained = obs.materialize().dematerialize(); Subscriber<Integer> observer = TestHelper.mockSubscriber(); chained.subscribe(observer); verify(observer, times(1)).onNext(1); verify(observer, times(1)).onComplete(); verify(observer, times(0)).onError(any(Throwable.class)); }
@Test public void testWrappingMock() { Observable<Integer> oi = Observable.from(Arrays.asList(1, 2)); @SuppressWarnings("unchecked") Observer<Integer> mockObserver = mock(Observer.class); oi.subscribe(new TestSubscriber<Integer>(mockObserver)); InOrder inOrder = inOrder(mockObserver); inOrder.verify(mockObserver, times(1)).onNext(1); inOrder.verify(mockObserver, times(1)).onNext(2); inOrder.verify(mockObserver, times(1)).onCompleted(); inOrder.verifyNoMoreInteractions(); }
@Test public void testAssertNotMatchValue() { Observable<Integer> oi = Observable.from(Arrays.asList(1, 2)); TestSubscriber<Integer> o = new TestSubscriber<Integer>(); oi.subscribe(o); thrown.expect(AssertionError.class); thrown.expectMessage("Value at index: 1 expected to be [3] (Integer) but was: [2] (Integer)"); o.assertReceivedOnNext(Arrays.asList(1, 3)); assertEquals(2, o.getOnNextEvents().size()); o.assertTerminalEvent(); }
@Test public void testAssertNotMatchCount() { Observable<Integer> oi = Observable.from(Arrays.asList(1, 2)); TestSubscriber<Integer> o = new TestSubscriber<Integer>(); oi.subscribe(o); thrown.expect(AssertionError.class); thrown.expectMessage("Number of items does not match. Provided: 1 Actual: 2"); o.assertReceivedOnNext(Arrays.asList(1)); assertEquals(2, o.getOnNextEvents().size()); o.assertTerminalEvent(); }
@Test public void testContainsWithInexistence() { Observable<Boolean> observable = Observable.just("a", "b").contains("c"); // FIXME null values are not allowed, removed Subscriber<Object> observer = TestHelper.mockSubscriber(); observable.subscribe(observer); verify(observer, times(1)).onNext(false); verify(observer, never()).onNext(true); verify(observer, never()).onError(org.mockito.Matchers.any(Throwable.class)); verify(observer, times(1)).onComplete(); }
@Test public void testContains() { Observable<Boolean> observable = Observable.just("a", "b", "c").contains("b"); // FIXME nulls not allowed, changed to "c" Subscriber<Boolean> observer = TestHelper.mockSubscriber(); observable.subscribe(observer); verify(observer, times(1)).onNext(true); verify(observer, never()).onNext(false); verify(observer, never()).onError(org.mockito.Matchers.any(Throwable.class)); verify(observer, times(1)).onComplete(); }
@Test @Ignore("null values are not allowed") public void testContainsWithNull() { Observable<Boolean> observable = Observable.just("a", "b", null).contains(null); Subscriber<Object> observer = TestHelper.mockSubscriber(); observable.subscribe(observer); verify(observer, times(1)).onNext(true); verify(observer, never()).onNext(false); verify(observer, never()).onError(org.mockito.Matchers.any(Throwable.class)); verify(observer, times(1)).onComplete(); }
@Test public void testOfType() { Observable<String> observable = Observable.just(1, "abc", false, 2L).ofType(String.class); Subscriber<Object> observer = TestHelper.mockSubscriber(); observable.subscribe(observer); verify(observer, never()).onNext(1); verify(observer, times(1)).onNext("abc"); verify(observer, never()).onNext(false); verify(observer, never()).onNext(2L); verify(observer, never()).onError(org.mockito.Matchers.any(Throwable.class)); verify(observer, times(1)).onComplete(); }
@Test public void testCreate() { Observable<String> observable = Observable.just("one", "two", "three"); Subscriber<String> observer = TestHelper.mockSubscriber(); observable.subscribe(observer); verify(observer, times(1)).onNext("one"); verify(observer, times(1)).onNext("two"); verify(observer, times(1)).onNext("three"); verify(observer, never()).onError(any(Throwable.class)); verify(observer, times(1)).onComplete(); }
@Test public void testDeferFunctionThrows() { Supplier<Observable<String>> factory = mock(Supplier.class); when(factory.get()).thenThrow(new TestException()); Observable<String> result = Observable.defer(factory); Observer<String> o = mock(Observer.class); result.subscribe(o); verify(o).onError(any(TestException.class)); verify(o, never()).onNext(any(String.class)); verify(o, never()).onComplete(); }
@Test public void testJustWithScheduler() { TestScheduler scheduler = new TestScheduler(); Observable<Integer> observable = Observable.fromArray(1, 2).subscribeOn(scheduler); Subscriber<Integer> observer = TestHelper.mockSubscriber(); observable.subscribe(observer); scheduler.advanceTimeBy(1, TimeUnit.MILLISECONDS); InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onNext(1); inOrder.verify(observer, times(1)).onNext(2); inOrder.verify(observer, times(1)).onComplete(); inOrder.verifyNoMoreInteractions(); }
@Ignore // FIXME throwing is not allowed from the create?! @Test public void testOnSubscribeFails() { Subscriber<String> observer = TestHelper.mockSubscriber(); final RuntimeException re = new RuntimeException("bad impl"); Observable<String> o = Observable.create( s -> { throw re; }); o.subscribe(observer); verify(observer, times(0)).onNext(anyString()); verify(observer, times(0)).onComplete(); verify(observer, times(1)).onError(re); }
@Test public void testHiding() { PublishSubject<Integer> src = PublishSubject.create(); Observable<Integer> dst = src.asObservable(); assertFalse(dst instanceof PublishSubject); Subscriber<Object> o = TestHelper.mockSubscriber(); dst.subscribe(o); src.onNext(1); src.onComplete(); verify(o).onNext(1); verify(o).onComplete(); verify(o, never()).onError(any(Throwable.class)); }
@Test public void testHidingError() { PublishSubject<Integer> src = PublishSubject.create(); Observable<Integer> dst = src.asObservable(); assertFalse(dst instanceof PublishSubject); @SuppressWarnings("unchecked") Observer<Object> o = mock(Observer.class); dst.subscribe(o); src.onError(new TestException()); verify(o, never()).onNext(any()); verify(o, never()).onComplete(); verify(o).onError(any(TestException.class)); }
@Test public void testOfTypeWithPolymorphism() { ArrayList<Integer> l1 = new ArrayList<>(); l1.add(1); LinkedList<Integer> l2 = new LinkedList<>(); l2.add(2); @SuppressWarnings("rawtypes") Observable<List> observable = Observable.<Object>just(l1, l2, "123").ofType(List.class); Subscriber<Object> observer = TestHelper.mockSubscriber(); observable.subscribe(observer); verify(observer, times(1)).onNext(l1); verify(observer, times(1)).onNext(l2); verify(observer, never()).onNext("123"); verify(observer, never()).onError(org.mockito.Matchers.any(Throwable.class)); verify(observer, times(1)).onComplete(); }
@Test public void testRefCount() { TestScheduler s = new TestScheduler(); Observable<Long> interval = Observable.interval(100, TimeUnit.MILLISECONDS, s).publish().refCount(); // subscribe list1 final List<Long> list1 = new ArrayList<Long>(); Subscription s1 = interval.subscribe( new Action1<Long>() { @Override public void call(Long t1) { list1.add(t1); } }); s.advanceTimeBy(200, TimeUnit.MILLISECONDS); assertEquals(2, list1.size()); assertEquals(0L, list1.get(0).longValue()); assertEquals(1L, list1.get(1).longValue()); // subscribe list2 final List<Long> list2 = new ArrayList<Long>(); Subscription s2 = interval.subscribe( new Action1<Long>() { @Override public void call(Long t1) { list2.add(t1); } }); s.advanceTimeBy(300, TimeUnit.MILLISECONDS); // list 1 should have 5 items assertEquals(5, list1.size()); assertEquals(2L, list1.get(2).longValue()); assertEquals(3L, list1.get(3).longValue()); assertEquals(4L, list1.get(4).longValue()); // list 2 should only have 3 items assertEquals(3, list2.size()); assertEquals(2L, list2.get(0).longValue()); assertEquals(3L, list2.get(1).longValue()); assertEquals(4L, list2.get(2).longValue()); // unsubscribe list1 s1.unsubscribe(); // advance further s.advanceTimeBy(300, TimeUnit.MILLISECONDS); // list 1 should still have 5 items assertEquals(5, list1.size()); // list 2 should have 6 items assertEquals(6, list2.size()); assertEquals(5L, list2.get(3).longValue()); assertEquals(6L, list2.get(4).longValue()); assertEquals(7L, list2.get(5).longValue()); // unsubscribe list2 s2.unsubscribe(); // advance further s.advanceTimeBy(1000, TimeUnit.MILLISECONDS); // subscribing a new one should start over because the source should have been unsubscribed // subscribe list3 final List<Long> list3 = new ArrayList<Long>(); Subscription s3 = interval.subscribe( new Action1<Long>() { @Override public void call(Long t1) { list3.add(t1); } }); s.advanceTimeBy(200, TimeUnit.MILLISECONDS); assertEquals(2, list3.size()); assertEquals(0L, list3.get(0).longValue()); assertEquals(1L, list3.get(1).longValue()); }