diff options
author | Steve Howard <showard@google.com> | 2010-07-27 16:28:40 -0700 |
---|---|---|
committer | Steve Howard <showard@google.com> | 2010-07-27 18:47:14 -0700 |
commit | dea0a5f8e5804d3aba40eaa7de763dd88058384e (patch) | |
tree | 2b65161a6e2824c5ce78a467e4f377e9e258a161 /src/com/android/providers | |
parent | f85aa9ef563f2fbb3c0db6c980121122a14d953f (diff) | |
download | android_packages_providers_DownloadProvider-dea0a5f8e5804d3aba40eaa7de763dd88058384e.tar.gz android_packages_providers_DownloadProvider-dea0a5f8e5804d3aba40eaa7de763dd88058384e.tar.bz2 android_packages_providers_DownloadProvider-dea0a5f8e5804d3aba40eaa7de763dd88058384e.zip |
Add test for many interruptions to a single download.
Adding a new test case for downloads that undergo many interruptions
(as may happen with a very large download that takes many hours).
Includes some refactoring in the test suite.
Early on, this test exposed a race condition in which the download
manager got some I/O exception while reading from the MockWebServer.
I went in and improved/refactored much of the error logging code in
DownloadThread to try and track this down. Unfortunately, once I
finished, the race condition no longer seems to be reproducible, even
with hundreds of runs of the test case. So I've given up on it for
now. In any event, error logging is better and much duplicate code
has been eliminated.
Diffstat (limited to 'src/com/android/providers')
-rw-r--r-- | src/com/android/providers/downloads/DownloadThread.java | 126 |
1 files changed, 46 insertions, 80 deletions
diff --git a/src/com/android/providers/downloads/DownloadThread.java b/src/com/android/providers/downloads/DownloadThread.java index 736bbce5..be02e3ec 100644 --- a/src/com/android/providers/downloads/DownloadThread.java +++ b/src/com/android/providers/downloads/DownloadThread.java @@ -116,7 +116,13 @@ public class DownloadThread extends Thread { * Raised from methods called by run() to indicate that the current request should be stopped * immediately. */ - private class StopRequest extends Exception {} + private class StopRequest extends Exception { + public StopRequest() {} + + public StopRequest(Throwable throwable) { + super(throwable); + } + } /** * Raised from methods called by executeDownload() to indicate that the download should be @@ -165,6 +171,9 @@ public class DownloadThread extends Thread { } state.mFinalStatus = Downloads.Impl.STATUS_SUCCESS; } catch (StopRequest error) { + if (Constants.LOGV) { + Log.v(Constants.TAG, "Aborting request for " + mInfo.mUri, error); + } // fall through to finally block } catch (FileNotFoundException ex) { Log.d(Constants.TAG, "FileNotFoundException for " + state.mFilename + " : " + ex); @@ -424,7 +433,7 @@ public class DownloadThread extends Thread { } catch (IOException ex) { if (!Helpers.discardPurgeableFiles(mContext, Constants.BUFFER_SIZE)) { state.mFinalStatus = Downloads.Impl.STATUS_FILE_ERROR; - throw new StopRequest(); + throw new StopRequest(ex); } } } @@ -454,19 +463,8 @@ public class DownloadThread extends Thread { mInfo.mId); } state.mFinalStatus = Downloads.Impl.STATUS_LENGTH_REQUIRED; - } else if (!Helpers.isNetworkAvailable(mSystemFacade)) { - state.mFinalStatus = Downloads.Impl.STATUS_RUNNING_PAUSED; - } else if (mInfo.mNumFailed < Constants.MAX_RETRIES) { - state.mFinalStatus = Downloads.Impl.STATUS_RUNNING_PAUSED; - state.mCountRetry = true; } else { - if (Constants.LOGV) { - Log.v(Constants.TAG, "closed socket for " + mInfo.mUri); - } else if (Config.LOGD) { - Log.d(Constants.TAG, "closed socket for download " + - mInfo.mId); - } - state.mFinalStatus = Downloads.Impl.STATUS_HTTP_DATA_ERROR; + handleHttpError(state, "closed socket"); } throw new StopRequest(); } @@ -483,30 +481,18 @@ public class DownloadThread extends Thread { try { return entityStream.read(data); } catch (IOException ex) { - if (Constants.LOGX) { - if (Helpers.isNetworkAvailable(mSystemFacade)) { - Log.i(Constants.TAG, "Read Failed " + mInfo.mId + ", Net Up"); - } else { - Log.i(Constants.TAG, "Read Failed " + mInfo.mId + ", Net Down"); - } - } + logNetworkState(); ContentValues values = new ContentValues(); values.put(Downloads.Impl.COLUMN_CURRENT_BYTES, innerState.mBytesSoFar); mContext.getContentResolver().update(state.mContentUri, values, null, null); if (!mInfo.mNoIntegrity && innerState.mHeaderETag == null) { - Log.d(Constants.TAG, "download IOException for download " + mInfo.mId + " : " + ex); + Log.d(Constants.TAG, "download IOException for download " + mInfo.mId, ex); Log.d(Constants.TAG, "can't resume interrupted download with no ETag"); state.mFinalStatus = Downloads.Impl.STATUS_PRECONDITION_FAILED; - } else if (!Helpers.isNetworkAvailable(mSystemFacade)) { - state.mFinalStatus = Downloads.Impl.STATUS_RUNNING_PAUSED; - } else if (mInfo.mNumFailed < Constants.MAX_RETRIES) { - state.mFinalStatus = Downloads.Impl.STATUS_RUNNING_PAUSED; - state.mCountRetry = true; } else { - Log.d(Constants.TAG, "download IOException for download " + mInfo.mId + " : " + ex); - state.mFinalStatus = Downloads.Impl.STATUS_HTTP_DATA_ERROR; + handleHttpError(state, "download IOException"); } - throw new StopRequest(); + throw new StopRequest(ex); } } @@ -519,32 +505,16 @@ public class DownloadThread extends Thread { try { return response.getEntity().getContent(); } catch (IOException ex) { - if (Constants.LOGX) { - if (Helpers.isNetworkAvailable(mSystemFacade)) { - Log.i(Constants.TAG, "Get Failed " + mInfo.mId + ", Net Up"); - } else { - Log.i(Constants.TAG, "Get Failed " + mInfo.mId + ", Net Down"); - } - } - if (!Helpers.isNetworkAvailable(mSystemFacade)) { - state.mFinalStatus = Downloads.Impl.STATUS_RUNNING_PAUSED; - } else if (mInfo.mNumFailed < Constants.MAX_RETRIES) { - state.mFinalStatus = Downloads.Impl.STATUS_RUNNING_PAUSED; - state.mCountRetry = true; - } else { - if (Constants.LOGV) { - Log.d(Constants.TAG, - "IOException getting entity for " + - mInfo.mUri + - " : " + - ex); - } else if (Config.LOGD) { - Log.d(Constants.TAG, "IOException getting entity for download " + - mInfo.mId + " : " + ex); - } - state.mFinalStatus = Downloads.Impl.STATUS_HTTP_DATA_ERROR; - } - throw new StopRequest(); + logNetworkState(); + handleHttpError(state, "IOException getting entity"); + throw new StopRequest(ex); + } + } + + private void logNetworkState() { + if (Constants.LOGX) { + Log.i(Constants.TAG, + "Net " + (Helpers.isNetworkAvailable(mSystemFacade) ? "Up" : "Down")); } } @@ -806,31 +776,27 @@ public class DownloadThread extends Thread { mInfo.mId + " : " + ex); } state.mFinalStatus = Downloads.Impl.STATUS_BAD_REQUEST; - throw new StopRequest(); + throw new StopRequest(ex); } catch (IOException ex) { - if (Constants.LOGX) { - if (Helpers.isNetworkAvailable(mSystemFacade)) { - Log.i(Constants.TAG, "Execute Failed " + mInfo.mId + ", Net Up"); - } else { - Log.i(Constants.TAG, "Execute Failed " + mInfo.mId + ", Net Down"); - } - } - if (!Helpers.isNetworkAvailable(mSystemFacade)) { - state.mFinalStatus = Downloads.Impl.STATUS_RUNNING_PAUSED; - } else if (mInfo.mNumFailed < Constants.MAX_RETRIES) { - state.mFinalStatus = Downloads.Impl.STATUS_RUNNING_PAUSED; - state.mCountRetry = true; - } else { - if (Constants.LOGV) { - Log.d(Constants.TAG, "IOException trying to execute request for " + - mInfo.mUri + " : " + ex); - } else if (Config.LOGD) { - Log.d(Constants.TAG, "IOException trying to execute request for " + - mInfo.mId + " : " + ex); - } - state.mFinalStatus = Downloads.Impl.STATUS_HTTP_DATA_ERROR; - } - throw new StopRequest(); + logNetworkState(); + handleHttpError(state, "IOException trying to execute request"); + throw new StopRequest(ex); + } + } + + private void handleHttpError(State state, String message) { + if (Constants.LOGV) { + Log.d(Constants.TAG, message + " for " + mInfo.mUri); + } + + if (!Helpers.isNetworkAvailable(mSystemFacade)) { + state.mFinalStatus = Downloads.Impl.STATUS_RUNNING_PAUSED; + } else if (mInfo.mNumFailed < Constants.MAX_RETRIES) { + state.mFinalStatus = Downloads.Impl.STATUS_RUNNING_PAUSED; + state.mCountRetry = true; + } else { + Log.d(Constants.TAG, "reached max retries: " + message + " for " + mInfo.mId); + state.mFinalStatus = Downloads.Impl.STATUS_HTTP_DATA_ERROR; } } |