Search in sources :

Example 56 with RunTestInLooperThread

use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.

the class RxJavaTests method realmResults_readableAcrossThreads.

@Test
@RunTestInLooperThread
public void realmResults_readableAcrossThreads() {
    final long TEST_SIZE = 10;
    Realm realm = looperThread.getRealm();
    realm.beginTransaction();
    for (int i = 0; i < TEST_SIZE; i++) {
        realm.createObject(AllTypes.class).setColumnLong(1);
    }
    realm.commitTransaction();
    AtomicLong startingThread = new AtomicLong(Thread.currentThread().getId());
    AtomicLong subscriberThread = new AtomicLong();
    subscription = realm.where(AllTypes.class).sort(AllTypes.FIELD_LONG).findAllAsync().asFlowable().doOnSubscribe((r) -> {
        subscriberThread.set(Thread.currentThread().getId());
    }).subscribeOn(Schedulers.io()).filter(results -> {
        assertNotEquals(startingThread.get(), subscriberThread.get());
        return results.isLoaded();
    }).map(results -> new Pair<>(results.size(), new Pair<>(results, results.first()))).observeOn(Schedulers.computation()).subscribe(pair -> {
        assertNotEquals(startingThread.get(), Thread.currentThread().getId());
        assertNotEquals(subscriberThread.get(), Thread.currentThread().getId());
        assertEquals(TEST_SIZE, pair.first.intValue());
        assertEquals(TEST_SIZE, pair.second.first.size());
        assertEquals(pair.second.second.getColumnLong(), pair.second.first.first().getColumnLong());
        disposeSuccessfulTest(realm);
    });
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) RunWith(org.junit.runner.RunWith) Dog(io.realm.entities.Dog) SystemClock(android.os.SystemClock) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) Flowable(io.reactivex.Flowable) RunInLooperThread(io.realm.rule.RunInLooperThread) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers(io.reactivex.schedulers.Schedulers) Pair(io.realm.internal.util.Pair) Assert.fail(org.junit.Assert.fail) Before(org.junit.Before) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) RealmLog(io.realm.log.RealmLog) Assert.assertNotNull(org.junit.Assert.assertNotNull) AllTypes(io.realm.entities.AllTypes) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Consumer(io.reactivex.functions.Consumer) CyclicType(io.realm.entities.CyclicType) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) AtomicLong(java.util.concurrent.atomic.AtomicLong) Disposable(io.reactivex.disposables.Disposable) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicLong(java.util.concurrent.atomic.AtomicLong) AllTypes(io.realm.entities.AllTypes) Pair(io.realm.internal.util.Pair) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 57 with RunTestInLooperThread

use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.

the class RxJavaTests method wrongGenericClassThrows.

@Test
@RunTestInLooperThread
public void wrongGenericClassThrows() {
    realm.beginTransaction();
    final AllTypes obj = realm.createObject(AllTypes.class);
    realm.commitTransaction();
    Flowable<CyclicType> obs = obj.asFlowable();
    subscription = obs.subscribe(cyclicType -> fail(), ignoredError -> {
        disposeSuccessfulTest(realm);
    });
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) RunWith(org.junit.runner.RunWith) Dog(io.realm.entities.Dog) SystemClock(android.os.SystemClock) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) Flowable(io.reactivex.Flowable) RunInLooperThread(io.realm.rule.RunInLooperThread) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers(io.reactivex.schedulers.Schedulers) Pair(io.realm.internal.util.Pair) Assert.fail(org.junit.Assert.fail) Before(org.junit.Before) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) RealmLog(io.realm.log.RealmLog) Assert.assertNotNull(org.junit.Assert.assertNotNull) AllTypes(io.realm.entities.AllTypes) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Consumer(io.reactivex.functions.Consumer) CyclicType(io.realm.entities.CyclicType) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) AtomicLong(java.util.concurrent.atomic.AtomicLong) Disposable(io.reactivex.disposables.Disposable) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertEquals(org.junit.Assert.assertEquals) AllTypes(io.realm.entities.AllTypes) CyclicType(io.realm.entities.CyclicType) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 58 with RunTestInLooperThread

use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.

the class RxJavaTests method asChangesetObservable_frozenRealmObject.

@Test
@RunTestInLooperThread
public void asChangesetObservable_frozenRealmObject() {
    realm.beginTransaction();
    final AllJavaTypes obj = realm.createObject(AllJavaTypes.class, 42);
    realm.commitTransaction();
    subscription = obj.<AllJavaTypes>freeze().<AllJavaTypes>asChangesetObservable().subscribe(change -> {
        AllJavaTypes rxObject = change.getObject();
        assertTrue(rxObject.isFrozen());
        assertNotEquals(rxObject, obj);
        assertEquals(rxObject.getFieldId(), obj.getFieldId());
        disposeSuccessfulTest(realm);
    });
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) RunWith(org.junit.runner.RunWith) Dog(io.realm.entities.Dog) SystemClock(android.os.SystemClock) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) Flowable(io.reactivex.Flowable) RunInLooperThread(io.realm.rule.RunInLooperThread) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers(io.reactivex.schedulers.Schedulers) Pair(io.realm.internal.util.Pair) Assert.fail(org.junit.Assert.fail) Before(org.junit.Before) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) RealmLog(io.realm.log.RealmLog) Assert.assertNotNull(org.junit.Assert.assertNotNull) AllTypes(io.realm.entities.AllTypes) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Consumer(io.reactivex.functions.Consumer) CyclicType(io.realm.entities.CyclicType) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) AtomicLong(java.util.concurrent.atomic.AtomicLong) Disposable(io.reactivex.disposables.Disposable) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertEquals(org.junit.Assert.assertEquals) AllJavaTypes(io.realm.entities.AllJavaTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 59 with RunTestInLooperThread

use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.

the class RxJavaTests method dynamicRealmObject_gcStressTest.

// Tests that Observables keep strong references to their parent, so they are not accidentally GC'ed while
// waiting for results from the async API's.
@Test
@RunTestInLooperThread
@SuppressWarnings("CheckReturnValue")
public void dynamicRealmObject_gcStressTest() {
    final int TEST_SIZE = 50;
    final AtomicLong innerCounter = new AtomicLong();
    final DynamicRealm dynamicRealm = DynamicRealm.getInstance(looperThread.getConfiguration());
    dynamicRealm.beginTransaction();
    for (int i = 0; i < TEST_SIZE; i++) {
        dynamicRealm.createObject(AllTypes.CLASS_NAME).set(AllTypes.FIELD_LONG, i);
    }
    dynamicRealm.commitTransaction();
    for (int i = 0; i < TEST_SIZE; i++) {
        // Doesn't keep a reference to the Observable.
        dynamicRealm.where(AllTypes.CLASS_NAME).equalTo(AllTypes.FIELD_LONG, i).findFirstAsync().<DynamicRealmObject>asFlowable().filter(obj -> obj.isLoaded()).take(// Unsubscribes from Realm.
        1).subscribe(dynamicRealmObject -> {
            // Not guaranteed, but can result in the GC of other RealmResults waiting for a result.
            Runtime.getRuntime().gc();
            if (innerCounter.incrementAndGet() == TEST_SIZE) {
                disposeSuccessfulTest(dynamicRealm);
            }
        }, throwable -> fail(throwable.toString()));
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 60 with RunTestInLooperThread

use of io.realm.rule.RunTestInLooperThread in project realm-java by realm.

the class RxJavaTests method asChangesetObservable_frozenDynamicRealmList.

@Test
@RunTestInLooperThread
public void asChangesetObservable_frozenDynamicRealmList() {
    realm.beginTransaction();
    final RealmList<Dog> list = realm.createObject(AllTypes.class).getColumnRealmList();
    list.add(new Dog("dog"));
    realm.commitTransaction();
    DynamicRealm dynamicRealm = DynamicRealm.getInstance(looperThread.getConfiguration());
    RealmList<DynamicRealmObject> frozenList = dynamicRealm.where(AllTypes.CLASS_NAME).findFirst().getList(AllTypes.FIELD_REALMLIST).freeze();
    subscription = frozenList.asChangesetObservable().subscribe(change -> {
        RealmList<DynamicRealmObject> rxList = change.getCollection();
        assertTrue(rxList.isFrozen());
        assertEquals(frozenList, rxList);
        assertNull(change.getChangeset());
        disposeSuccessfulTest(dynamicRealm);
    });
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) RunWith(org.junit.runner.RunWith) Dog(io.realm.entities.Dog) SystemClock(android.os.SystemClock) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) Flowable(io.reactivex.Flowable) RunInLooperThread(io.realm.rule.RunInLooperThread) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers(io.reactivex.schedulers.Schedulers) Pair(io.realm.internal.util.Pair) Assert.fail(org.junit.Assert.fail) Before(org.junit.Before) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) RealmLog(io.realm.log.RealmLog) Assert.assertNotNull(org.junit.Assert.assertNotNull) AllTypes(io.realm.entities.AllTypes) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Consumer(io.reactivex.functions.Consumer) CyclicType(io.realm.entities.CyclicType) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) AtomicLong(java.util.concurrent.atomic.AtomicLong) Disposable(io.reactivex.disposables.Disposable) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertEquals(org.junit.Assert.assertEquals) AllTypes(io.realm.entities.AllTypes) Dog(io.realm.entities.Dog) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Aggregations

RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)180 Test (org.junit.Test)180 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)80 AllTypes (io.realm.entities.AllTypes)76 Dog (io.realm.entities.Dog)59 RunInLooperThread (io.realm.rule.RunInLooperThread)30 AllJavaTypes (io.realm.entities.AllJavaTypes)29 AtomicLong (java.util.concurrent.atomic.AtomicLong)23 AndroidJUnit4 (androidx.test.ext.junit.runners.AndroidJUnit4)21 RealmLog (io.realm.log.RealmLog)21 Assert.assertEquals (org.junit.Assert.assertEquals)21 Assert.assertFalse (org.junit.Assert.assertFalse)21 Assert.assertNotNull (org.junit.Assert.assertNotNull)21 Assert.assertTrue (org.junit.Assert.assertTrue)21 Before (org.junit.Before)21 Rule (org.junit.Rule)21 RunWith (org.junit.runner.RunWith)21 Consumer (io.reactivex.functions.Consumer)20 Assert.fail (org.junit.Assert.fail)20 SystemClock (android.os.SystemClock)19