@Test public void testInterruptTerminalEventAwaitTimed() { TestSubscriber<Integer> ts = TestSubscriber.create(); final Thread t0 = Thread.currentThread(); Worker w = Schedulers.computation().createWorker(); try { w.schedule( new Action0() { @Override public void call() { t0.interrupt(); } }, 200, TimeUnit.MILLISECONDS); try { ts.awaitTerminalEvent(5, TimeUnit.SECONDS); fail("Did not interrupt wait!"); } catch (RuntimeException ex) { if (!(ex.getCause() instanceof InterruptedException)) { fail("The cause is not InterruptedException! " + ex.getCause()); } } } finally { w.unsubscribe(); } }
@Test public void testAwaitTerminalEventWithDuration() { TestSubscriber<Object> ts = new TestSubscriber<Object>(); Observable.just(1).subscribe(ts); ts.awaitTerminalEvent(1, TimeUnit.SECONDS); ts.assertTerminalEvent(); }
@Test public void testInterruptTerminalEventAwaitAndUnsubscribe() { TestSubscriber<Integer> ts = TestSubscriber.create(); final Thread t0 = Thread.currentThread(); Worker w = Schedulers.computation().createWorker(); try { w.schedule( new Action0() { @Override public void call() { t0.interrupt(); } }, 200, TimeUnit.MILLISECONDS); ts.awaitTerminalEventAndUnsubscribeOnTimeout(5, TimeUnit.SECONDS); if (!ts.isUnsubscribed()) { fail("Did not unsubscribe!"); } } finally { w.unsubscribe(); } }
@Test public void testAssertError() { RuntimeException e = new RuntimeException("Oops"); TestSubscriber<Object> subscriber = new TestSubscriber<Object>(); Observable.error(e).subscribe(subscriber); subscriber.assertError(e); }
@Test public void testDelegate1() { TestObserver<Integer> to = new TestObserver<Integer>(); TestSubscriber<Integer> ts = TestSubscriber.create(to); ts.onCompleted(); to.assertTerminalEvent(); }
@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 testNotCompleted() { TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); try { ts.assertCompleted(); } catch (AssertionError ex) { // expected return; } fail("Not completed and no assertion error!"); }
@Test public void testNoError2() { TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); try { ts.assertError(new TestException()); } catch (AssertionError ex) { // expected return; } fail("No present but no assertion error!"); }
@Test public void testUnsubscribed() { TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); try { ts.assertUnsubscribed(); } catch (AssertionError ex) { // expected return; } fail("Not unsubscribed but not reported!"); }
@Test public void testNoValues() { TestSubscriber<Integer> ts = TestSubscriber.create(); ts.onNext(1); try { ts.assertNoValues(); fail("Failed to report there were values!"); } catch (AssertionError ex) { // expected } }
@Test public void testDifferentError3() { TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); ts.onError(new RuntimeException()); try { ts.assertError(TestException.class); } catch (AssertionError ex) { // expected return; } fail("Different Error present but no assertion error!"); }
@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 testNoTerminalEventBut1Error() { TestSubscriber<Integer> ts = TestSubscriber.create(); ts.onError(new TestException()); try { ts.assertNoTerminalEvent(); fail("Failed to report there were terminal event(s)!"); } catch (AssertionError ex) { // expected } }
@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 testMultipleCompletions2() { TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); ts.onCompleted(); ts.onCompleted(); try { ts.assertNotCompleted(); } catch (AssertionError ex) { // expected return; } fail("Multiple completions and no assertion error!"); }
@Test public void testAssertTerminalEventNotReceived() { PublishSubject<Integer> p = PublishSubject.create(); TestSubscriber<Integer> o = new TestSubscriber<Integer>(); p.subscribe(o); p.onNext(1); p.onNext(2); thrown.expect(AssertionError.class); thrown.expectMessage("No terminal events received."); o.assertReceivedOnNext(Arrays.asList(1, 2)); assertEquals(2, o.getOnNextEvents().size()); o.assertTerminalEvent(); }
@Test public void testMultipleErrors3() { TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); ts.onError(new TestException()); ts.onError(new TestException()); try { ts.assertError(new TestException()); } catch (AssertionError ex) { if (!(ex.getCause() instanceof CompositeException)) { fail("Multiple Error present but the reported error doesn't have a composite cause!"); } // expected return; } fail("Multiple Error present but no assertion error!"); }
@Test public void testNoTerminalEventBut2Errors() { TestSubscriber<Integer> ts = TestSubscriber.create(); ts.onError(new TestException()); ts.onError(new TestException()); try { ts.assertNoTerminalEvent(); fail("Failed to report there were terminal event(s)!"); } catch (AssertionError ex) { // expected if (!(ex.getCause() instanceof CompositeException)) { fail("Did not report a composite exception cause: " + ex.getCause()); } } }
@Test(timeout = 1000) public void testOnErrorCrashCountsDownLatch() { TestObserver<Integer> to = new TestObserver<Integer>() { @Override public void onError(Throwable e) { throw new TestException(); } }; TestSubscriber<Integer> ts = TestSubscriber.create(to); try { ts.onError(new RuntimeException()); } catch (TestException ex) { // expected } ts.awaitTerminalEvent(); }
@Test public void testAwaitTerminalEventWithDurationAndUnsubscribeOnTimeout() { TestSubscriber<Object> ts = new TestSubscriber<Object>(); final AtomicBoolean unsub = new AtomicBoolean(false); Observable.just(1) // .doOnUnsubscribe( new Action0() { @Override public void call() { unsub.set(true); } }) // .delay(1000, TimeUnit.MILLISECONDS) .subscribe(ts); ts.awaitTerminalEventAndUnsubscribeOnTimeout(100, TimeUnit.MILLISECONDS); assertTrue(unsub.get()); }
@Test public void testDelegate3() { TestSubscriber<Integer> ts1 = TestSubscriber.create(); TestSubscriber<Integer> ts2 = TestSubscriber.create(ts1, 0); ts2.onCompleted(); ts1.assertCompleted(); }
@Test(expected = NullPointerException.class) public void testNullDelegate3() { TestSubscriber<Integer> ts = new TestSubscriber<Integer>((Subscriber<Integer>) null, 0); ts.onCompleted(); }