@Test public void run_responseCodeNotMet_error() throws IOException, IllegalAccessException { final FileDownloadConnection connection = mock(FileDownloadConnection.class); when(connection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_PRECON_FAILED); when(mockConnectTask.connect()).thenReturn(connection); downloadRunnable.run(); verify(mockCallback).onRetry(any(Exception.class), anyLong()); verify(mockCallback).onError(any(Exception.class)); }
@Override public void run() { Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); FileDownloadConnection connection = null; final long beginOffset = connectTask.getProfile().currentOffset; boolean isConnected = false; do { try { if (paused) { return; } isConnected = false; connection = connectTask.connect(); final int code = connection.getResponseCode(); if (FileDownloadLog.NEED_LOG) { FileDownloadLog.d(this, "the connection[%d] for %d, is connected %s with code[%d]", connectionIndex, downloadId, connectTask.getProfile(), code); } if (code != HttpURLConnection.HTTP_PARTIAL && code != HttpURLConnection.HTTP_OK) { throw new SocketException(FileDownloadUtils. formatString("Connection failed with request[%s] response[%s] http-state[%d] on task[%d-%d], " + "which is changed after verify connection, so please try again.", connectTask.getRequestHeader(), connection.getResponseHeaderFields(), code, downloadId, connectionIndex)); } isConnected = true; final FetchDataTask.Builder builder = new FetchDataTask.Builder(); if (paused) return; fetchDataTask = builder .setDownloadId(downloadId) .setConnectionIndex(connectionIndex) .setCallback(callback) .setHost(this) .setWifiRequired(isWifiRequired) .setConnection(connection) .setConnectionProfile(this.connectTask.getProfile()) .setPath(path) .build(); fetchDataTask.run(); if (paused){ fetchDataTask.pause(); } break; } catch (IllegalAccessException | IOException | FileDownloadGiveUpRetryException | IllegalArgumentException e) { if (callback.isRetry(e)) { if (!isConnected) { callback.onRetry(e, 0); } else if (fetchDataTask != null) { final long invalidIncreaseBytes = fetchDataTask.currentOffset - beginOffset; callback.onRetry(e, invalidIncreaseBytes); } else { FileDownloadLog.w(this, "it is valid to retry and connection is valid but" + " create fetch-data-task failed, so give up directly with %s", e); callback.onError(e); break; } } else { callback.onError(e); break; } } finally { if (connection != null) connection.ending(); } } while (true); }
DownloadRunnable implements Runnable { @Override public void run() { Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); FileDownloadConnection connection = null; final long beginOffset = connectTask.getProfile().currentOffset; boolean isConnected = false; do { try { if (paused) { return; } isConnected = false; connection = connectTask.connect(); final int code = connection.getResponseCode(); if (FileDownloadLog.NEED_LOG) { FileDownloadLog.d(this, "the connection[%d] for %d, is connected %s with code[%d]", connectionIndex, downloadId, connectTask.getProfile(), code); } if (code != HttpURLConnection.HTTP_PARTIAL && code != HttpURLConnection.HTTP_OK) { throw new SocketException(FileDownloadUtils. formatString("Connection failed with request[%s] response[%s] http-state[%d] on task[%d-%d], " + "which is changed after verify connection, so please try again.", connectTask.getRequestHeader(), connection.getResponseHeaderFields(), code, downloadId, connectionIndex)); } isConnected = true; final FetchDataTask.Builder builder = new FetchDataTask.Builder(); if (paused) return; fetchDataTask = builder .setDownloadId(downloadId) .setConnectionIndex(connectionIndex) .setCallback(callback) .setHost(this) .setWifiRequired(isWifiRequired) .setConnection(connection) .setConnectionProfile(this.connectTask.getProfile()) .setPath(path) .build(); fetchDataTask.run(); if (paused){ fetchDataTask.pause(); } break; } catch (IllegalAccessException | IOException | FileDownloadGiveUpRetryException | IllegalArgumentException e) { if (callback.isRetry(e)) { if (!isConnected) { callback.onRetry(e, 0); } else if (fetchDataTask != null) { final long invalidIncreaseBytes = fetchDataTask.currentOffset - beginOffset; callback.onRetry(e, invalidIncreaseBytes); } else { FileDownloadLog.w(this, "it is valid to retry and connection is valid but" + " create fetch-data-task failed, so give up directly with %s", e); callback.onError(e); break; } } else { callback.onError(e); break; } } finally { if (connection != null) connection.ending(); } } while (true); } }
DownloadRunnable implements Runnable { @Override public void run() { Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); FileDownloadConnection connection = null; final long beginOffset = connectTask.getProfile().currentOffset; boolean isConnected = false; do { try { if (paused) { return; } isConnected = false; connection = connectTask.connect(); final int code = connection.getResponseCode(); if (FileDownloadLog.NEED_LOG) { FileDownloadLog.d(this, "the connection[%d] for %d, is connected %s with code[%d]", connectionIndex, downloadId, connectTask.getProfile(), code); } if (code != HttpURLConnection.HTTP_PARTIAL && code != HttpURLConnection.HTTP_OK) { throw new SocketException(FileDownloadUtils. formatString("Connection failed with request[%s] response[%s] http-state[%d] on task[%d-%d], " + "which is changed after verify connection, so please try again.", connectTask.getRequestHeader(), connection.getResponseHeaderFields(), code, downloadId, connectionIndex)); } isConnected = true; final FetchDataTask.Builder builder = new FetchDataTask.Builder(); if (paused) return; fetchDataTask = builder .setDownloadId(downloadId) .setConnectionIndex(connectionIndex) .setCallback(callback) .setHost(this) .setWifiRequired(isWifiRequired) .setConnection(connection) .setConnectionProfile(this.connectTask.getProfile()) .setPath(path) .build(); fetchDataTask.run(); if (paused){ fetchDataTask.pause(); } break; } catch (IllegalAccessException | IOException | FileDownloadGiveUpRetryException | IllegalArgumentException e) { if (callback.isRetry(e)) { if (!isConnected) { callback.onRetry(e, 0); } else if (fetchDataTask != null) { final long invalidIncreaseBytes = fetchDataTask.currentOffset - beginOffset; callback.onRetry(e, invalidIncreaseBytes); } else { FileDownloadLog.w(this, "it is valid to retry and connection is valid but" + " create fetch-data-task failed, so give up directly with %s", e); callback.onError(e); break; } } else { callback.onError(e); break; } } finally { if (connection != null) connection.ending(); } } while (true); } private  DownloadRunnable(int id, int connectionIndex, ConnectTask connectTask,
                             ProcessCallback callback, boolean isWifiRequired, String path); }
DownloadRunnable implements Runnable { @Override public void run() { Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); FileDownloadConnection connection = null; final long beginOffset = connectTask.getProfile().currentOffset; boolean isConnected = false; do { try { if (paused) { return; } isConnected = false; connection = connectTask.connect(); final int code = connection.getResponseCode(); if (FileDownloadLog.NEED_LOG) { FileDownloadLog.d(this, "the connection[%d] for %d, is connected %s with code[%d]", connectionIndex, downloadId, connectTask.getProfile(), code); } if (code != HttpURLConnection.HTTP_PARTIAL && code != HttpURLConnection.HTTP_OK) { throw new SocketException(FileDownloadUtils. formatString("Connection failed with request[%s] response[%s] http-state[%d] on task[%d-%d], " + "which is changed after verify connection, so please try again.", connectTask.getRequestHeader(), connection.getResponseHeaderFields(), code, downloadId, connectionIndex)); } isConnected = true; final FetchDataTask.Builder builder = new FetchDataTask.Builder(); if (paused) return; fetchDataTask = builder .setDownloadId(downloadId) .setConnectionIndex(connectionIndex) .setCallback(callback) .setHost(this) .setWifiRequired(isWifiRequired) .setConnection(connection) .setConnectionProfile(this.connectTask.getProfile()) .setPath(path) .build(); fetchDataTask.run(); if (paused){ fetchDataTask.pause(); } break; } catch (IllegalAccessException | IOException | FileDownloadGiveUpRetryException | IllegalArgumentException e) { if (callback.isRetry(e)) { if (!isConnected) { callback.onRetry(e, 0); } else if (fetchDataTask != null) { final long invalidIncreaseBytes = fetchDataTask.currentOffset - beginOffset; callback.onRetry(e, invalidIncreaseBytes); } else { FileDownloadLog.w(this, "it is valid to retry and connection is valid but" + " create fetch-data-task failed, so give up directly with %s", e); callback.onError(e); break; } } else { callback.onError(e); break; } } finally { if (connection != null) connection.ending(); } } while (true); } private  DownloadRunnable(int id, int connectionIndex, ConnectTask connectTask,
                             ProcessCallback callback, boolean isWifiRequired, String path); void pause(); void discard(); @Override void run(); }
DownloadRunnable implements Runnable { @Override public void run() { Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); FileDownloadConnection connection = null; final long beginOffset = connectTask.getProfile().currentOffset; boolean isConnected = false; do { try { if (paused) { return; } isConnected = false; connection = connectTask.connect(); final int code = connection.getResponseCode(); if (FileDownloadLog.NEED_LOG) { FileDownloadLog.d(this, "the connection[%d] for %d, is connected %s with code[%d]", connectionIndex, downloadId, connectTask.getProfile(), code); } if (code != HttpURLConnection.HTTP_PARTIAL && code != HttpURLConnection.HTTP_OK) { throw new SocketException(FileDownloadUtils. formatString("Connection failed with request[%s] response[%s] http-state[%d] on task[%d-%d], " + "which is changed after verify connection, so please try again.", connectTask.getRequestHeader(), connection.getResponseHeaderFields(), code, downloadId, connectionIndex)); } isConnected = true; final FetchDataTask.Builder builder = new FetchDataTask.Builder(); if (paused) return; fetchDataTask = builder .setDownloadId(downloadId) .setConnectionIndex(connectionIndex) .setCallback(callback) .setHost(this) .setWifiRequired(isWifiRequired) .setConnection(connection) .setConnectionProfile(this.connectTask.getProfile()) .setPath(path) .build(); fetchDataTask.run(); if (paused){ fetchDataTask.pause(); } break; } catch (IllegalAccessException | IOException | FileDownloadGiveUpRetryException | IllegalArgumentException e) { if (callback.isRetry(e)) { if (!isConnected) { callback.onRetry(e, 0); } else if (fetchDataTask != null) { final long invalidIncreaseBytes = fetchDataTask.currentOffset - beginOffset; callback.onRetry(e, invalidIncreaseBytes); } else { FileDownloadLog.w(this, "it is valid to retry and connection is valid but" + " create fetch-data-task failed, so give up directly with %s", e); callback.onError(e); break; } } else { callback.onError(e); break; } } finally { if (connection != null) connection.ending(); } } while (true); } private  DownloadRunnable(int id, int connectionIndex, ConnectTask connectTask,
                             ProcessCallback callback, boolean isWifiRequired, String path); void pause(); void discard(); @Override void run();  }