Search in sources :

Example 51 with Changes

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

the class NotifyAboutChangesTest method shouldReceiveOneNotificationWithAllAffectedTablesInTransactionWithMultipleThreads.

@Test
@Repeat(times = 20)
public void shouldReceiveOneNotificationWithAllAffectedTablesInTransactionWithMultipleThreads() throws InterruptedException {
    final String table1 = "test_table1";
    final String table2 = "test_table2";
    final int numberOfThreads = ConcurrencyTesting.optimalTestThreadsCount();
    final TestSubscriber<Changes> testSubscriber = new TestSubscriber<Changes>();
    storIOSQLite.observeChanges(LATEST).subscribe(testSubscriber);
    final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
    lowLevel.beginTransaction();
    final CountDownLatch startAllThreadsLock = new CountDownLatch(1);
    final CountDownLatch allThreadsFinishedLock = new CountDownLatch(numberOfThreads);
    for (int i = 0; i < numberOfThreads; i++) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    // All threads should start "simultaneously".
                    startAllThreadsLock.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                lowLevel.notifyAboutChanges(Changes.newInstance(table1));
                lowLevel.notifyAboutChanges(Changes.newInstance(table2));
                allThreadsFinishedLock.countDown();
            }
        }).start();
    }
    // Ready!
    // Steady!
    // Go!
    startAllThreadsLock.countDown();
    assertThat(allThreadsFinishedLock.await(25, SECONDS)).isTrue();
    // While we in transaction, no changes should be sent.
    testSubscriber.assertValueCount(0);
    lowLevel.endTransaction();
    testSubscriber.assertNoErrors();
    List<Changes> actualChanges = testSubscriber.values();
    assertThat(actualChanges).hasSize(1);
    assertThat(actualChanges.get(0).affectedTables()).containsOnly("test_table1", "test_table2");
}
Also used : Changes(com.pushtorefresh.storio3.sqlite.Changes) CountDownLatch(java.util.concurrent.CountDownLatch) TestSubscriber(io.reactivex.subscribers.TestSubscriber) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) Test(org.junit.Test) Repeat(com.pushtorefresh.storio3.test.Repeat)

Example 52 with Changes

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

the class ChangesFilterTest method applyForTags_shouldFilterRequiredTagWhichIsPartOfSomeChanges.

@Test
public void applyForTags_shouldFilterRequiredTagWhichIsPartOfSomeChanges() {
    final TestSubscriber<Changes> testSubscriber = new TestSubscriber<Changes>();
    Changes changes = Changes.newInstance(new HashSet<String>() {

        {
            add("table1");
        }
    }, new HashSet<String>() {

        {
            add("tag1");
            add("tag2");
        }
    });
    ChangesFilter.applyForTags(Flowable.just(changes, Changes.newInstance("table3", "tag3"), Changes.newInstance("table4")), singleton("tag1")).subscribe(testSubscriber);
    // All other tags should be filtered
    testSubscriber.assertValue(changes);
    testSubscriber.dispose();
}
Also used : Changes(com.pushtorefresh.storio3.sqlite.Changes) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Example 53 with Changes

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

the class ObserveChangesOfTagTest method insertEmission.

@Test
public void insertEmission() {
    final List<User> users = TestFactory.newUsers(10);
    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();
    putUsersBlocking(users);
    // 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) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 54 with Changes

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

the class ObserveChangesOfTagTest method deleteEmission.

@Test
public void deleteEmission() {
    final List<User> users = putUsersBlocking(10);
    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();
    deleteUsersBlocking(users);
    // 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) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 55 with Changes

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

the class DefaultStorIOSQLiteTest method observeChangesOfTags_shouldReceiveIfObservedTagExistInChanges.

@Test
public void observeChangesOfTags_shouldReceiveIfObservedTagExistInChanges() {
    TestSubscriber<Changes> testSubscriber = new TestSubscriber<Changes>();
    String tag1 = "tag1";
    String tag2 = "tag2";
    Set<String> tags = new HashSet<String>(2);
    tags.add(tag1);
    tags.add(tag2);
    storIOSQLite.observeChangesOfTags(tags, LATEST).subscribe(testSubscriber);
    testSubscriber.assertNoValues();
    Changes changes = Changes.newInstance("table1", tag1);
    storIOSQLite.lowLevel().notifyAboutChanges(changes);
    testSubscriber.assertValues(changes);
    testSubscriber.assertNoErrors();
    testSubscriber.dispose();
}
Also used : Changes(com.pushtorefresh.storio3.sqlite.Changes) TestSubscriber(io.reactivex.subscribers.TestSubscriber) HashSet(java.util.HashSet) 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