Search in sources :

Example 1 with RunTestWithRemoteService

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

the class ProcessCommitTests method expectSimpleCommit.

// 1. Open a sync Realm and listen to changes.
// A. Open the same sync Realm and add one object.
// 2. Get the notification, check if the change in A is received.
@Test
@RunTestInLooperThread
@RunTestWithRemoteService(remoteService = SimpleCommitRemoteService.class, onLooperThread = true)
public void expectSimpleCommit() {
    looperThread.runAfterTest(remoteService.afterRunnable);
    remoteService.createHandler(Looper.myLooper());
    final SyncUser user = UserFactory.getInstance().createDefaultUser(Constants.AUTH_URL);
    String realmUrl = Constants.SYNC_SERVER_URL;
    final SyncConfiguration syncConfig = user.createConfiguration(realmUrl).modules(new ProcessCommitTestsModule()).directory(looperThread.getRoot()).build();
    final Realm realm = Realm.getInstance(syncConfig);
    final RealmResults<ProcessInfo> all = realm.where(ProcessInfo.class).findAll();
    looperThread.keepStrongReference(all);
    all.addChangeListener(new RealmChangeListener<RealmResults<ProcessInfo>>() {

        @Override
        public void onChange(RealmResults<ProcessInfo> element) {
            assertEquals(1, all.size());
            assertEquals("Background_Process1", all.get(0).getName());
            realm.close();
            user.logOut();
            remoteService.triggerServiceStep(SimpleCommitRemoteService.stepB_closeRealmAndLogOut);
            looperThread.testComplete();
        }
    });
    remoteService.triggerServiceStep(SimpleCommitRemoteService.stepA_openRealmAndCreateOneObject);
}
Also used : SyncUser(io.realm.SyncUser) ProcessInfo(io.realm.objectserver.model.ProcessInfo) Realm(io.realm.Realm) SyncConfiguration(io.realm.SyncConfiguration) RealmResults(io.realm.RealmResults) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) RunTestWithRemoteService(io.realm.rule.RunTestWithRemoteService) Test(org.junit.Test) StandardIntegrationTest(io.realm.StandardIntegrationTest)

Example 2 with RunTestWithRemoteService

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

the class ProcessCommitTests method expectALot.

// 1. Open a sync Realm and listen to changes.
// A. Open the same sync Realm.
// B. Create 100 objects.
// 2. Check if the 100 objects are received.
// #. Repeat B/2 10 times.
@Test
@RunTestWithRemoteService(remoteService = ALotCommitsRemoteService.class, onLooperThread = true)
@RunTestInLooperThread
public void expectALot() throws Throwable {
    looperThread.runAfterTest(remoteService.afterRunnable);
    remoteService.createHandler(Looper.myLooper());
    final SyncUser user = UserFactory.getInstance().createDefaultUser(Constants.AUTH_URL);
    String realmUrl = Constants.SYNC_SERVER_URL;
    final SyncConfiguration syncConfig = user.createConfiguration(realmUrl).modules(new ProcessCommitTestsModule()).directory(looperThread.getRoot()).build();
    final Realm realm = Realm.getInstance(syncConfig);
    final RealmResults<TestObject> all = realm.where(TestObject.class).sort("intProp").findAll();
    looperThread.keepStrongReference(all);
    final AtomicInteger listenerCalledCounter = new AtomicInteger(0);
    all.addChangeListener(new RealmChangeListener<RealmResults<TestObject>>() {

        @Override
        public void onChange(RealmResults<TestObject> element) {
            int counter = listenerCalledCounter.incrementAndGet();
            int size = all.size();
            if (size == 0) {
                listenerCalledCounter.decrementAndGet();
                return;
            }
            // Added 100 objects every time.
            assertEquals(0, size % 100);
            assertEquals(counter * 100 - 1, all.last().getIntProp());
            assertEquals("Str" + (counter * 100 - 1), all.last().getStringProp());
            if (counter == 10) {
                remoteService.triggerServiceStep(ALotCommitsRemoteService.stepC_closeRealm);
                realm.close();
                user.logOut();
                looperThread.testComplete();
            } else {
                remoteService.triggerServiceStep(ALotCommitsRemoteService.stepB_createObjects);
            }
        }
    });
    remoteService.triggerServiceStep(ALotCommitsRemoteService.stepA_openRealm);
    remoteService.triggerServiceStep(ALotCommitsRemoteService.stepB_createObjects);
}
Also used : SyncUser(io.realm.SyncUser) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestObject(io.realm.objectserver.model.TestObject) Realm(io.realm.Realm) SyncConfiguration(io.realm.SyncConfiguration) RealmResults(io.realm.RealmResults) RunTestWithRemoteService(io.realm.rule.RunTestWithRemoteService) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test) StandardIntegrationTest(io.realm.StandardIntegrationTest)

Aggregations

Realm (io.realm.Realm)2 RealmResults (io.realm.RealmResults)2 StandardIntegrationTest (io.realm.StandardIntegrationTest)2 SyncConfiguration (io.realm.SyncConfiguration)2 SyncUser (io.realm.SyncUser)2 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)2 RunTestWithRemoteService (io.realm.rule.RunTestWithRemoteService)2 Test (org.junit.Test)2 ProcessInfo (io.realm.objectserver.model.ProcessInfo)1 TestObject (io.realm.objectserver.model.TestObject)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1