summaryrefslogtreecommitdiff
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
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
-rw-r--r--expectations/knownfailures.txt8
-rw-r--r--luni/src/test/java/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java1
-rw-r--r--support/src/test/java/tests/support/Support_TestWebServer.java38
3 files changed, 38 insertions, 9 deletions
diff --git a/expectations/knownfailures.txt b/expectations/knownfailures.txt
index 9eccf8e51f..2c1191b95b 100644
--- a/expectations/knownfailures.txt
+++ b/expectations/knownfailures.txt
@@ -1551,13 +1551,5 @@
names: [
"libcore.java.text.OldBidiTest#testUnicode9EmojisAreLtrNeutral"
]
-},
-{
- description: "Hitting timeout",
- bug: 32363029,
- result: EXEC_FAILED,
- names: [
- "org.apache.harmony.luni.tests.java.net.URLConnectionTest#test_setReadTimeoutI"
- ]
}
]
diff --git a/luni/src/test/java/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java b/luni/src/test/java/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java
index 8474cb2a82..4c04d2f7c0 100644
--- a/luni/src/test/java/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java
+++ b/luni/src/test/java/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java
@@ -219,6 +219,7 @@ public class URLConnectionTest extends TestCaseWithRules {
super.setUp();
server = new Support_TestWebServer();
+ server.setDelay(10);
port = server.initServer();
url = new URL("http://localhost:" + port + "/test1");
uc = url.openConnection();
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) {