Search in sources :

Example 1 with RealmThread

use of io.realm.util.RealmThread in project realm-java by realm.

the class RealmTests method waitForChange_runWithRealmThread.

// waitForChange & stopWaitForChange within a simple Thread wrapper.
@Test
public void waitForChange_runWithRealmThread() throws InterruptedException {
    final CountDownLatch bgRealmStarted = new CountDownLatch(1);
    final CountDownLatch bgRealmFished = new CountDownLatch(1);
    final AtomicBoolean bgRealmChangeResult = new AtomicBoolean(false);
    final AtomicLong bgRealmResultSize = new AtomicLong(0);
    RealmThread thread = new RealmThread(realmConfig, new RealmThread.RealmRunnable() {

        @Override
        public void run(Realm realm) {
            bgRealmStarted.countDown();
            bgRealmChangeResult.set(realm.waitForChange());
            bgRealmResultSize.set(realm.where(AllTypes.class).count());
            realm.close();
            bgRealmFished.countDown();
        }
    });
    thread.start();
    TestHelper.awaitOrFail(bgRealmStarted);
    populateTestRealm();
    TestHelper.awaitOrFail(bgRealmFished);
    assertTrue(bgRealmChangeResult.get());
    assertEquals(TEST_DATA_SIZE, bgRealmResultSize.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) AllTypes(io.realm.entities.AllTypes) CountDownLatch(java.util.concurrent.CountDownLatch) RealmThread(io.realm.util.RealmThread) OsSharedRealm(io.realm.internal.OsSharedRealm) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 2 with RealmThread

use of io.realm.util.RealmThread in project realm-java by realm.

the class RealmTests method waitForChange_endRealmThread.

@Test
public void waitForChange_endRealmThread() throws InterruptedException {
    final CountDownLatch bgRealmStarted = new CountDownLatch(1);
    final CountDownLatch bgRealmFished = new CountDownLatch(1);
    final AtomicBoolean bgRealmChangeResult = new AtomicBoolean(true);
    RealmThread thread = new RealmThread(realmConfig, new RealmThread.RealmRunnable() {

        @Override
        public void run(Realm realm) {
            bgRealmStarted.countDown();
            bgRealmChangeResult.set(realm.waitForChange());
            realm.close();
            bgRealmFished.countDown();
        }
    });
    thread.start();
    TestHelper.awaitOrFail(bgRealmStarted);
    thread.end();
    TestHelper.awaitOrFail(bgRealmFished);
    assertFalse(bgRealmChangeResult.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CountDownLatch(java.util.concurrent.CountDownLatch) RealmThread(io.realm.util.RealmThread) OsSharedRealm(io.realm.internal.OsSharedRealm) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Aggregations

UiThreadTest (androidx.test.annotation.UiThreadTest)2 OsSharedRealm (io.realm.internal.OsSharedRealm)2 RealmThread (io.realm.util.RealmThread)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Test (org.junit.Test)2 AllTypes (io.realm.entities.AllTypes)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1