use of com.pushtorefresh.storio3.sqlite.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();
}
use of com.pushtorefresh.storio3.sqlite.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();
}
use of com.pushtorefresh.storio3.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.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));
}
use of com.pushtorefresh.storio3.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.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();
}
use of com.pushtorefresh.storio3.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(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();
}
Aggregations