### Question:
DownloadCall extends NamedRunnable implements Comparable<DownloadCall> { public boolean cancel() { synchronized (this) { if (canceled) return false; if (finishing) return false; this.canceled = true; } final long startCancelTime = SystemClock.uptimeMillis(); OkDownload.with().downloadDispatcher().flyingCanceled(this); final DownloadCache cache = this.cache; if (cache != null) cache.setUserCanceled(); final Object[] chains = blockChainList.toArray(); if (chains == null || chains.length == 0) { if (currentThread != null) { Util.d(TAG, "interrupt thread with cancel operation because of chains are not running " + task.getId()); currentThread.interrupt(); } } else { for (Object chain : chains) { if (chain instanceof DownloadChain) { ((DownloadChain) chain).cancel(); } } } if (cache != null) cache.getOutputStream().cancelAsync(); Util.d(TAG, "cancel task " + task.getId() + " consume: " + (SystemClock .uptimeMillis() - startCancelTime) + "ms"); return true; } private  DownloadCall(DownloadTask task, boolean asyncExecuted, @NonNull DownloadStore store);   DownloadCall(DownloadTask task, boolean asyncExecuted,
                 @NonNull ArrayList<DownloadChain> runningBlockList,
                 @NonNull DownloadStore store); static DownloadCall create(DownloadTask task, boolean asyncExecuted,
                                      @NonNull DownloadStore store); boolean cancel(); boolean isCanceled(); boolean isFinishing(); @Override void execute(); boolean equalsTask(@NonNull DownloadTask task); @Nullable File getFile(); @SuppressFBWarnings(value = "Eq", justification = "This special case is just for task priority") @Override int compareTo(@NonNull DownloadCall o); final DownloadTask task; final boolean asyncExecuted; }### Answer:
@Test public void cancel() { assertThat(call.cancel()).isTrue(); assertThat(call.cancel()).isFalse(); }
@Test public void cancel_cache() { final DownloadCache cache = mock(DownloadCache.class); final MultiPointOutputStream outputStream = mock(MultiPointOutputStream.class); call.cache = cache; when(cache.getOutputStream()).thenReturn(outputStream); call.cancel(); verify(call.cache).setUserCanceled(); verify(outputStream).cancelAsync(); }
@Test public void cancel_finishing() { call.finishing = true; assertThat(call.cancel()).isFalse(); final DownloadDispatcher dispatcher = OkDownload.with().downloadDispatcher(); verify(dispatcher, never()).flyingCanceled(eq(call)); }
@Test public void cancel_chains() { final DownloadChain mockChain = mock(DownloadChain.class); call.blockChainList.add(mockChain); call.blockChainList.add(null); assertThat(call.cancel()).isTrue(); verify(mockChain).cancel(); call.blockChainList.clear(); }
@Test public void cancel_current_thread_interrupt() { final Thread mockThread = mock(Thread.class); call.currentThread = mockThread; call.cancel(); verify(mockThread).interrupt(); call.currentThread = null; call.canceled = false; call.finishing = false; doReturn(null).when(blockChainList).toArray(); assertThat(call.cancel()).isTrue(); }