Skip to content

Commit

Permalink
feat: support cancel task just used its id
Browse files Browse the repository at this point in the history
closes #30
  • Loading branch information
Jacksgong committed Apr 15, 2018
1 parent 9e91272 commit 405cb2d
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,15 @@ public DownloadTask build() {
+ "/" + filenameHolder.get();
}

/**
* Create a Identified task only for compare with their id, and only the id is the same.
*
* @param id the id is set for this mock task.
*/
public static MockTaskForCompare mockTaskForCompare(int id) {
return new MockTaskForCompare(id);
}

@NonNull public MockTaskForCompare mock(int id) {
return new MockTaskForCompare(id, this);
}
Expand Down Expand Up @@ -891,6 +900,14 @@ public static class MockTaskForCompare extends IdentifiedTask {
@Nullable final String filename;
@NonNull final File parentFile;

public MockTaskForCompare(int id) {
this.id = id;
this.url = EMPTY_URL;
this.providedPathFile = EMPTY_FILE;
this.filename = null;
this.parentFile = EMPTY_FILE;
}

public MockTaskForCompare(int id, @NonNull DownloadTask task) {
this.id = id;
this.url = task.url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

public abstract class IdentifiedTask {

public static final String EMPTY_URL = "";
public static final File EMPTY_FILE = new File("");

public abstract int getId();

@NonNull public abstract String getUrl();
Expand All @@ -36,6 +39,8 @@ public abstract class IdentifiedTask {
public boolean compareIgnoreId(IdentifiedTask another) {
if (!getUrl().equals(another.getUrl())) return false;

if (getUrl().equals(EMPTY_URL) || getParentFile().equals(EMPTY_FILE)) return false;

if (getProvidedPathFile().equals(another.getProvidedPathFile())) return true;

if (!getParentFile().equals(another.getParentFile())) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.liulishuo.okdownload.DownloadTask;
import com.liulishuo.okdownload.OkDownload;
import com.liulishuo.okdownload.StatusUtil;
import com.liulishuo.okdownload.core.IdentifiedTask;
import com.liulishuo.okdownload.core.Util;
import com.liulishuo.okdownload.core.breakpoint.DownloadStore;
import com.liulishuo.okdownload.core.cause.EndCause;
Expand Down Expand Up @@ -188,29 +189,37 @@ public void cancelAll() {
skipProceedCallCount.decrementAndGet();
}

public void cancel(DownloadTask[] tasks) {
public void cancel(IdentifiedTask[] tasks) {
skipProceedCallCount.incrementAndGet();
cancelLocked(tasks);
skipProceedCallCount.decrementAndGet();
processCalls();
}

public boolean cancel(DownloadTask task) {
public boolean cancel(IdentifiedTask task) {
skipProceedCallCount.incrementAndGet();
final boolean result = cancelLocked(task);
skipProceedCallCount.decrementAndGet();
processCalls();
return result;
}

private synchronized void cancelLocked(DownloadTask[] tasks) {
public boolean cancel(int id) {
skipProceedCallCount.incrementAndGet();
final boolean result = cancelLocked(DownloadTask.mockTaskForCompare(id));
skipProceedCallCount.decrementAndGet();
processCalls();
return result;
}

private synchronized void cancelLocked(IdentifiedTask[] tasks) {
final long startCancelTime = SystemClock.uptimeMillis();
Util.d(TAG, "start cancel bunch task manually: " + tasks.length);

final List<DownloadCall> needCallbackCalls = new ArrayList<>();
final List<DownloadCall> needCancelCalls = new ArrayList<>();
try {
for (DownloadTask task : tasks) {
for (IdentifiedTask task : tasks) {
filterCanceledCalls(task, needCallbackCalls, needCancelCalls);
}
} finally {
Expand All @@ -221,7 +230,7 @@ private synchronized void cancelLocked(DownloadTask[] tasks) {
}
}

private synchronized boolean cancelLocked(DownloadTask task) {
synchronized boolean cancelLocked(IdentifiedTask task) {
Util.d(TAG, "cancel manually: " + task.getId());
final List<DownloadCall> needCallbackCalls = new ArrayList<>();
final List<DownloadCall> needCancelCalls = new ArrayList<>();
Expand All @@ -235,7 +244,7 @@ private synchronized boolean cancelLocked(DownloadTask task) {
return needCallbackCalls.size() > 0 || needCancelCalls.size() > 0;
}

private synchronized void filterCanceledCalls(@NonNull DownloadTask task,
private synchronized void filterCanceledCalls(@NonNull IdentifiedTask task,
@NonNull List<DownloadCall> needCallbackCalls,
@NonNull List<DownloadCall> needCancelCalls) {
for (Iterator<DownloadCall> i = readyAsyncCalls.iterator(); i.hasNext(); ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,5 +539,14 @@ public void taskToString() {
+ new File(parentPath, filename).getAbsolutePath());
}

@Test
public void mockTaskForCompare_justId() {
final IdentifiedTask task = DownloadTask.mockTaskForCompare(1);
assertThat(task.getId()).isEqualTo(1);
assertThat(task.getUrl()).isEqualTo(IdentifiedTask.EMPTY_URL);
assertThat(task.getFilename()).isNull();
assertThat(task.getParentFile()).isEqualTo(IdentifiedTask.EMPTY_FILE);
}

@Rule public ExpectedException thrown = ExpectedException.none();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.liulishuo.okdownload.DownloadTask;

import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -126,4 +128,13 @@ public void compareIgnoreId_filename() {
when(another.getFilename()).thenReturn(filename);
assertThat(task.compareIgnoreId(another)).isTrue();
}

@Test
public void compareIgnoreId_falseEmpty() {
final IdentifiedTask task = DownloadTask.mockTaskForCompare(1);
final IdentifiedTask anotherTask = DownloadTask.mockTaskForCompare(2);

assertThat(task.compareIgnoreId(task)).isFalse();
assertThat(task.compareIgnoreId(anotherTask)).isFalse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.liulishuo.okdownload.DownloadListener;
import com.liulishuo.okdownload.DownloadTask;
import com.liulishuo.okdownload.OkDownload;
import com.liulishuo.okdownload.core.IdentifiedTask;
import com.liulishuo.okdownload.core.Util;
import com.liulishuo.okdownload.core.breakpoint.BreakpointStore;
import com.liulishuo.okdownload.core.breakpoint.DownloadStore;
Expand Down Expand Up @@ -412,6 +413,15 @@ public void cancel_notSameReferenceButSameId_readyAsync() {
verify(listener).taskEnd(eq(readyAsyncTask), eq(CANCELED), nullable(Exception.class));
}

@Test
public void cancel_withId() {
doReturn(true).when(dispatcher).cancelLocked(any(IdentifiedTask.class));
dispatcher.cancel(1);
final ArgumentCaptor<IdentifiedTask> captor = ArgumentCaptor.forClass(IdentifiedTask.class);
verify(dispatcher).cancelLocked(captor.capture());
assertThat(captor.getValue().getId()).isEqualTo(1);
}

@Test
public void cancel_bunch() throws IOException {
mockOkDownload();
Expand Down

0 comments on commit 405cb2d

Please sign in to comment.