diff options
author | Steve Howard <showard@google.com> | 2010-06-22 18:20:55 -0700 |
---|---|---|
committer | Steve Howard <showard@google.com> | 2010-07-01 11:35:26 -0700 |
commit | 23357198c440e6872d3aef3e608295db7f8273bc (patch) | |
tree | 16391d655af983b751b41af5267e8053009a1f2c /tests/src/tests/http | |
parent | b0aada69b9e6258bb9a1a7c1b783d0361ef3c6f2 (diff) | |
download | android_packages_providers_DownloadProvider-23357198c440e6872d3aef3e608295db7f8273bc.tar.gz android_packages_providers_DownloadProvider-23357198c440e6872d3aef3e608295db7f8273bc.tar.bz2 android_packages_providers_DownloadProvider-23357198c440e6872d3aef3e608295db7f8273bc.zip |
Stub out the system clock in the download manager, add tests
Introduce SystemFacade, an interface that allows us to stub out the
system clock for testing the download manager. This allows us to test
retrying a failed download without having the test wait 60 seconds.
This interface can include other dependencies in the future as well.
I've also used this to add tests for 503 (retry-after) and 301
(redirect), and I've added a test for download to the cache partition.
Other changes:
* made MockWebServer capable of checking + rethrowing exceptions from child threads
* refactoring + cleanup of DownloadManagerFunctionalTest
Diffstat (limited to 'tests/src/tests/http')
-rw-r--r-- | tests/src/tests/http/MockWebServer.java | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/tests/src/tests/http/MockWebServer.java b/tests/src/tests/http/MockWebServer.java index b2cb8d7b..11c8063e 100644 --- a/tests/src/tests/http/MockWebServer.java +++ b/tests/src/tests/http/MockWebServer.java @@ -27,13 +27,18 @@ import java.net.ServerSocket; import java.net.Socket; import java.net.URL; import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; +import java.util.Queue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.Future; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; /** * A scriptable web server. Callers supply canned responses and the server @@ -50,6 +55,8 @@ public final class MockWebServer { = new LinkedBlockingQueue<MockResponse>(); private int bodyLimit = Integer.MAX_VALUE; private final ExecutorService executor = Executors.newCachedThreadPool(); + // keep Futures around so we can rethrow any exceptions thrown by Callables + private final Queue<Future<?>> futures = new LinkedList<Future<?>>(); private int port = -1; @@ -107,7 +114,7 @@ public final class MockWebServer { final ServerSocket ss = new ServerSocket(0); ss.setReuseAddress(true); port = ss.getLocalPort(); - executor.submit(new Callable<Void>() { + submitCallable(new Callable<Void>() { public Void call() throws Exception { int count = 0; while (true) { @@ -125,7 +132,7 @@ public final class MockWebServer { } private void serveConnection(final Socket s) { - executor.submit(new Callable<Void>() { + submitCallable(new Callable<Void>() { public Void call() throws Exception { InputStream in = new BufferedInputStream(s.getInputStream()); OutputStream out = new BufferedOutputStream(s.getOutputStream()); @@ -156,6 +163,28 @@ public final class MockWebServer { }); } + private void submitCallable(Callable<?> callable) { + Future<?> future = executor.submit(callable); + futures.add(future); + } + + /** + * Check for and raise any exceptions that have been thrown by child threads. Will not block on + * children still running. + * @throws ExecutionException for the first child thread that threw an exception + */ + public void checkForExceptions() throws ExecutionException, InterruptedException { + final int originalSize = futures.size(); + for (int i = 0; i < originalSize; i++) { + Future<?> future = futures.remove(); + try { + future.get(0, TimeUnit.SECONDS); + } catch (TimeoutException e) { + futures.add(future); // still running + } + } + } + /** * @param sequenceNumber the index of this request on this connection. */ |