summaryrefslogtreecommitdiff
path: root/support
diff options
context:
space:
mode:
authorYi Kong <yikong@google.com>2016-10-31 15:27:43 +0800
committerYi Kong <yikong@google.com>2016-10-31 17:15:09 +0800
commit44c55c5b639d6c1eea6eeae8414ef9df95b67bb7 (patch)
tree4fc3617102f9e30946b3cb62683443aaf1c524c8 /support
parentae7c1851b7d11f297b38646ca6c0db1fb10f9056 (diff)
Fix URLConnectionTest.test_setReadTimeoutI on host test
This was failing because the test assumes that read will not complete within 1ms, while it is still possible, especially on host tests. This patch introduces a minimum time delay before the TestWebServer sends data. Bug: 32363029 Test: org.apache.harmony.luni.tests.java.net.URLConnectionTest#test_setReadTimeoutI under host tests Change-Id: Ibb4e2125f99b0544d0f09331b4ab9b0448b14035
Diffstat (limited to 'support')
-rw-r--r--support/src/test/java/tests/support/Support_TestWebServer.java38
1 files changed, 37 insertions, 1 deletions
diff --git a/support/src/test/java/tests/support/Support_TestWebServer.java b/support/src/test/java/tests/support/Support_TestWebServer.java
index 4ccf92ce82..91fe6eef09 100644
--- a/support/src/test/java/tests/support/Support_TestWebServer.java
+++ b/support/src/test/java/tests/support/Support_TestWebServer.java
@@ -69,6 +69,9 @@ public class Support_TestWebServer implements Support_HttpConstants {
/* Switch on/off logging */
boolean mLog = false;
+ /* Minimum delay before sending response */
+ int mDelay = 0;
+
/* If set, this will keep connections alive after a request has been
* processed.
*/
@@ -174,6 +177,15 @@ public class Support_TestWebServer implements Support_HttpConstants {
}
/**
+ * Call this to introduce a minimum delay before server responds to requests. When this value is
+ * not set, no delay will be introduced.
+ * @param delay The delay in milliseconds
+ */
+ public void setDelay(int delay) {
+ mDelay = delay;
+ }
+
+ /**
* Returns a map from recently-requested paths (like "/index.html") to a
* snapshot of the request data.
*/
@@ -215,7 +227,7 @@ public class Support_TestWebServer implements Support_HttpConstants {
while (!closed) {
try {
Socket s = ss.accept();
- new Thread(new Worker(s), "additional worker").start();
+ new Thread(new Worker(s).setDelay(mDelay), "additional worker").start();
} catch (SocketException e) {
log(e.getMessage());
} catch (IOException e) {
@@ -303,6 +315,9 @@ public class Support_TestWebServer implements Support_HttpConstants {
/* Indicates whether current request has any data content */
private boolean hasContent = false;
+ /* Optional delay before sending response */
+ private int delay = 0;
+
/* Request headers are stored here */
private Map<String, String> headers = new LinkedHashMap<String, String>();
@@ -312,6 +327,11 @@ public class Support_TestWebServer implements Support_HttpConstants {
this.s = s;
}
+ public Worker setDelay(int delay) {
+ this.delay = delay;
+ return this;
+ }
+
public synchronized void run() {
try {
handleClient();
@@ -787,6 +807,14 @@ public class Support_TestWebServer implements Support_HttpConstants {
* @param ps The PrintStream to write to
*/
void send404(PrintStream ps) throws IOException {
+ if (delay > 0) {
+ try {
+ Thread.sleep(delay);
+ } catch (InterruptedException e) {
+ // Ignored
+ }
+ }
+
ps.println("Not Found\n\n"+
"The requested resource was not found.\n");
}
@@ -808,6 +836,14 @@ public class Support_TestWebServer implements Support_HttpConstants {
}
void sendFile(PrintStream ps, byte[] bytes) throws IOException {
+ if (delay > 0) {
+ try {
+ Thread.sleep(delay);
+ } catch (InterruptedException e) {
+ // Ignored
+ }
+ }
+
if (chunked) {
int offset = 0;
while (offset < bytes.length) {