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);
}
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");
}
Aggregations