From c336ac24d4e7df9ac35e97fb3657155b0e0189d4 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Fri, 20 Mar 2020 14:34:03 +0100 Subject: Give cancelling downloads more time The first download installs the ssl security provider and might block around 1.2 seconds longer than in previous versions. --- .../service/download/DownloadServiceTest.java | 26 ++++++++----- .../util/event/DownloadEventListener.java | 45 ++++++++++++++++++++++ 2 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 app/src/androidTest/java/de/test/antennapod/util/event/DownloadEventListener.java diff --git a/app/src/androidTest/java/de/test/antennapod/service/download/DownloadServiceTest.java b/app/src/androidTest/java/de/test/antennapod/service/download/DownloadServiceTest.java index 1ca4de1ad..229d4b307 100644 --- a/app/src/androidTest/java/de/test/antennapod/service/download/DownloadServiceTest.java +++ b/app/src/androidTest/java/de/test/antennapod/service/download/DownloadServiceTest.java @@ -16,6 +16,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.concurrent.CountDownLatch; @@ -36,6 +37,7 @@ import de.danoeh.antennapod.core.storage.DBWriter; import de.danoeh.antennapod.core.storage.DownloadRequester; import de.danoeh.antennapod.core.util.Consumer; +import static de.test.antennapod.util.event.DownloadEventListener.withDownloadEventListener; import static de.test.antennapod.util.event.FeedItemEventListener.withFeedItemEventListener; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -119,8 +121,9 @@ public class DownloadServiceTest { DBReader.getFeedMedia(testMedia11.getId()).isDownloaded()); DownloadRequester.getInstance().downloadMedia(false, InstrumentationRegistry.getTargetContext(), - testMedia11.getItem());Awaitility.await() - .atMost(1000, TimeUnit.MILLISECONDS) + testMedia11.getItem()); + Awaitility.await() + .atMost(5000, TimeUnit.MILLISECONDS) .until(() -> feedItemEventListener.getEvents().size() >= numEventsExpected); assertTrue("After media download has completed, FeedMedia object in db should indicate so.", DBReader.getFeedMedia(testMedia11.getId()).isDownloaded()); @@ -146,8 +149,8 @@ public class DownloadServiceTest { private void doTestCancelDownload_UndoEnqueue(boolean itemAlreadyInQueue) throws Exception { Context context = InstrumentationRegistry.getTargetContext(); - // let download takes longer to ensure the test can cancel the download in time - DownloadService.setDownloaderFactory(new StubDownloaderFactory(10000, downloadStatus -> { + // let download take longer to ensure the test can cancel the download in time + DownloadService.setDownloaderFactory(new StubDownloaderFactory(30000, downloadStatus -> { downloadStatus.setSuccessful(); })); UserPreferences.setEnqueueDownloadedEpisodes(true); @@ -164,11 +167,16 @@ public class DownloadServiceTest { withFeedItemEventListener(feedItemEventListener -> { DownloadRequester.getInstance().downloadMedia(false, context, testMedia11.getItem()); + withDownloadEventListener(downloadEventListener -> + Awaitility.await("download is actually running") + .atMost(5000, TimeUnit.MILLISECONDS) + .until(() -> downloadEventListener.getLatestEvent() != null + && downloadEventListener.getLatestEvent().update.mediaIds.length > 0 + && downloadEventListener.getLatestEvent().update.mediaIds[0] == testMedia11.getId())); + if (itemAlreadyInQueue) { - Awaitility.await("download service receives the request - " - + "no event is expected before cancel is issued") - .atLeast(100, TimeUnit.MILLISECONDS) - .until(() -> true); + assertEquals("download service receives the request - no event is expected before cancel is issued", + 0, feedItemEventListener.getEvents().size()); } else { Awaitility.await("item enqueue event") .atMost(2000, TimeUnit.MILLISECONDS) @@ -177,7 +185,7 @@ public class DownloadServiceTest { DownloadRequester.getInstance().cancelDownload(context, testMedia11); final int totalNumEventsExpected = itemAlreadyInQueue ? 1 : 3; Awaitility.await("item dequeue event + download termination event") - .atMost(1000, TimeUnit.MILLISECONDS) + .atMost(2000, TimeUnit.MILLISECONDS) .until(() -> feedItemEventListener.getEvents().size() >= totalNumEventsExpected); assertFalse("The download should have been canceled", DBReader.getFeedMedia(testMedia11.getId()).isDownloaded()); diff --git a/app/src/androidTest/java/de/test/antennapod/util/event/DownloadEventListener.java b/app/src/androidTest/java/de/test/antennapod/util/event/DownloadEventListener.java new file mode 100644 index 000000000..d322c1cbf --- /dev/null +++ b/app/src/androidTest/java/de/test/antennapod/util/event/DownloadEventListener.java @@ -0,0 +1,45 @@ +package de.test.antennapod.util.event; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import de.danoeh.antennapod.core.event.DownloadEvent; +import io.reactivex.functions.Consumer; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; + +import java.util.ArrayList; +import java.util.List; + +/** + * Test helper to listen to {@link DownloadEvent} and handle them accordingly. + */ +public class DownloadEventListener { + private final List events = new ArrayList<>(); + + /** + * Provides an listener subscribing to {@link DownloadEvent} that the callers can use. + * Note: it uses RxJava's version of {@link Consumer} because it allows exceptions to be thrown. + */ + public static void withDownloadEventListener(@NonNull Consumer consumer) throws Exception { + DownloadEventListener feedItemEventListener = new DownloadEventListener(); + try { + EventBus.getDefault().register(feedItemEventListener); + consumer.accept(feedItemEventListener); + } finally { + EventBus.getDefault().unregister(feedItemEventListener); + } + } + + @Subscribe + public void onEvent(DownloadEvent event) { + events.add(event); + } + + @Nullable + public DownloadEvent getLatestEvent() { + if (events.size() == 0) { + return null; + } + return events.get(events.size() - 1); + } +} -- cgit v1.2.3