summaryrefslogtreecommitdiffstats
path: root/tests/src/tests
diff options
context:
space:
mode:
authorVasu Nori <vnori@google.com>2010-12-06 15:16:23 -0800
committerVasu Nori <vnori@google.com>2010-12-09 17:22:38 -0800
commit5d217003acf21aea852975af0dff3b398cea6768 (patch)
treec9cf9a9fe823d667544e2e4ecd4b9f9bff5db670 /tests/src/tests
parent00caf765965d4356308b36ce387e14854cedfcdd (diff)
downloadandroid_packages_providers_DownloadProvider-5d217003acf21aea852975af0dff3b398cea6768.tar.gz
android_packages_providers_DownloadProvider-5d217003acf21aea852975af0dff3b398cea6768.tar.bz2
android_packages_providers_DownloadProvider-5d217003acf21aea852975af0dff3b398cea6768.zip
fix broken DownloadManager tests
one big change in this CL is addition of a new feature to MockWebServer. It can now play a long response to the Downloading thread to keep it busy while something - such as cancel/remove - can be done to that Download Request. Also, added changes to FakeSystemFacade to start threads in normal fashion instead of queuing them up and later running just their run() methods. the following tests should work now packages/providers/DownloadProvider/tests/src/com/android/providers/downloads/ DownloadManagerFunctionalTest.java PublicApiFunctionalTest.java ThreadingTest.java PublicApiAccessTest.java DownloadProviderPermissionsTest.java the following are flaky. they need to be split up into smaller tests. frameworks/base/core/tests/coretests/src/android/app/ DownloadManagerIntegrationTest.java DownloadManagerStressTest.java Change-Id: Ia0b11963f92e8f8365f701761dcbce467be3ee9b
Diffstat (limited to 'tests/src/tests')
-rw-r--r--tests/src/tests/http/MockResponse.java11
-rw-r--r--tests/src/tests/http/MockWebServer.java53
2 files changed, 52 insertions, 12 deletions
diff --git a/tests/src/tests/http/MockResponse.java b/tests/src/tests/http/MockResponse.java
index 4cda92d2..aec5490c 100644
--- a/tests/src/tests/http/MockResponse.java
+++ b/tests/src/tests/http/MockResponse.java
@@ -36,6 +36,7 @@ public class MockResponse {
private Map<String, String> headers = new HashMap<String, String>();
private byte[] body = EMPTY_BODY;
private boolean closeConnectionAfter = false;
+ private int numPackets = 0;
public MockResponse() {
addHeader("Content-Length", 0);
@@ -133,4 +134,14 @@ public class MockResponse {
this.closeConnectionAfter = closeConnectionAfter;
return this;
}
+
+ public int getNumPackets() {
+ return numPackets;
+ }
+
+ public MockResponse setNumPackets(int numPackets) {
+ this.numPackets = numPackets;
+ return this;
+ }
+
}
diff --git a/tests/src/tests/http/MockWebServer.java b/tests/src/tests/http/MockWebServer.java
index 11c8063e..6096783d 100644
--- a/tests/src/tests/http/MockWebServer.java
+++ b/tests/src/tests/http/MockWebServer.java
@@ -16,6 +16,9 @@
package tests.http;
+import android.text.TextUtils;
+import android.util.Log;
+
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
@@ -59,6 +62,7 @@ public final class MockWebServer {
private final Queue<Future<?>> futures = new LinkedList<Future<?>>();
private int port = -1;
+ private ServerSocket serverSocket;
public int getPort() {
if (port == -1) {
@@ -111,26 +115,35 @@ public final class MockWebServer {
* down.
*/
public void play() throws IOException {
- final ServerSocket ss = new ServerSocket(0);
- ss.setReuseAddress(true);
- port = ss.getLocalPort();
+ serverSocket = new ServerSocket(0);
+ serverSocket.setReuseAddress(true);
+ port = serverSocket.getLocalPort();
submitCallable(new Callable<Void>() {
public Void call() throws Exception {
int count = 0;
while (true) {
if (count > 0 && responseQueue.isEmpty()) {
- ss.close();
+ serverSocket.close();
executor.shutdown();
return null;
}
- serveConnection(ss.accept());
+ serveConnection(serverSocket.accept());
count++;
}
}
});
}
+ /**
+ * shutdown the webserver
+ */
+ public void shutdown() throws IOException {
+ responseQueue.clear();
+ serverSocket.close();
+ executor.shutdown();
+ }
+
private void serveConnection(final Socket s) {
submitCallable(new Callable<Void>() {
public Void call() throws Exception {
@@ -148,8 +161,7 @@ public final class MockWebServer {
}
}
requestQueue.add(request);
- MockResponse response = computeResponse(request);
- writeResponse(out, response);
+ MockResponse response = sendResponse(out, request);
if (response.shouldCloseConnectionAfter()) {
break;
}
@@ -241,7 +253,6 @@ public final class MockWebServer {
} else {
throw new UnsupportedOperationException("Unexpected method: " + request);
}
-
return new RecordedRequest(request, headers, chunkSizes,
requestBody.numBytesReceived, requestBody.toByteArray(), sequenceNumber);
}
@@ -249,14 +260,32 @@ public final class MockWebServer {
/**
* Returns a response to satisfy {@code request}.
*/
- private MockResponse computeResponse(RecordedRequest request) throws InterruptedException {
+ private MockResponse sendResponse(OutputStream out, RecordedRequest request)
+ throws InterruptedException, IOException {
if (responseQueue.isEmpty()) {
throw new IllegalStateException("Unexpected request: " + request);
}
- return responseQueue.take();
- }
+ MockResponse response = responseQueue.take();
+ writeResponse(out, response, false);
+ if (response.getNumPackets() > 0) {
+ // there are continuing packets to send as part of this response.
+ for (int i = 0; i < response.getNumPackets(); i++) {
+ writeResponse(out, response, true);
+ // delay sending next continuing response just a little bit
+ Thread.sleep(100);
+ }
+ }
+ return response;
+ }
- private void writeResponse(OutputStream out, MockResponse response) throws IOException {
+ private void writeResponse(OutputStream out, MockResponse response,
+ boolean continuingPacket) throws IOException {
+ if (continuingPacket) {
+ // this is a continuing response - just send the body - no headers, status
+ out.write(response.getBody());
+ out.flush();
+ return;
+ }
out.write((response.getStatus() + "\r\n").getBytes(ASCII));
for (String header : response.getHeaders()) {
out.write((header + "\r\n").getBytes(ASCII));