Skip to content

Commit

Permalink
Merge pull request #126 from stoyicker:cap_concurrent_remove_tasks
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 463792127
  • Loading branch information
marcbaechinger committed Oct 19, 2022
2 parents bac323d + 28e3207 commit 3b5a53f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
Expand Up @@ -707,6 +707,7 @@ private static final class InternalHandler extends Handler {
private int maxParallelDownloads;
private int minRetryCount;
private int activeDownloadTaskCount;
private boolean hasActiveRemoveTask;

public InternalHandler(
HandlerThread thread,
Expand Down Expand Up @@ -1058,6 +1059,10 @@ private void syncRemovingDownload(@Nullable Task activeTask, Download download)
return;
}

if (hasActiveRemoveTask) {
return;
}

// We can start a remove task.
Downloader downloader = downloaderFactory.createDownloader(download.request);
activeTask =
Expand All @@ -1069,6 +1074,7 @@ private void syncRemovingDownload(@Nullable Task activeTask, Download download)
minRetryCount,
/* internalHandler= */ this);
activeTasks.put(download.request.id, activeTask);
hasActiveRemoveTask = true;
activeTask.start();
}

Expand Down Expand Up @@ -1098,7 +1104,9 @@ private void onTaskStopped(Task task) {
activeTasks.remove(downloadId);

boolean isRemove = task.isRemove;
if (!isRemove && --activeDownloadTaskCount == 0) {
if (isRemove) {
hasActiveRemoveTask = false;
} else if (--activeDownloadTaskCount == 0) {
removeMessages(MSG_UPDATE_PROGRESS);
}

Expand Down
Expand Up @@ -283,14 +283,14 @@ public void removeAllDownloads_removesAllDownloads() throws Throwable {
postRemoveAllRequest();
// Both downloads should be removed.
FakeDownloader downloader2 = getDownloaderAt(2);
FakeDownloader downloader3 = getDownloaderAt(3);
downloader2.assertId(ID1);
downloader3.assertId(ID2);
downloader2.assertRemoveStarted();
downloader3.assertRemoveStarted();
downloader2.finish();
downloader3.finish();
assertRemoved(ID1);
FakeDownloader downloader3 = getDownloaderAt(3);
downloader3.assertId(ID2);
downloader3.assertRemoveStarted();
downloader3.finish();
assertRemoved(ID2);

downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
Expand Down Expand Up @@ -712,6 +712,36 @@ public void mergeRequest_completedWithStopReason_becomesStopped() {
assertEqualIgnoringUpdateTime(mergedDownload, expectedDownload);
}

@Test
public void removeRequests_runSequentially() throws Throwable {
// Trigger two remove requests.
postDownloadRequest(ID1);
getDownloaderAt(0).finish();
postDownloadRequest(ID2);
getDownloaderAt(1).finish();
postRemoveRequest(ID1);
postRemoveRequest(ID2);

// Assert first remove request is executing, second one is queued.
assertRemoving(ID1);
assertQueued(ID2);
FakeDownloader downloader2 = getDownloaderAt(2);
downloader2.assertId(ID1);
downloader2.assertRemoveStarted();
downloader2.finish();
assertRemoved(ID1);

// Assert second one is running after first one finished
assertRemoving(ID2);
FakeDownloader downloader3 = getDownloaderAt(3);
downloader3.assertId(ID2);
downloader3.assertRemoveStarted();
downloader3.finish();
assertRemoved(ID2);

downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
}

private void setupDownloadManager(int maxParallelDownloads) throws Exception {
if (downloadManager != null) {
releaseDownloadManager();
Expand Down

0 comments on commit 3b5a53f

Please sign in to comment.