Search in sources :

Example 51 with TestObserver

use of io.reactivex.observers.TestObserver in project rxjava2-jdbc by davidmoten.

the class NonBlockingPoolTest method testPoolFactoryWhenFailsThenRecovers.

@Test
public void testPoolFactoryWhenFailsThenRecovers() {
    AtomicReference<Throwable> ex = new AtomicReference<>();
    Consumer<? super Throwable> handler = RxJavaPlugins.getErrorHandler();
    RxJavaPlugins.setErrorHandler(t -> ex.set(t));
    try {
        TestScheduler s = new TestScheduler();
        AtomicInteger c = new AtomicInteger();
        NonBlockingPool<Integer> pool = NonBlockingPool.factory(() -> {
            if (c.getAndIncrement() == 0) {
                throw new TestException();
            } else {
                return c.get();
            }
        }).maxSize(// 
        1).scheduler(// 
        s).createRetryInterval(10, // 
        TimeUnit.SECONDS).build();
        TestObserver<Integer> ts = // 
        pool.member().map(// 
        m -> m.value()).test().assertNotTerminated().assertNoValues();
        s.triggerActions();
        assertTrue(ex.get() instanceof UndeliverableException);
        assertTrue(((UndeliverableException) ex.get()).getCause() instanceof TestException);
        s.advanceTimeBy(10, TimeUnit.SECONDS);
        s.triggerActions();
        ts.assertComplete();
        ts.assertValue(2);
    } finally {
        RxJavaPlugins.setErrorHandler(handler);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays(java.util.Arrays) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UndeliverableException(io.reactivex.exceptions.UndeliverableException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) Scheduler(io.reactivex.Scheduler) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers(io.reactivex.schedulers.Schedulers) TestSubscriber(io.reactivex.subscribers.TestSubscriber) FlowableSingleDeferUntilRequest(org.davidmoten.rx.internal.FlowableSingleDeferUntilRequest) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) TestObserver(io.reactivex.observers.TestObserver) RxJavaPlugins(io.reactivex.plugins.RxJavaPlugins) Consumer(io.reactivex.functions.Consumer) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Disposable(io.reactivex.disposables.Disposable) Assert.assertNull(org.junit.Assert.assertNull) SingleObserver(io.reactivex.SingleObserver) Assert.assertFalse(org.junit.Assert.assertFalse) TestScheduler(io.reactivex.schedulers.TestScheduler) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) TestScheduler(io.reactivex.schedulers.TestScheduler) UndeliverableException(io.reactivex.exceptions.UndeliverableException) Test(org.junit.Test)

Example 52 with TestObserver

use of io.reactivex.observers.TestObserver in project quran_android by quran.

the class RecentPageModelTest method testUpdateLatestPageWithSlowRecents.

@Test
public void testUpdateLatestPageWithSlowRecents() {
    final TestScheduler testScheduler = new TestScheduler();
    RecentPageModel recentPageModel = new RecentPageModel(bookmarksAdapter) {

        @Override
        Single<List<RecentPage>> getRecentPagesObservable() {
            // testScheduler simulates the passing of 5 seconds (the timer time).
            return Single.timer(5, TimeUnit.SECONDS, testScheduler).map(aLong -> SAMPLE_RECENT_PAGES).subscribeOn(Schedulers.trampoline());
        }
    };
    TestObserver<Integer> testObserver = new TestObserver<>();
    recentPageModel.getLatestPageObservable().take(2).subscribe(testObserver);
    // write before the mock data from getRecentPagesObservable comes back
    recentPageModel.updateLatestPage(51);
    // now let's pretend the recent page came back - we expect that this page should be ignored.
    testScheduler.advanceTimeBy(5, TimeUnit.SECONDS);
    // and that another page afterwards was written
    recentPageModel.updateLatestPage(23);
    testObserver.awaitTerminalEvent();
    testObserver.assertValues(51, 23);
}
Also used : BeforeClass(org.junit.BeforeClass) RxAndroidPlugins(io.reactivex.android.plugins.RxAndroidPlugins) Mock(org.mockito.Mock) Constants(com.quran.labs.androidquran.data.Constants) BookmarksDBAdapter(com.quran.labs.androidquran.database.BookmarksDBAdapter) Test(org.junit.Test) TestObserver(io.reactivex.observers.TestObserver) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Single(io.reactivex.Single) RecentPage(com.quran.labs.androidquran.dao.RecentPage) ArrayList(java.util.ArrayList) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) MockitoAnnotations(org.mockito.MockitoAnnotations) List(java.util.List) Matchers.anyInt(org.mockito.Matchers.anyInt) Schedulers(io.reactivex.schedulers.Schedulers) TestScheduler(io.reactivex.schedulers.TestScheduler) Before(org.junit.Before) ArrayList(java.util.ArrayList) List(java.util.List) TestScheduler(io.reactivex.schedulers.TestScheduler) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 53 with TestObserver

use of io.reactivex.observers.TestObserver in project quran_android by quran.

the class TranslationManagerPresenterTest method getRemoteTranslationListObservableIssue.

@Test
public void getRemoteTranslationListObservableIssue() throws Exception {
    MockResponse mockResponse = new MockResponse();
    mockResponse.setResponseCode(500);
    this.mockWebServer.enqueue(mockResponse);
    TestObserver<TranslationList> testObserver = new TestObserver<>();
    this.translationManager.getRemoteTranslationListObservable().subscribe(testObserver);
    testObserver.awaitTerminalEvent();
    testObserver.assertNoValues();
    testObserver.assertError(IOException.class);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) TranslationList(com.quran.labs.androidquran.dao.translation.TranslationList) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 54 with TestObserver

use of io.reactivex.observers.TestObserver in project quran_android by quran.

the class BookmarkImportExportModelTest method testReadInvalidBookmarks.

@Test
public void testReadInvalidBookmarks() {
    TestObserver<BookmarkData> testObserver = new TestObserver<>();
    Buffer source = new Buffer();
    source.writeUtf8(")");
    bookmarkImportExportModel.readBookmarks(source).subscribe(testObserver);
    testObserver.awaitTerminalEvent();
    testObserver.assertValueCount(0);
    testObserver.assertError(IOException.class);
}
Also used : Buffer(okio.Buffer) BookmarkData(com.quran.labs.androidquran.dao.BookmarkData) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 55 with TestObserver

use of io.reactivex.observers.TestObserver in project tutorials by eugenp.

the class ExceptionHandlingTest method givenSubscriberAndError_whenChangeStateOnError_thenErrorThrown.

@Test
public void givenSubscriberAndError_whenChangeStateOnError_thenErrorThrown() {
    TestObserver<String> testObserver = new TestObserver<>();
    final AtomicBoolean state = new AtomicBoolean(false);
    Observable.<String>error(UNKNOWN_ERROR).doOnError(throwable -> state.set(true)).subscribe(testObserver);
    testObserver.assertError(UNKNOWN_ERROR);
    testObserver.assertNotComplete();
    testObserver.assertNoValues();
    assertTrue("state should be changed", state.get());
}
Also used : CompositeException(io.reactivex.exceptions.CompositeException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Assert.assertTrue(org.junit.Assert.assertTrue) Observable(io.reactivex.Observable) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Aggregations

TestObserver (io.reactivex.observers.TestObserver)158 Test (org.junit.Test)128 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)31 TargetApi (android.annotation.TargetApi)21 Matchers.anyString (org.mockito.Matchers.anyString)21 StorIOException (com.pushtorefresh.storio3.StorIOException)19 TestException (io.reactivex.exceptions.TestException)19 Observable (io.reactivex.Observable)12 BaseTest (io.rx_cache2.internal.common.BaseTest)12 Completable (io.reactivex.Completable)11 Disposable (io.reactivex.disposables.Disposable)11 InOrder (org.mockito.InOrder)10 TestScheduler (io.reactivex.schedulers.TestScheduler)9 StorIOSQLite (com.pushtorefresh.storio3.sqlite.StorIOSQLite)8 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)7 Schedulers (io.reactivex.schedulers.Schedulers)6 Activity (android.app.Activity)5 Instrumentation (android.app.Instrumentation)5 ContentValues (android.content.ContentValues)5 Context (android.content.Context)5