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);
});
}
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);
});
}
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);
});
}
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()));
}
}
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);
});
}
Aggregations