use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class RealmAsyncQueryTests method queryingLinkHandover.
// This test makes sure that Async queries update when using link.
@Test
@RunTestInLooperThread
public void queryingLinkHandover() throws Throwable {
final AtomicInteger numberOfInvocations = new AtomicInteger(0);
final Realm realm = looperThread.realm;
final RealmResults<Dog> allAsync = realm.where(Dog.class).equalTo("owner.name", "kiba").findAllAsync();
looperThread.keepStrongReference.add(allAsync);
allAsync.addChangeListener(new RealmChangeListener<RealmResults<Dog>>() {
@Override
public void onChange(RealmResults<Dog> object) {
switch(numberOfInvocations.incrementAndGet()) {
case 1:
assertEquals(0, allAsync.size());
assertTrue(allAsync.isLoaded());
assertTrue(allAsync.isValid());
assertTrue(allAsync.isEmpty());
new RealmBackgroundTask(realm.getConfiguration()) {
@Override
public void doInBackground(Realm realm) {
realm.beginTransaction();
Dog dog = realm.createObject(Dog.class);
dog.setAge(10);
dog.setName("Akamaru");
Owner kiba = realm.createObject(Owner.class);
kiba.setName("kiba");
dog.setOwner(kiba);
realm.commitTransaction();
}
}.awaitOrFail();
break;
case 2:
assertEquals(1, realm.where(Dog.class).count());
assertEquals(1, realm.where(Owner.class).count());
assertEquals(1, allAsync.size());
assertTrue(allAsync.isLoaded());
assertTrue(allAsync.isValid());
assertFalse(allAsync.isEmpty());
assertEquals(1, allAsync.size());
assertEquals("Akamaru", allAsync.get(0).getName());
assertEquals("kiba", allAsync.get(0).getOwner().getName());
looperThread.testComplete();
break;
}
}
});
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class RealmAsyncQueryTests method stressTestBackgroundCommits.
// Keeps advancing the Realm by sending 1 commit for each frame (16ms).
// The async queries should keep up with the modification.
@Test
@RunTestInLooperThread
public void stressTestBackgroundCommits() throws Throwable {
final int NUMBER_OF_COMMITS = 100;
final CountDownLatch bgRealmClosed = new CountDownLatch(1);
final long[] latestLongValue = new long[1];
final float[] latestFloatValue = new float[1];
// Starts a background thread that pushes a commit every 16ms.
final Thread backgroundThread = new Thread() {
@Override
public void run() {
Random random = new Random(System.currentTimeMillis());
Realm backgroundThreadRealm = Realm.getInstance(looperThread.realm.getConfiguration());
for (int i = 0; i < NUMBER_OF_COMMITS; i++) {
backgroundThreadRealm.beginTransaction();
AllTypes object = backgroundThreadRealm.createObject(AllTypes.class);
latestLongValue[0] = random.nextInt(100);
latestFloatValue[0] = random.nextFloat();
object.setColumnFloat(latestFloatValue[0]);
object.setColumnLong(latestLongValue[0]);
backgroundThreadRealm.commitTransaction();
// Waits 16ms. Before adding the next commit.
SystemClock.sleep(16);
}
backgroundThreadRealm.close();
bgRealmClosed.countDown();
}
};
final RealmResults<AllTypes> allAsync = looperThread.realm.where(AllTypes.class).findAllAsync();
allAsync.addChangeListener(new RealmChangeListener<RealmResults<AllTypes>>() {
@Override
public void onChange(RealmResults<AllTypes> object) {
assertTrue(allAsync.isLoaded());
if (allAsync.size() == NUMBER_OF_COMMITS) {
AllTypes lastInserted = looperThread.realm.where(AllTypes.class).equalTo("columnLong", latestLongValue[0]).equalTo("columnFloat", latestFloatValue[0]).findFirst();
assertNotNull(lastInserted);
TestHelper.awaitOrFail(bgRealmClosed);
looperThread.testComplete();
}
}
});
looperThread.keepStrongReference.add(allAsync);
looperThread.postRunnableDelayed(new Runnable() {
@Override
public void run() {
backgroundThread.start();
}
}, 16);
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class RealmAsyncQueryTests method findFirstAsync_updatedIfsyncRealmObjectIsUpdated.
@Test
@RunTestInLooperThread
public void findFirstAsync_updatedIfsyncRealmObjectIsUpdated() throws Throwable {
populateTestRealm(looperThread.realm, 1);
AllTypes firstSync = looperThread.realm.where(AllTypes.class).findFirst();
assertEquals(0, firstSync.getColumnLong());
assertEquals("test data 0", firstSync.getColumnString());
final AllTypes firstAsync = looperThread.realm.where(AllTypes.class).findFirstAsync();
assertTrue(firstAsync.load());
assertTrue(firstAsync.isLoaded());
assertTrue(firstAsync.isValid());
assertEquals(0, firstAsync.getColumnLong());
assertEquals("test data 0", firstAsync.getColumnString());
looperThread.keepStrongReference.add(firstAsync);
firstAsync.addChangeListener(new RealmChangeListener<AllTypes>() {
@Override
public void onChange(AllTypes object) {
assertEquals("Galacticon", firstAsync.getColumnString());
looperThread.testComplete();
}
});
looperThread.realm.beginTransaction();
firstSync.setColumnString("Galacticon");
looperThread.realm.commitTransaction();
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class RealmAsyncQueryTests method findFirstAsync_initalEmptyRow.
@Test
@RunTestInLooperThread
public void findFirstAsync_initalEmptyRow() throws Throwable {
Realm realm = looperThread.realm;
final AllTypes firstAsync = realm.where(AllTypes.class).findFirstAsync();
looperThread.keepStrongReference.add(firstAsync);
firstAsync.addChangeListener(new RealmChangeListener<AllTypes>() {
@Override
public void onChange(AllTypes object) {
assertTrue(firstAsync.load());
assertTrue(firstAsync.isLoaded());
assertTrue(firstAsync.isValid());
assertEquals(0, firstAsync.getColumnLong());
looperThread.testComplete();
}
});
realm.beginTransaction();
realm.createObject(AllTypes.class).setColumnLong(0);
realm.commitTransaction();
assertTrue(firstAsync.load());
assertTrue(firstAsync.isLoaded());
}
use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.
the class RealmAsyncQueryTests method batchUpdateDifferentTypeOfQueries.
@Test
@RunTestInLooperThread
public void batchUpdateDifferentTypeOfQueries() {
final Realm realm = looperThread.realm;
realm.beginTransaction();
for (int i = 0; i < 5; ) {
AllTypes allTypes = realm.createObject(AllTypes.class);
allTypes.setColumnLong(i);
allTypes.setColumnString("data " + i % 3);
allTypes = realm.createObject(AllTypes.class);
allTypes.setColumnLong(i);
allTypes.setColumnString("data " + (++i % 3));
}
final long numberOfBlocks = 25;
// Must be greater than 1
final long numberOfObjects = 10;
realm.commitTransaction();
populateForDistinct(realm, numberOfBlocks, numberOfObjects, false);
RealmResults<AllTypes> findAllAsync = realm.where(AllTypes.class).findAllAsync();
RealmResults<AllTypes> findAllSorted = realm.where(AllTypes.class).findAllSortedAsync("columnString", Sort.ASCENDING);
RealmResults<AllTypes> findAllSortedMulti = realm.where(AllTypes.class).findAllSortedAsync(new String[] { "columnString", "columnLong" }, new Sort[] { Sort.ASCENDING, Sort.DESCENDING });
RealmResults<AnnotationIndexTypes> findDistinct = realm.where(AnnotationIndexTypes.class).distinctAsync("indexString");
looperThread.keepStrongReference.add(findAllAsync);
looperThread.keepStrongReference.add(findAllSorted);
looperThread.keepStrongReference.add(findAllSortedMulti);
looperThread.keepStrongReference.add(findDistinct);
final CountDownLatch queriesCompleted = new CountDownLatch(4);
final CountDownLatch bgRealmClosedLatch = new CountDownLatch(1);
final AtomicInteger batchUpdateCompleted = new AtomicInteger(0);
final AtomicInteger findAllAsyncInvocation = new AtomicInteger(0);
final AtomicInteger findAllSortedInvocation = new AtomicInteger(0);
final AtomicInteger findAllSortedMultiInvocation = new AtomicInteger(0);
final AtomicInteger findDistinctInvocation = new AtomicInteger(0);
findAllAsync.addChangeListener(new RealmChangeListener<RealmResults<AllTypes>>() {
@Override
public void onChange(RealmResults<AllTypes> object) {
switch(findAllAsyncInvocation.incrementAndGet()) {
case 1:
{
queriesCompleted.countDown();
break;
}
case 2:
{
if (batchUpdateCompleted.incrementAndGet() == 4) {
looperThread.testComplete(bgRealmClosedLatch);
}
break;
}
}
}
});
findAllSorted.addChangeListener(new RealmChangeListener<RealmResults<AllTypes>>() {
@Override
public void onChange(RealmResults<AllTypes> object) {
switch(findAllSortedInvocation.incrementAndGet()) {
case 1:
{
queriesCompleted.countDown();
break;
}
case 2:
{
if (batchUpdateCompleted.incrementAndGet() == 4) {
looperThread.testComplete(bgRealmClosedLatch);
}
break;
}
}
}
});
findAllSortedMulti.addChangeListener(new RealmChangeListener<RealmResults<AllTypes>>() {
@Override
public void onChange(RealmResults<AllTypes> object) {
switch(findAllSortedMultiInvocation.incrementAndGet()) {
case 1:
{
queriesCompleted.countDown();
break;
}
case 2:
{
if (batchUpdateCompleted.incrementAndGet() == 4) {
looperThread.testComplete(bgRealmClosedLatch);
}
break;
}
}
}
});
findDistinct.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {
@Override
public void onChange(RealmResults<AnnotationIndexTypes> object) {
switch(findDistinctInvocation.incrementAndGet()) {
case 1:
{
queriesCompleted.countDown();
break;
}
case 2:
{
if (batchUpdateCompleted.incrementAndGet() == 4) {
looperThread.testComplete(bgRealmClosedLatch);
}
break;
}
}
}
});
// Waits for the queries to complete then sends a commit from
// another thread to trigger a batch update of the 4 queries.
new Thread() {
@Override
public void run() {
try {
queriesCompleted.await();
Realm bgRealm = Realm.getInstance(realm.getConfiguration());
bgRealm.beginTransaction();
bgRealm.createObject(AllTypes.class);
bgRealm.createObject(AnnotationIndexTypes.class);
bgRealm.commitTransaction();
bgRealm.close();
bgRealmClosedLatch.countDown();
} catch (InterruptedException e) {
fail(e.getMessage());
}
}
}.start();
}
Aggregations