Search in sources :

Example 31 with AllTypes

use of io.realm.entities.AllTypes in project realm-java by realm.

the class RealmAsyncQueryTests method batchUpdate_localRefIsDeletedInLoopOfNativeBatchUpdateQueries.

// This test reproduces the issue in https://secure.helpscout.net/conversation/244053233/6163/?folderId=366141
// First it creates 512 async queries, then trigger a transaction to make the queries gets update with
// nativeBatchUpdateQueries. It should not exceed the limits of local ref map size in JNI.
// NOTE: This test is not checking the same thing after the OS results integration. Just keep it for an additional
// test for async.
@Test
@RunTestInLooperThread
public void batchUpdate_localRefIsDeletedInLoopOfNativeBatchUpdateQueries() {
    final Realm realm = looperThread.realm;
    // For Android, the size of local ref map is 512. Uses 1024 for more pressure.
    final int TEST_COUNT = 1024;
    final AtomicBoolean updatesTriggered = new AtomicBoolean(false);
    // The first time onChange gets called for every results.
    final AtomicInteger firstOnChangeCounter = new AtomicInteger(0);
    // The second time onChange gets called for every results which is triggered by the transaction.
    final AtomicInteger secondOnChangeCounter = new AtomicInteger(0);
    final RealmChangeListener<RealmResults<AllTypes>> listener = new RealmChangeListener<RealmResults<AllTypes>>() {

        @Override
        public void onChange(RealmResults<AllTypes> element) {
            if (updatesTriggered.get()) {
                // Step 4: Test finished after all results's onChange gets called the 2nd time.
                int count = secondOnChangeCounter.addAndGet(1);
                if (count == TEST_COUNT) {
                    realm.removeAllChangeListeners();
                    looperThread.testComplete();
                }
            } else {
                int count = firstOnChangeCounter.addAndGet(1);
                if (count == TEST_COUNT) {
                    // Step 3: Commits the transaction to trigger queries updates.
                    updatesTriggered.set(true);
                    realm.executeTransactionAsync(new Realm.Transaction() {

                        @Override
                        public void execute(Realm realm) {
                            realm.createObject(AllTypes.class);
                        }
                    });
                } else {
                    // Step 2: Creates 2nd - TEST_COUNT queries.
                    RealmResults<AllTypes> results = realm.where(AllTypes.class).findAllAsync();
                    results.addChangeListener(this);
                    looperThread.keepStrongReference.add(results);
                }
            }
        }
    };
    // Step 1. Creates first async to kick the test start.
    RealmResults<AllTypes> results = realm.where(AllTypes.class).findAllAsync();
    results.addChangeListener(listener);
    looperThread.keepStrongReference.add(results);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AllTypes(io.realm.entities.AllTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 32 with AllTypes

use of io.realm.entities.AllTypes in project realm-java by realm.

the class RealmAsyncQueryTests method accessingRealmListOnUnloadedRealmObjectShouldThrow.

@Test
@RunTestInLooperThread
public void accessingRealmListOnUnloadedRealmObjectShouldThrow() {
    Realm realm = looperThread.realm;
    populateTestRealm(realm, 10);
    final AllTypes results = realm.where(AllTypes.class).equalTo("columnLong", 0).findFirstAsync();
    assertFalse(results.isLoaded());
    try {
        results.getColumnRealmList();
        fail("Accessing property on an empty row");
    } catch (IllegalStateException ignored) {
    }
    looperThread.testComplete();
}
Also used : AllTypes(io.realm.entities.AllTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 33 with AllTypes

use of io.realm.entities.AllTypes in project realm-java by realm.

the class RealmAsyncQueryTests method findFirstAsync_withNotification.

// Finds elements [0-4] asynchronously then waits for the promise to be loaded
// using a callback to be notified when the data is loaded.
@Test
@RunTestInLooperThread
public void findFirstAsync_withNotification() throws Throwable {
    Realm realm = looperThread.realm;
    populateTestRealm(realm, 10);
    final AllTypes realmResults = realm.where(AllTypes.class).between("columnLong", 4, 9).findFirstAsync();
    looperThread.keepStrongReference.add(realmResults);
    realmResults.addChangeListener(new RealmChangeListener<AllTypes>() {

        @Override
        public void onChange(AllTypes object) {
            assertTrue(realmResults.isLoaded());
            assertTrue(realmResults.isValid());
            assertEquals("test data 4", realmResults.getColumnString());
            looperThread.testComplete();
        }
    });
    assertFalse(realmResults.isLoaded());
    assertFalse(realmResults.isValid());
    try {
        realmResults.setColumnString("should fail");
        fail("Accessing an unloaded object should throw");
    } catch (IllegalStateException ignored) {
    }
}
Also used : AllTypes(io.realm.entities.AllTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 34 with AllTypes

use of io.realm.entities.AllTypes in project realm-java by realm.

the class RealmAsyncQueryTests method stressTestBackgroundCommits.

// Keeps advancing the Realm by sending 1 commit for each frame (16ms).
// The async queries should keep up with the modification.
@Test
@RunTestInLooperThread
public void stressTestBackgroundCommits() throws Throwable {
    final int NUMBER_OF_COMMITS = 100;
    final CountDownLatch bgRealmClosed = new CountDownLatch(1);
    final long[] latestLongValue = new long[1];
    final float[] latestFloatValue = new float[1];
    // Starts a background thread that pushes a commit every 16ms.
    final Thread backgroundThread = new Thread() {

        @Override
        public void run() {
            Random random = new Random(System.currentTimeMillis());
            Realm backgroundThreadRealm = Realm.getInstance(looperThread.realm.getConfiguration());
            for (int i = 0; i < NUMBER_OF_COMMITS; i++) {
                backgroundThreadRealm.beginTransaction();
                AllTypes object = backgroundThreadRealm.createObject(AllTypes.class);
                latestLongValue[0] = random.nextInt(100);
                latestFloatValue[0] = random.nextFloat();
                object.setColumnFloat(latestFloatValue[0]);
                object.setColumnLong(latestLongValue[0]);
                backgroundThreadRealm.commitTransaction();
                // Waits 16ms. Before adding the next commit.
                SystemClock.sleep(16);
            }
            backgroundThreadRealm.close();
            bgRealmClosed.countDown();
        }
    };
    final RealmResults<AllTypes> allAsync = looperThread.realm.where(AllTypes.class).findAllAsync();
    allAsync.addChangeListener(new RealmChangeListener<RealmResults<AllTypes>>() {

        @Override
        public void onChange(RealmResults<AllTypes> object) {
            assertTrue(allAsync.isLoaded());
            if (allAsync.size() == NUMBER_OF_COMMITS) {
                AllTypes lastInserted = looperThread.realm.where(AllTypes.class).equalTo("columnLong", latestLongValue[0]).equalTo("columnFloat", latestFloatValue[0]).findFirst();
                assertNotNull(lastInserted);
                TestHelper.awaitOrFail(bgRealmClosed);
                looperThread.testComplete();
            }
        }
    });
    looperThread.keepStrongReference.add(allAsync);
    looperThread.postRunnableDelayed(new Runnable() {

        @Override
        public void run() {
            backgroundThread.start();
        }
    }, 16);
}
Also used : AllTypes(io.realm.entities.AllTypes) CountDownLatch(java.util.concurrent.CountDownLatch) RunInLooperThread(io.realm.rule.RunInLooperThread) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Random(java.util.Random) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 35 with AllTypes

use of io.realm.entities.AllTypes in project realm-java by realm.

the class RealmAsyncQueryTests method findFirstAsync_updatedIfsyncRealmObjectIsUpdated.

@Test
@RunTestInLooperThread
public void findFirstAsync_updatedIfsyncRealmObjectIsUpdated() throws Throwable {
    populateTestRealm(looperThread.realm, 1);
    AllTypes firstSync = looperThread.realm.where(AllTypes.class).findFirst();
    assertEquals(0, firstSync.getColumnLong());
    assertEquals("test data 0", firstSync.getColumnString());
    final AllTypes firstAsync = looperThread.realm.where(AllTypes.class).findFirstAsync();
    assertTrue(firstAsync.load());
    assertTrue(firstAsync.isLoaded());
    assertTrue(firstAsync.isValid());
    assertEquals(0, firstAsync.getColumnLong());
    assertEquals("test data 0", firstAsync.getColumnString());
    looperThread.keepStrongReference.add(firstAsync);
    firstAsync.addChangeListener(new RealmChangeListener<AllTypes>() {

        @Override
        public void onChange(AllTypes object) {
            assertEquals("Galacticon", firstAsync.getColumnString());
            looperThread.testComplete();
        }
    });
    looperThread.realm.beginTransaction();
    firstSync.setColumnString("Galacticon");
    looperThread.realm.commitTransaction();
}
Also used : AllTypes(io.realm.entities.AllTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Aggregations

AllTypes (io.realm.entities.AllTypes)134 Test (org.junit.Test)122 UiThreadTest (android.support.test.annotation.UiThreadTest)40 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)33 Date (java.util.Date)27 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 Action1 (rx.functions.Action1)12 JSONObject (org.json.JSONObject)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 Dog (io.realm.entities.Dog)8 InputStream (java.io.InputStream)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 NonLatinFieldNames (io.realm.entities.NonLatinFieldNames)5 SharedRealm (io.realm.internal.SharedRealm)5 RealmException (io.realm.exceptions.RealmException)4 RunInLooperThread (io.realm.rule.RunInLooperThread)3 IOException (java.io.IOException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ExecutionException (java.util.concurrent.ExecutionException)3 JSONException (org.json.JSONException)3