Search in sources :

Example 11 with Changes

use of com.pushtorefresh.storio3.contentresolver.Changes in project storio by pushtorefresh.

the class PreparedGetNumberOfResultsTest method shouldWrapExceptionIntoStorIOExceptionForFlowable.

@Test
public void shouldWrapExceptionIntoStorIOExceptionForFlowable() {
    final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
    Uri testUri = mock(Uri.class);
    when(storIOContentResolver.observeChangesOfUri(eq(testUri), eq(BackpressureStrategy.MISSING))).thenReturn(Flowable.<Changes>empty());
    // noinspection unchecked
    final GetResolver<Integer> getResolver = mock(GetResolver.class);
    when(getResolver.performGet(eq(storIOContentResolver), any(Query.class))).thenThrow(new IllegalStateException("test exception"));
    final TestSubscriber<Integer> testSubscriber = new TestSubscriber<Integer>();
    new PreparedGetNumberOfResults.Builder(storIOContentResolver).withQuery(Query.builder().uri(testUri).build()).withGetResolver(getResolver).prepare().asRxFlowable(BackpressureStrategy.MISSING).subscribe(testSubscriber);
    testSubscriber.awaitTerminalEvent(60, SECONDS);
    testSubscriber.assertError(StorIOException.class);
    assertThat(testSubscriber.errors()).hasSize(1);
    StorIOException storIOException = (StorIOException) testSubscriber.errors().get(0);
    IllegalStateException cause = (IllegalStateException) storIOException.getCause();
    assertThat(cause).hasMessage("test exception");
    testSubscriber.dispose();
}
Also used : Query(com.pushtorefresh.storio3.contentresolver.queries.Query) StorIOException(com.pushtorefresh.storio3.StorIOException) TestSubscriber(io.reactivex.subscribers.TestSubscriber) StorIOContentResolver(com.pushtorefresh.storio3.contentresolver.StorIOContentResolver) Uri(android.net.Uri) Test(org.junit.Test)

Example 12 with Changes

use of com.pushtorefresh.storio3.contentresolver.Changes in project storio by pushtorefresh.

the class PreparedGetNumberOfResultsTest method shouldWrapExceptionIntoStorIOExceptionForFlowable.

@Test
public void shouldWrapExceptionIntoStorIOExceptionForFlowable() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    when(storIOSQLite.observeChanges(any(BackpressureStrategy.class))).thenReturn(Flowable.<Changes>empty());
    // noinspection unchecked
    final GetResolver<Integer> getResolver = mock(GetResolver.class);
    when(getResolver.performGet(eq(storIOSQLite), any(Query.class))).thenThrow(new IllegalStateException("test exception"));
    final TestSubscriber<Integer> testSubscriber = new TestSubscriber<Integer>();
    new PreparedGetNumberOfResults.Builder(storIOSQLite).withQuery(Query.builder().table("test_table").observesTags("test_tag").build()).withGetResolver(getResolver).prepare().asRxFlowable(LATEST).subscribe(testSubscriber);
    testSubscriber.awaitTerminalEvent(60, SECONDS);
    testSubscriber.assertError(StorIOException.class);
    assertThat(testSubscriber.errorCount()).isEqualTo(1);
    StorIOException storIOException = (StorIOException) testSubscriber.errors().get(0);
    IllegalStateException cause = (IllegalStateException) storIOException.getCause();
    assertThat(cause).hasMessage("test exception");
    testSubscriber.dispose();
}
Also used : Query(com.pushtorefresh.storio3.sqlite.queries.Query) StorIOException(com.pushtorefresh.storio3.StorIOException) BackpressureStrategy(io.reactivex.BackpressureStrategy) TestSubscriber(io.reactivex.subscribers.TestSubscriber) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 13 with Changes

use of com.pushtorefresh.storio3.contentresolver.Changes in project storio by pushtorefresh.

the class PreparedGetCursorTest method shouldWrapExceptionIntoStorIOExceptionForFlowable.

@Test
public void shouldWrapExceptionIntoStorIOExceptionForFlowable() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    when(storIOSQLite.observeChanges(any(BackpressureStrategy.class))).thenReturn(Flowable.<Changes>empty());
    // noinspection unchecked
    final GetResolver<Cursor> getResolver = mock(GetResolver.class);
    when(getResolver.performGet(eq(storIOSQLite), any(Query.class))).thenThrow(new IllegalStateException("test exception"));
    final TestSubscriber<Cursor> testSubscriber = new TestSubscriber<Cursor>();
    new PreparedGetCursor.Builder(storIOSQLite).withQuery(Query.builder().table("test_table").observesTags("test_tag").build()).withGetResolver(getResolver).prepare().asRxFlowable(LATEST).subscribe(testSubscriber);
    testSubscriber.awaitTerminalEvent(60, SECONDS);
    testSubscriber.assertError(StorIOException.class);
    StorIOException storIOException = (StorIOException) testSubscriber.errors().get(0);
    IllegalStateException cause = (IllegalStateException) storIOException.getCause();
    assertThat(cause).hasMessage("test exception");
    testSubscriber.dispose();
}
Also used : Query(com.pushtorefresh.storio3.sqlite.queries.Query) StorIOException(com.pushtorefresh.storio3.StorIOException) BackpressureStrategy(io.reactivex.BackpressureStrategy) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Cursor(android.database.Cursor) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 14 with Changes

use of com.pushtorefresh.storio3.contentresolver.Changes in project storio by pushtorefresh.

the class RxQueryTest method concurrentPutWithoutGlobalTransaction.

@Test
@Repeat(times = 20)
public void concurrentPutWithoutGlobalTransaction() throws InterruptedException {
    final int numberOfConcurrentPuts = ConcurrencyTesting.optimalTestThreadsCount();
    TestSubscriber<Changes> testSubscriber = new TestSubscriber<Changes>();
    storIOSQLite.observeChangesInTable(TweetTableMeta.TABLE, LATEST).subscribe(testSubscriber);
    final CountDownLatch concurrentPutLatch = new CountDownLatch(1);
    final CountDownLatch allPutsDoneLatch = new CountDownLatch(numberOfConcurrentPuts);
    for (int i = 0; i < numberOfConcurrentPuts; i++) {
        final int iCopy = i;
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    concurrentPutLatch.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                storIOSQLite.put().object(Tweet.newInstance(null, 1L, "Some text: " + iCopy)).prepare().executeAsBlocking();
                allPutsDoneLatch.countDown();
            }
        }).start();
    }
    // Start concurrent Put operations.
    concurrentPutLatch.countDown();
    assertThat(allPutsDoneLatch.await(25, SECONDS)).isTrue();
    testSubscriber.assertNoErrors();
    // Put operation creates short-term transaction which might result in merge of some notifications.
    // So we have two extreme cases:
    // - no merged notifications → isEqualTo(numberOfParallelPuts)
    // - all notifications merged → isEqualTo(1)
    // Obviously truth is somewhere between those (depends on CPU of machine that runs test).
    assertThat(testSubscriber.valueCount()).isLessThanOrEqualTo(numberOfConcurrentPuts).isGreaterThanOrEqualTo(1);
}
Also used : Changes(com.pushtorefresh.storio3.sqlite.Changes) TestSubscriber(io.reactivex.subscribers.TestSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) Repeat(com.pushtorefresh.storio3.test.Repeat)

Example 15 with Changes

use of com.pushtorefresh.storio3.contentresolver.Changes in project storio by pushtorefresh.

the class ObserveChangesOfTagTest method updateEmission.

@Test
public void updateEmission() {
    final List<User> users = putUsersBlocking(10);
    final List<User> updated = new ArrayList<User>(users.size());
    for (User user : users) {
        updated.add(User.newInstance(user.id(), user.email()));
    }
    final Queue<Changes> expectedChanges = new LinkedList<Changes>();
    expectedChanges.add(Changes.newInstance(UserTableMeta.TABLE, UserTableMeta.NOTIFICATION_TAG));
    final EmissionChecker emissionChecker = new EmissionChecker(expectedChanges);
    final Disposable disposable = emissionChecker.subscribe();
    storIOSQLite.put().objects(updated).prepare().executeAsBlocking();
    // Should receive changes of Users table
    emissionChecker.awaitNextExpectedValue();
    emissionChecker.assertThatNoExpectedValuesLeft();
    disposable.dispose();
}
Also used : Changes(com.pushtorefresh.storio3.sqlite.Changes) Disposable(io.reactivex.disposables.Disposable) AbstractEmissionChecker(com.pushtorefresh.storio3.test.AbstractEmissionChecker) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)56 TestSubscriber (io.reactivex.subscribers.TestSubscriber)47 Changes (com.pushtorefresh.storio3.contentresolver.Changes)37 Cursor (android.database.Cursor)33 Uri (android.net.Uri)16 Changes (com.pushtorefresh.storio3.sqlite.Changes)16 PutResult (com.pushtorefresh.storio3.contentresolver.operations.put.PutResult)15 Disposable (io.reactivex.disposables.Disposable)11 HashSet (java.util.HashSet)7 DeleteResult (com.pushtorefresh.storio3.contentresolver.operations.delete.DeleteResult)6 AbstractEmissionChecker (com.pushtorefresh.storio3.test.AbstractEmissionChecker)6 LinkedList (java.util.LinkedList)6 ContentResolver (android.content.ContentResolver)5 ContentObserver (android.database.ContentObserver)5 ArrayList (java.util.ArrayList)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Mockito.doAnswer (org.mockito.Mockito.doAnswer)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 Answer (org.mockito.stubbing.Answer)5 ContentValues (android.content.ContentValues)4