Search in sources :

Example 1 with ProcessInfo

use of io.realm.objectserver.model.ProcessInfo in project realm-java by realm.

the class SendOneCommit method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    Realm.init(getApplicationContext());
    SyncUser user = UserFactory.createDefaultUser(Constants.AUTH_URL);
    String realmUrl = Constants.SYNC_SERVER_URL;
    final SyncConfiguration syncConfig = new SyncConfiguration.Builder(user, realmUrl).name(SendOneCommit.class.getSimpleName()).build();
    Realm.deleteRealm(syncConfig);
    Realm realm = Realm.getInstance(syncConfig);
    realm.beginTransaction();
    ProcessInfo processInfo = realm.createObject(ProcessInfo.class);
    processInfo.setName("Background_Process1");
    processInfo.setPid(android.os.Process.myPid());
    processInfo.setThreadId(Thread.currentThread().getId());
    realm.commitTransaction();
    //FIXME the close may not give a chance to the sync client to process/upload the changeset
    realm.close();
}
Also used : SyncUser(io.realm.SyncUser) ProcessInfo(io.realm.objectserver.model.ProcessInfo) Realm(io.realm.Realm) SyncConfiguration(io.realm.SyncConfiguration)

Example 2 with ProcessInfo

use of io.realm.objectserver.model.ProcessInfo in project realm-java by realm.

the class ProcessCommitTests method expectServerCommit.

// FIXME: Ignore for now. They do still not work. It might be caused by two processes each creating
// a Sync Client, but it needs to be investigated.
@Test
@Ignore
public void expectServerCommit() throws Throwable {
    final Throwable[] exception = new Throwable[1];
    final CountDownLatch testFinished = new CountDownLatch(1);
    ExecutorService service = Executors.newSingleThreadExecutor();
    service.submit(new Runnable() {

        @Override
        public void run() {
            try {
                Looper.prepare();
                Context targetContext = InstrumentationRegistry.getTargetContext();
                SyncUser user = UserFactory.createDefaultUser(Constants.AUTH_URL);
                String realmUrl = Constants.SYNC_SERVER_URL;
                final SyncConfiguration syncConfig = new SyncConfiguration.Builder(user, realmUrl).name(SendOneCommit.class.getSimpleName()).errorHandler(new SyncSession.ErrorHandler() {

                    @Override
                    public void onError(SyncSession session, ObjectServerError error) {
                        fail("Sync failure: " + error);
                    }

                    @Override
                    public void onClientResetRequired(SyncSession session, ClientResetHandler handler) {
                        fail("Client Reset");
                    }
                }).build();
                //TODO do this in Rule as async tests
                Realm.deleteRealm(syncConfig);
                final Realm realm = Realm.getInstance(syncConfig);
                Intent intent = new Intent(targetContext, SendOneCommit.class);
                targetContext.startService(intent);
                final RealmResults<ProcessInfo> all = realm.where(ProcessInfo.class).findAll();
                all.addChangeListener(new RealmChangeListener<RealmResults<ProcessInfo>>() {

                    @Override
                    public void onChange(RealmResults<ProcessInfo> element) {
                        assertEquals(1, all.size());
                        assertEquals("Background_Process1", all.get(0).getName());
                        testFinished.countDown();
                    }
                });
                Looper.loop();
            } catch (Throwable e) {
                exception[0] = e;
                testFinished.countDown();
            }
        }
    });
    boolean testTimedOut = testFinished.await(300, TimeUnit.SECONDS);
    if (exception[0] != null) {
        throw exception[0];
    } else if (!testTimedOut) {
        fail("Test timed out ");
    }
}
Also used : Context(android.content.Context) RealmChangeListener(io.realm.RealmChangeListener) SyncUser(io.realm.SyncUser) Intent(android.content.Intent) SendOneCommit(io.realm.objectserver.service.SendOneCommit) ProcessInfo(io.realm.objectserver.model.ProcessInfo) CountDownLatch(java.util.concurrent.CountDownLatch) ObjectServerError(io.realm.ObjectServerError) ClientResetHandler(io.realm.ClientResetHandler) ExecutorService(java.util.concurrent.ExecutorService) SyncSession(io.realm.SyncSession) Realm(io.realm.Realm) SyncConfiguration(io.realm.SyncConfiguration) RealmResults(io.realm.RealmResults) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Realm (io.realm.Realm)2 SyncConfiguration (io.realm.SyncConfiguration)2 SyncUser (io.realm.SyncUser)2 ProcessInfo (io.realm.objectserver.model.ProcessInfo)2 Context (android.content.Context)1 Intent (android.content.Intent)1 ClientResetHandler (io.realm.ClientResetHandler)1 ObjectServerError (io.realm.ObjectServerError)1 RealmChangeListener (io.realm.RealmChangeListener)1 RealmResults (io.realm.RealmResults)1 SyncSession (io.realm.SyncSession)1 SendOneCommit (io.realm.objectserver.service.SendOneCommit)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1