Merge pull request #3946 from ByteHamster/speed-up-start

Install ssl provider asynchronously
This commit is contained in:
H. Lehmann 2020-03-20 16:14:41 +01:00 committed by GitHub
commit 4da4b0e1c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 69 additions and 11 deletions

View File

@ -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());

View File

@ -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<DownloadEvent> 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<DownloadEventListener> 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);
}
}

View File

@ -28,6 +28,7 @@ import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.adapter.FeedItemlistDescriptionAdapter;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.dialog.DownloadRequestErrorDialogCreator;
import de.danoeh.antennapod.core.event.DownloadEvent;
import de.danoeh.antennapod.core.event.FeedListUpdateEvent;
@ -238,6 +239,7 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
download = Observable.fromCallable(() -> {
feeds = DBReader.getFeedList();
ClientConfig.installSslProvider(this);
downloader = new HttpDownloader(request);
downloader.call();
return downloader.getResult();

View File

@ -53,4 +53,7 @@ public class ClientConfig {
initialized = true;
}
public static void installSslProvider(Context context) {
// ProviderInstaller is a closed-source Google library
}
}

View File

@ -474,6 +474,7 @@ public class DownloadService extends Service {
@NonNull List<? extends FeedItem> itemsEnqueued) {
writeFileUrl(request);
ClientConfig.installSslProvider(this);
Downloader downloader = downloaderFactory.create(request);
if (downloader != null) {
numberOfDownloads.incrementAndGet();

View File

@ -64,7 +64,6 @@ public class ClientConfig {
} else {
Log.v(TAG, "Cast is disabled. All Cast-related initialization will be skipped.");
}
installSslProvider(context);
AntennapodHttpClient.setCacheDirectory(new File(context.getCacheDir(), "okhttp"));
SleepTimerPreferences.init(context);
RxJavaErrorHandlerSetup.setupRxJavaErrorHandler();
@ -72,7 +71,7 @@ public class ClientConfig {
initialized = true;
}
private static void installSslProvider(Context context) {
public static void installSslProvider(Context context) {
try {
ProviderInstaller.installIfNeeded(context);
} catch (GooglePlayServicesRepairableException e) {