Search in sources :

Example 46 with Changes

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

the class ObserveChangesInTableTest 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 47 with Changes

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

the class ObserveChangesInTableTest 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)

Example 48 with Changes

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

the class NotifyAboutChangesTest method shouldReceiveOneNotificationInTransactionWithMultipleThreads.

@Test
public void shouldReceiveOneNotificationInTransactionWithMultipleThreads() throws InterruptedException {
    final String table = "test_table";
    final int numberOfThreads = 100;
    final TestSubscriber<Changes> testSubscriber = new TestSubscriber<Changes>();
    storIOSQLite.observeChanges(LATEST).subscribe(testSubscriber);
    storIOSQLite.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);
                }
                storIOSQLite.lowLevel().notifyAboutChanges(Changes.newInstance(table));
                allThreadsFinishedLock.countDown();
            }
        }).start();
    }
    // Ready!
    // Steady!
    // Go!
    startAllThreadsLock.countDown();
    assertThat(allThreadsFinishedLock.await(20, SECONDS)).isTrue();
    // While we in transaction, no changes should be sent.
    testSubscriber.assertValueCount(0);
    storIOSQLite.lowLevel().endTransaction();
    testSubscriber.assertNoErrors();
    testSubscriber.assertValues(Changes.newInstance(table));
}
Also used : Changes(com.pushtorefresh.storio3.sqlite.Changes) TestSubscriber(io.reactivex.subscribers.TestSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 49 with Changes

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

the class NotifyAboutChangesTest method notifyAboutChangesConcurrently.

@Test
public void notifyAboutChangesConcurrently() {
    // do you feel concurrency?
    final int numberOfThreads = 100;
    final TestSubscriber<Changes> testSubscriber = new TestSubscriber<Changes>();
    final Set<String> tables = new HashSet<String>();
    final List<Changes> expectedChanges = new ArrayList<Changes>();
    for (int i = 0; i < numberOfThreads; i++) {
        final String table = "test_table" + i;
        tables.add(table);
        expectedChanges.add(Changes.newInstance(table));
    }
    storIOSQLite.observeChanges(LATEST).subscribe(testSubscriber);
    final CountDownLatch startAllThreadsLock = new CountDownLatch(1);
    for (int i = 0; i < numberOfThreads; i++) {
        final int finalI = i;
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    // All threads should start "simultaneously".
                    startAllThreadsLock.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                storIOSQLite.lowLevel().notifyAboutChanges(Changes.newInstance("test_table" + finalI));
            }
        }).start();
    }
    // Ready!
    // Steady!
    // Go!
    startAllThreadsLock.countDown();
    final long startTime = SystemClock.elapsedRealtime();
    while (testSubscriber.valueCount() != tables.size() && (SystemClock.elapsedRealtime() - startTime) < 20000) {
        // let other threads work
        Thread.yield();
    }
    testSubscriber.assertNoErrors();
    // notice, that order of received notification can be different
    // but in total, they should be equal
    testSubscriber.assertValueCount(expectedChanges.size());
    assertThat(expectedChanges.containsAll(testSubscriber.values())).isTrue();
}
Also used : Changes(com.pushtorefresh.storio3.sqlite.Changes) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) TestSubscriber(io.reactivex.subscribers.TestSubscriber) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 50 with Changes

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

the class NotifyAboutChangesTest method shouldNotReceiveNotificationIfNoChangesAfterTransactionEnd.

@Test
public void shouldNotReceiveNotificationIfNoChangesAfterTransactionEnd() throws InterruptedException {
    final int numberOfThreads = 100;
    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);
                }
                allThreadsFinishedLock.countDown();
            }
        }).start();
    }
    // Ready!
    // Steady!
    // Go!
    startAllThreadsLock.countDown();
    assertThat(allThreadsFinishedLock.await(20, SECONDS)).isTrue();
    // While we in transaction, no changes should be sent.
    testSubscriber.assertValueCount(0);
    lowLevel.endTransaction();
    testSubscriber.assertNoErrors();
    testSubscriber.assertNoValues();
}
Also used : Changes(com.pushtorefresh.storio3.sqlite.Changes) TestSubscriber(io.reactivex.subscribers.TestSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) 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