Search in sources :

Example 6 with Changes

use of com.pushtorefresh.storio.sqlite.Changes in project storio by pushtorefresh.

the class RxQueryTest method parallelWritesWithoutTransaction.

@Test
public void parallelWritesWithoutTransaction() {
    final int numberOfParallelWorkers = 50;
    TestSubscriber<Changes> testSubscriber = new TestSubscriber<Changes>();
    storIOSQLite.observeChangesInTable(TweetTableMeta.TABLE).take(numberOfParallelWorkers).subscribe(testSubscriber);
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    for (int i = 0; i < numberOfParallelWorkers; i++) {
        final int copyOfCurrentI = i;
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                storIOSQLite.put().object(Tweet.newInstance(null, 1L, "Some text: " + copyOfCurrentI)).prepare().executeAsBlocking();
            }
        }).start();
    }
    // Release the KRAKEN!
    countDownLatch.countDown();
    testSubscriber.awaitTerminalEvent();
    testSubscriber.assertNoErrors();
    assertThat(testSubscriber.getOnNextEvents()).hasSize(numberOfParallelWorkers);
}
Also used : Changes(com.pushtorefresh.storio.sqlite.Changes) TestSubscriber(rx.observers.TestSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 7 with Changes

use of com.pushtorefresh.storio.sqlite.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().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.
    assertThat(testSubscriber.getOnNextEvents()).hasSize(0);
    lowLevel.endTransaction();
    testSubscriber.assertNoErrors();
    testSubscriber.assertNoValues();
}
Also used : Changes(com.pushtorefresh.storio.sqlite.Changes) TestSubscriber(rx.observers.TestSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 8 with Changes

use of com.pushtorefresh.storio.sqlite.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.observeChangesInTables(tables).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.getOnNextEvents().size() != 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
    assertThat(testSubscriber.getOnNextEvents()).hasSize(expectedChanges.size());
    assertThat(expectedChanges.containsAll(testSubscriber.getOnNextEvents())).isTrue();
}
Also used : Changes(com.pushtorefresh.storio.sqlite.Changes) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) TestSubscriber(rx.observers.TestSubscriber) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with Changes

use of com.pushtorefresh.storio.sqlite.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.observeChangesInTable(table).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.
    assertThat(testSubscriber.getOnNextEvents()).hasSize(0);
    storIOSQLite.lowLevel().endTransaction();
    testSubscriber.assertNoErrors();
    testSubscriber.assertReceivedOnNext(singletonList(Changes.newInstance(table)));
}
Also used : Changes(com.pushtorefresh.storio.sqlite.Changes) TestSubscriber(rx.observers.TestSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 10 with Changes

use of com.pushtorefresh.storio.sqlite.Changes in project storio by pushtorefresh.

the class NotifyAboutChangesTest method shouldReceiveOneNotificationWithAllAffectedTablesInTransactionWithMultipleThreads.

@Test
public void shouldReceiveOneNotificationWithAllAffectedTablesInTransactionWithMultipleThreads() throws InterruptedException {
    final String table1 = "test_table1";
    final String table2 = "test_table2";
    final Set<String> tables = new HashSet<String>(2);
    tables.add(table1);
    tables.add(table2);
    final int numberOfThreads = 100;
    final TestSubscriber<Changes> testSubscriber = new TestSubscriber<Changes>();
    storIOSQLite.observeChangesInTables(tables).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(20, SECONDS)).isTrue();
    // While we in transaction, no changes should be sent.
    assertThat(testSubscriber.getOnNextEvents()).hasSize(0);
    lowLevel.endTransaction();
    testSubscriber.assertNoErrors();
    List<Changes> actualChanges = testSubscriber.getOnNextEvents();
    assertThat(actualChanges).hasSize(1);
    assertThat(actualChanges.get(0).affectedTables()).containsOnly("test_table1", "test_table2");
}
Also used : Changes(com.pushtorefresh.storio.sqlite.Changes) CountDownLatch(java.util.concurrent.CountDownLatch) TestSubscriber(rx.observers.TestSubscriber) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Changes (com.pushtorefresh.storio.sqlite.Changes)12 Test (org.junit.Test)12 TestSubscriber (rx.observers.TestSubscriber)8 StorIOSQLite (com.pushtorefresh.storio.sqlite.StorIOSQLite)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 AbstractEmissionChecker (com.pushtorefresh.storio.test.AbstractEmissionChecker)3 HashSet (java.util.HashSet)3 LinkedList (java.util.LinkedList)3 Subscription (rx.Subscription)3 SQLiteOpenHelper (android.database.sqlite.SQLiteOpenHelper)2 ArrayList (java.util.ArrayList)2