Source:
public void addChangeListener_bug5507() throws InterruptedException { if (type == ObservablesType.REALM_RESULTS) looperThread.testComplete(); Realm realm = looperThread.getRealm(); populateData(realm, 1); OrderedRealmCollectionChangeListener<OrderedRealmCollection<Dog>> listener1 = new OrderedRealmCollectionChangeListener<OrderedRealmCollection<Dog>>() { @Override public void onChange(OrderedRealmCollection<Dog> dogs, @Nullable OrderedCollectionChangeSet changeSet) { fail(); } }; OrderedRealmCollection<Dog> dogs = getTestingCollection(realm); assertEquals(1, dogs.size()); if (type == ObservablesType.REALM_LIST) { ((RealmList) dogs).addChangeListener(listener1); } else { ((RealmResults) dogs).addChangeListener(listener1); } Thread bgThread = new Thread(new Runnable() { @Override public void run() { Realm realm = Realm.getInstance(looperThread.getConfiguration()); realm.beginTransaction(); createObjects(realm, 2); realm.commitTransaction(); realm.close(); } }); bgThread.start(); bgThread.join(); if (type == ObservablesType.REALM_LIST) { ((RealmList) dogs).removeChangeListener(listener1); } else { ((RealmResults) dogs).removeChangeListener(listener1); } OrderedRealmCollectionChangeListener<OrderedRealmCollection<Dog>> listener2 = new OrderedRealmCollectionChangeListener<OrderedRealmCollection<Dog>>() { @Override public void onChange(OrderedRealmCollection<Dog> dogs, @Nullable OrderedCollectionChangeSet changeSet) { assertEquals(2, dogs.size()); looperThread.testComplete(); } }; if (type == ObservablesType.REALM_LIST) { ((RealmList) dogs).addChangeListener(listener2); } else { ((RealmResults) dogs).addChangeListener(listener2); } }

Target:
public void addChangeListener_bug5507() throws InterruptedException { if (type == ObservablesType.REALM_RESULTS) { looperThread.testComplete(); return; } Realm realm = looperThread.getRealm(); populateData(realm, 1); OrderedRealmCollectionChangeListener<OrderedRealmCollection<Dog>> listener1 = new OrderedRealmCollectionChangeListener<OrderedRealmCollection<Dog>>() { @Override public void onChange(OrderedRealmCollection<Dog> dogs, @Nullable OrderedCollectionChangeSet changeSet) { fail(); } }; OrderedRealmCollection<Dog> dogs = getTestingCollection(realm); assertEquals(1, dogs.size()); if (type == ObservablesType.REALM_LIST) { ((RealmList) dogs).addChangeListener(listener1); } else { ((RealmResults) dogs).addChangeListener(listener1); } Thread bgThread = new Thread(new Runnable() { @Override public void run() { Realm realm = Realm.getInstance(looperThread.getConfiguration()); realm.beginTransaction(); createObjects(realm, 2); realm.commitTransaction(); realm.close(); } }); bgThread.start(); bgThread.join(); if (type == ObservablesType.REALM_LIST) { ((RealmList) dogs).removeChangeListener(listener1); } else { ((RealmResults) dogs).removeChangeListener(listener1); } OrderedRealmCollectionChangeListener<OrderedRealmCollection<Dog>> listener2 = new OrderedRealmCollectionChangeListener<OrderedRealmCollection<Dog>>() { @Override public void onChange(OrderedRealmCollection<Dog> dogs, @Nullable OrderedCollectionChangeSet changeSet) { assertEquals(2, dogs.size()); looperThread.testComplete(); } }; if (type == ObservablesType.REALM_LIST) { ((RealmList) dogs).addChangeListener(listener2); } else { ((RealmResults) dogs).addChangeListener(listener2); } }