Search in sources :

Example 1 with Repeat

use of com.pushtorefresh.storio3.test.Repeat 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 2 with Repeat

use of com.pushtorefresh.storio3.test.Repeat 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)

Aggregations

Changes (com.pushtorefresh.storio3.sqlite.Changes)2 Repeat (com.pushtorefresh.storio3.test.Repeat)2 TestSubscriber (io.reactivex.subscribers.TestSubscriber)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.Test)2 StorIOSQLite (com.pushtorefresh.storio3.sqlite.StorIOSQLite)1