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