Search in sources :

Example 31 with RunTestInLooperThread

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;
            }
        }
    });
}
Also used : Owner(io.realm.entities.Owner) RealmBackgroundTask(io.realm.util.RealmBackgroundTask) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Dog(io.realm.entities.Dog) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 32 with RunTestInLooperThread

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);
}
Also used : AllTypes(io.realm.entities.AllTypes) CountDownLatch(java.util.concurrent.CountDownLatch) RunInLooperThread(io.realm.rule.RunInLooperThread) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Random(java.util.Random) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 33 with RunTestInLooperThread

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();
}
Also used : AllTypes(io.realm.entities.AllTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 34 with RunTestInLooperThread

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());
}
Also used : AllTypes(io.realm.entities.AllTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 35 with RunTestInLooperThread

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();
}
Also used : AllTypes(io.realm.entities.AllTypes) CountDownLatch(java.util.concurrent.CountDownLatch) RunInLooperThread(io.realm.rule.RunInLooperThread) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AnnotationIndexTypes(io.realm.entities.AnnotationIndexTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Aggregations

RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)109 Test (org.junit.Test)109 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)46 UiThreadTest (android.support.test.annotation.UiThreadTest)37 AllTypes (io.realm.entities.AllTypes)35 Dog (io.realm.entities.Dog)30 AllJavaTypes (io.realm.entities.AllJavaTypes)9 Action1 (rx.functions.Action1)9 RunInLooperThread (io.realm.rule.RunInLooperThread)8 AnnotationIndexTypes (io.realm.entities.AnnotationIndexTypes)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 SharedRealm (io.realm.internal.SharedRealm)5 ObjectServerError (io.realm.ObjectServerError)4 SyncUser (io.realm.SyncUser)4 AllTypesPrimaryKey (io.realm.entities.AllTypesPrimaryKey)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 SyncCredentials (io.realm.SyncCredentials)3 Date (java.util.Date)3 ClientResetHandler (io.realm.ClientResetHandler)2