1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
package com.koushikdutta.async.test;
import android.util.Log;
import com.koushikdutta.async.AsyncServer;
import com.koushikdutta.async.DataSink;
import com.koushikdutta.async.callback.CompletedCallback;
import com.koushikdutta.async.http.AsyncHttpClient;
import com.koushikdutta.async.http.AsyncHttpRequest;
import com.koushikdutta.async.http.body.StringBody;
import com.koushikdutta.async.http.server.AsyncHttpServer;
import com.koushikdutta.async.http.server.AsyncHttpServerRequest;
import com.koushikdutta.async.http.server.AsyncHttpServerResponse;
import com.koushikdutta.async.http.server.HttpServerRequestCallback;
import junit.framework.TestCase;
import java.net.URI;
import java.util.concurrent.TimeoutException;
/**
* Created by koush on 7/11/13.
*/
public class TimeoutTests extends TestCase {
public TimeoutTests() {
server.get("/3", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
// never respond
AsyncServer.getDefault().postDelayed(new Runnable() {
@Override
public void run() {
response.send("3");
}
}, 1000);
}
});
server.post("/now", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
StringBody body = (StringBody)request.getBody();
response.send(body.get());
}
});
}
AsyncHttpServer server = new AsyncHttpServer();
@Override
protected void setUp() throws Exception {
super.setUp();
server.listen(AsyncServer.getDefault(), 5000);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
server.stop();
AsyncServer.getDefault().stop();
}
public void testTimeout() throws Exception {
AsyncHttpRequest req = new AsyncHttpRequest(URI.create("http://localhost:5000/3"), "GET");
req.setTimeout(1000);
try {
AsyncHttpClient.getDefaultInstance().executeString(req, null).get();
fail();
}
catch (Exception e) {
Log.d("timeout", "error", e);
assertTrue(e.getCause() instanceof TimeoutException);
}
req = new AsyncHttpRequest(URI.create("http://localhost:5000/3"), "GET");
assertEquals("3", AsyncHttpClient.getDefaultInstance().executeString(req, null).get());
}
public void testSlowBody() throws Exception {
AsyncHttpRequest req = new AsyncHttpRequest(URI.create("http://localhost:5000/now"), "POST");
req.setTimeout(1000);
req.setLogging("slowbody", Log.VERBOSE);
req.setBody(new DelayedStringBody("foo"));
assertEquals("foo", AsyncHttpClient.getDefaultInstance().executeString(req, null).get());
req = new AsyncHttpRequest(URI.create("http://localhost:5000/3"), "GET");
req.setLogging("slowbody", Log.VERBOSE);
req.setTimeout(100);
req.setBody(new DelayedStringBody("foo"));
try {
AsyncHttpClient.getDefaultInstance().executeString(req, null).get();
fail();
}
catch (Exception e) {
Log.d("timeout", "error", e);
assertTrue(e.getCause() instanceof TimeoutException);
}
}
class DelayedStringBody extends StringBody {
public DelayedStringBody(String value) {
super(value);
}
@Override
public void write(final AsyncHttpRequest request, final DataSink sink, final CompletedCallback completed) {
AsyncServer.getDefault().postDelayed(new Runnable() {
@Override
public void run() {
DelayedStringBody.super.write(request, sink, completed);
}
}, 1000);
}
}
}
|