Skip to content

Commit 2587650

Browse files
committed
add timeout tests
1 parent c520831 commit 2587650

File tree

4 files changed

+73
-7
lines changed

4 files changed

+73
-7
lines changed

AndroidAsync/AndroidAsync.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
<excludeFolder url="file://$MODULE_DIR$/build/symbols" />
6464
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
6565
</content>
66-
<orderEntry type="jdk" jdkName="Android 4.2.2" jdkType="Android SDK" />
66+
<orderEntry type="jdk" jdkName="Android 4.2.2 Platform" jdkType="Android SDK" />
6767
<orderEntry type="sourceFolder" forTests="false" />
6868
</component>
6969
</module>

AndroidAsync/src/com/koushikdutta/async/http/AsyncHttpClient.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ public void run() {
137137
}
138138

139139
private static long getTimeoutRemaining(AsyncHttpRequest request) {
140-
return Math.max(System.currentTimeMillis() - request.executionTime + request.getTimeout(), 1);
140+
// need a better way to calculate this.
141+
// a timer of sorts that stops/resumes.
142+
return request.getTimeout();
141143
}
142144

143145
private void executeAffinity(final AsyncHttpRequest request, final int redirectCount, final FutureAsyncHttpResponse cancel, final HttpConnectCallback callback) {

AndroidAsyncTest/AndroidAsyncTest.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<sourceFolder url="file://$MODULE_DIR$/gen" isTestSource="false" />
1919
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
2020
</content>
21-
<orderEntry type="jdk" jdkName="Android 4.2.2" jdkType="Android SDK" />
21+
<orderEntry type="jdk" jdkName="Android 4.2.2 Platform" jdkType="Android SDK" />
2222
<orderEntry type="sourceFolder" forTests="false" />
2323
<orderEntry type="module" module-name="AndroidAsync" />
2424
</component>

AndroidAsyncTest/src/com/koushikdutta/async/test/TimeoutTests.java

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
package com.koushikdutta.async.test;
22

3+
import android.util.Log;
4+
35
import com.koushikdutta.async.AsyncServer;
6+
import com.koushikdutta.async.DataEmitter;
7+
import com.koushikdutta.async.DataSink;
8+
import com.koushikdutta.async.callback.CompletedCallback;
9+
import com.koushikdutta.async.http.AsyncHttpClient;
10+
import com.koushikdutta.async.http.AsyncHttpRequest;
11+
import com.koushikdutta.async.http.AsyncHttpRequestBody;
12+
import com.koushikdutta.async.http.StringBody;
413
import com.koushikdutta.async.http.server.AsyncHttpServer;
514
import com.koushikdutta.async.http.server.AsyncHttpServerRequest;
615
import com.koushikdutta.async.http.server.AsyncHttpServerResponse;
716
import com.koushikdutta.async.http.server.HttpServerRequestCallback;
817

918
import junit.framework.TestCase;
1019

20+
import java.net.URI;
21+
import java.util.concurrent.TimeoutException;
22+
1123
/**
1224
* Created by koush on 7/11/13.
1325
*/
@@ -22,14 +34,15 @@ public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerRespo
2234
public void run() {
2335
response.send("3");
2436
}
25-
}, 3000);
37+
}, 1000);
2638
}
2739
});
2840

29-
server.get("/now", new HttpServerRequestCallback() {
41+
server.post("/now", new HttpServerRequestCallback() {
3042
@Override
3143
public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
32-
response.send("now");
44+
StringBody body = (StringBody)request.getBody();
45+
response.send(body.get());
3346
}
3447
});
3548
}
@@ -44,9 +57,60 @@ protected void setUp() throws Exception {
4457
@Override
4558
protected void tearDown() throws Exception {
4659
super.tearDown();
47-
AsyncServer.getDefault().stop();
60+
// AsyncServer.getDefault().stop();
61+
server.stop();
62+
4863
}
4964

5065
public void testTimeout() throws Exception {
66+
AsyncHttpRequest req = new AsyncHttpRequest(URI.create("http://localhost:5000/3"), "GET");
67+
req.setTimeout(1000);
68+
try {
69+
AsyncHttpClient.getDefaultInstance().executeString(req).get();
70+
fail();
71+
}
72+
catch (Exception e) {
73+
Log.d("timeout", "error", e);
74+
assertTrue(e.getCause() instanceof TimeoutException);
75+
}
76+
77+
req = new AsyncHttpRequest(URI.create("http://localhost:5000/3"), "GET");
78+
assertEquals("3", AsyncHttpClient.getDefaultInstance().executeString(req).get());
79+
}
80+
81+
public void testSlowBody() throws Exception {
82+
AsyncHttpRequest req = new AsyncHttpRequest(URI.create("http://localhost:5000/now"), "POST");
83+
req.setTimeout(1000);
84+
req.setLogging("slowbody", Log.VERBOSE);
85+
req.setBody(new DelayedStringBody("foo"));
86+
assertEquals("foo", AsyncHttpClient.getDefaultInstance().executeString(req).get());
87+
88+
req = new AsyncHttpRequest(URI.create("http://localhost:5000/3"), "GET");
89+
req.setLogging("slowbody", Log.VERBOSE);
90+
req.setTimeout(100);
91+
req.setBody(new DelayedStringBody("foo"));
92+
try {
93+
AsyncHttpClient.getDefaultInstance().executeString(req).get();
94+
fail();
95+
}
96+
catch (Exception e) {
97+
Log.d("timeout", "error", e);
98+
assertTrue(e.getCause() instanceof TimeoutException);
99+
}
100+
}
101+
102+
class DelayedStringBody extends StringBody {
103+
public DelayedStringBody(String value) {
104+
super(value);
105+
}
106+
@Override
107+
public void write(final AsyncHttpRequest request, final DataSink sink, final CompletedCallback completed) {
108+
AsyncServer.getDefault().postDelayed(new Runnable() {
109+
@Override
110+
public void run() {
111+
DelayedStringBody.super.write(request, sink, completed);
112+
}
113+
}, 1000);
114+
}
51115
}
52116
}

0 commit comments

Comments
 (0)