Source:
<code>public void downloadProgressListener_indefinitely() throws InterruptedException { final AtomicInteger transferCompleted = new AtomicInteger(0); final CountDownLatch allChangesDownloaded = new CountDownLatch(1); final CountDownLatch startWorker = new CountDownLatch(1); final SyncUser userWithData = UserFactory.createUniqueUser(Constants.AUTH_URL); URI serverUrl = createRemoteData(userWithData); Thread worker = new Thread(new Runnable() { @Override public void run() { TestHelper.awaitOrFail(startWorker); createRemoteData(userWithData); } }); worker.start(); SyncUser adminUser = UserFactory.createAdminUser(Constants.AUTH_URL); final SyncConfiguration adminConfig = configFactory.createSyncConfigurationBuilder(adminUser, serverUrl.toString()).build(); Realm adminRealm = Realm.getInstance(adminConfig); Realm userRealm = Realm.getInstance(configFactory.createSyncConfigurationBuilder(userWithData, Constants.USER_REALM).build()); SyncSession session = SyncManager.getSession(adminConfig); session.addDownloadProgressListener(ProgressMode.INDEFINITELY, new ProgressListener() { @Override public void onChange(Progress progress) { if (progress.isTransferComplete()) { switch (transferCompleted.incrementAndGet()) { case 1: assertTransferComplete(progress, false); break; case 2: { assertTransferComplete(progress, true); Realm adminRealm = Realm.getInstance(adminConfig); assertEquals(TEST_SIZE, adminRealm.where(AllTypes.class).count()); adminRealm.close(); startWorker.countDown(); break; } case 3: { assertTransferComplete(progress, true); Realm adminRealm = Realm.getInstance(adminConfig); assertEquals(TEST_SIZE * 2, adminRealm.where(AllTypes.class).count()); adminRealm.close(); allChangesDownloaded.countDown(); break; } default: fail(); } } } }); TestHelper.awaitOrFail(allChangesDownloaded); adminRealm.close(); <START> userRealm.close(); <END> userWithData.logout(); adminUser.logout(); }</code><technical_language>concerns a lot this is a problem. This a problem if progress listeners do work case 2 is triggered. fixed in this PR, please add a FIXME instead remember follow it</technical_language>

Target:
public void downloadProgressListener_indefinitely() throws InterruptedException { final AtomicInteger transferCompleted = new AtomicInteger(0); final CountDownLatch allChangesDownloaded = new CountDownLatch(1); final CountDownLatch startWorker = new CountDownLatch(1); final SyncUser userWithData = UserFactory.createUniqueUser(Constants.AUTH_URL); URI serverUrl = createRemoteData(userWithData); Thread worker = new Thread(new Runnable() { @Override public void run() { TestHelper.awaitOrFail(startWorker); createRemoteData(userWithData); } }); worker.start(); SyncUser adminUser = UserFactory.createAdminUser(Constants.AUTH_URL); final SyncConfiguration adminConfig = configFactory.createSyncConfigurationBuilder(adminUser, serverUrl.toString()).build(); Realm adminRealm = Realm.getInstance(adminConfig); Realm userRealm = Realm.getInstance(configFactory.createSyncConfigurationBuilder(userWithData, Constants.USER_REALM).build()); SyncSession session = SyncManager.getSession(adminConfig); session.addDownloadProgressListener(ProgressMode.INDEFINITELY, new ProgressListener() { @Override public void onChange(Progress progress) { if (progress.isTransferComplete()) { switch (transferCompleted.incrementAndGet()) { case 1: assertTransferComplete(progress, false); break; case 2: { assertTransferComplete(progress, true); Realm adminRealm = Realm.getInstance(adminConfig); assertEquals(TEST_SIZE, adminRealm.where(AllTypes.class).count()); adminRealm.close(); startWorker.countDown(); break; } case 3: { assertTransferComplete(progress, true); Realm adminRealm = Realm.getInstance(adminConfig); assertEquals(TEST_SIZE * 2, adminRealm.where(AllTypes.class).count()); adminRealm.close(); allChangesDownloaded.countDown(); break; } default: fail("Transfer complete called too many times:" + transferCompleted.get()); } } } }); TestHelper.awaitOrFail(allChangesDownloaded); adminRealm.close(); userRealm.close(); userWithData.logout(); adminUser.logout(); worker.join(); }