Skip to content

Commit

Permalink
fix: fix the old file isn't delete when file is dirty and have to dow…
Browse files Browse the repository at this point in the history
…nload from beginning which will raise can't resume when the file length is larger than current response instant-length
  • Loading branch information
Jacksgong committed Apr 27, 2018
1 parent 225945c commit c799b63
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,26 +178,33 @@ public void execute() throws InterruptedException {
OkDownload.with().downloadStrategy()
.inspectAnotherSameInfo(task, info, remoteCheck.getInstanceLength());

if (remoteCheck.isResumable()) {
// 5. local check
final BreakpointLocalCheck localCheck = createLocalCheck(info);
localCheck.check();
if (localCheck.isDirty()) {
try {
if (remoteCheck.isResumable()) {
// 5. local check
final BreakpointLocalCheck localCheck = createLocalCheck(info);
localCheck.check();
if (localCheck.isDirty()) {
Util.d(TAG, "breakpoint invalid: download from beginning because of "
+ "local check is dirty " + task.getId() + " " + localCheck);
// 6. assemble block data
fileStrategy.discardProcess(task);
assembleBlockAndCallbackFromBeginning(info, remoteCheck,
localCheck.getCauseOrThrow());
} else {
okDownload.callbackDispatcher().dispatch()
.downloadFromBreakpoint(task, info);
}
} else {
Util.d(TAG, "breakpoint invalid: download from beginning because of "
+ "local check is dirty " + task.getId() + " " + localCheck);
+ "remote check not resumable " + task.getId() + " " + remoteCheck);
// 6. assemble block data
fileStrategy.discardProcess(task);
assembleBlockAndCallbackFromBeginning(info, remoteCheck,
localCheck.getCauseOrThrow());
} else {
okDownload.callbackDispatcher().dispatch()
.downloadFromBreakpoint(task, info);
remoteCheck.getCauseOrThrow());
}
} else {
Util.d(TAG, "breakpoint invalid: download from beginning because of "
+ "remote check not resumable " + task.getId() + " " + remoteCheck);
// 6. assemble block data
assembleBlockAndCallbackFromBeginning(info, remoteCheck,
remoteCheck.getCauseOrThrow());
} catch (IOException e) {
cache.setUnknownError(e);
break;
}

// 7. start with cache and info.
Expand All @@ -209,12 +216,6 @@ public void execute() throws InterruptedException {
if (cache.isPreconditionFailed()
&& retryCount++ < MAX_COUNT_RETRY_FOR_PRECONDITION_FAILED) {
store.remove(task.getId());
try {
fileStrategy.discardProcess(task);
} catch (IOException e) {
cache.setUnknownError(e);
break;
}
retry = true;
} else {
retry = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ public void setup() {
when(store.get(anyInt())).thenReturn(info);
when(task.getUrl()).thenReturn("https://jacksgong.com");

}

private void setupFileStrategy() throws IOException {
mockOkDownload();

final ProcessFileStrategy fileStrategy = OkDownload.with().processFileStrategy();
when(fileStrategy.getFileLock()).thenReturn(fileLock);
}
Expand Down Expand Up @@ -136,7 +141,9 @@ public void execute_remoteCheck() throws IOException, InterruptedException {
}

@Test
public void execute_waitForRelease() throws InterruptedException {
public void execute_waitForRelease() throws InterruptedException, IOException {
setupFileStrategy();

final BreakpointRemoteCheck remoteCheck = mock(BreakpointRemoteCheck.class);
doReturn(remoteCheck).when(call).createRemoteCheck(eq(info));
doReturn(mock(BreakpointLocalCheck.class)).when(call).createLocalCheck(eq(info));
Expand Down Expand Up @@ -178,7 +185,10 @@ public void execute_localCheck() throws InterruptedException {
}

@Test
public void execute_assembleBlockData() throws InterruptedException {
public void execute_assembleBlockData() throws InterruptedException, IOException {
setupFileStrategy();

final ProcessFileStrategy fileStrategy = OkDownload.with().processFileStrategy();
final BreakpointLocalCheck localCheck = mock(BreakpointLocalCheck.class);
final BreakpointRemoteCheck remoteCheck = mock(BreakpointRemoteCheck.class);
final ResumeFailedCause failedCauseByRemote = mock(ResumeFailedCause.class);
Expand All @@ -195,6 +205,7 @@ public void execute_assembleBlockData() throws InterruptedException {
call.execute();
verify(call).assembleBlockAndCallbackFromBeginning(eq(info), eq(remoteCheck),
eq(failedCauseByRemote));
verify(fileStrategy).discardProcess(eq(task));

when(remoteCheck.isResumable()).thenReturn(true);
when(localCheck.isDirty()).thenReturn(false);
Expand All @@ -209,6 +220,7 @@ public void execute_assembleBlockData() throws InterruptedException {
call.execute();
verify(call).assembleBlockAndCallbackFromBeginning(eq(info), eq(remoteCheck),
eq(failedCauseByLocal));
verify(fileStrategy, times(2)).discardProcess(eq(task));
}

@Test
Expand All @@ -223,6 +235,8 @@ public void execute_start() throws InterruptedException {

@Test
public void execute_preconditionFailed() throws InterruptedException, IOException {
setupFileStrategy();

final DownloadCache cache = mock(DownloadCache.class);
doReturn(cache).when(call).createCache(eq(info));
when(cache.isPreconditionFailed()).thenReturn(true, false);
Expand All @@ -238,7 +252,7 @@ public void execute_preconditionFailed() throws InterruptedException, IOExceptio
final ProcessFileStrategy fileStrategy = OkDownload.with().processFileStrategy();
final int id = task.getId();
verify(store).remove(eq(id));
verify(fileStrategy).discardProcess(eq(task));
verify(fileStrategy, times(2)).discardProcess(eq(task));
}

@Test
Expand Down Expand Up @@ -281,13 +295,15 @@ public void execute_urlIsEmpty() throws InterruptedException {

@Test
public void execute_finish() throws InterruptedException, IOException {
setupFileStrategy();

assertThat(call.finishing).isFalse();

final OkDownload okDownload = OkDownload.with();
final CallbackDispatcher dispatcher = OkDownload.with().callbackDispatcher();
final DownloadCache cache = mock(DownloadCache.class);
final DownloadListener listener = mock(DownloadListener.class);
final ProcessFileStrategy fileStrategy = OkDownload.with().processFileStrategy();
final ProcessFileStrategy fileStrategy = okDownload.processFileStrategy();
final IOException iOException = mock(IOException.class);
final MultiPointOutputStream multiPointOutputStream = mock(MultiPointOutputStream.class);

Expand Down

0 comments on commit c799b63

Please sign in to comment.