[OLINGO-659] Improved async test case

This commit is contained in:
Michael Bolz 2015-08-17 10:12:08 +02:00
parent e6d1b964f9
commit 5248f34215
1 changed files with 25 additions and 4 deletions

View File

@ -60,6 +60,7 @@ public class BasicAsyncITCase extends AbstractBaseTestITCase {
private static final String CRLF = "\r\n";
private static final String DEFAULT_ENCODING = "utf-8";
public static final long SLEEP_TIMEOUT_IN_MS = 200;
/**
* Works
@ -87,8 +88,8 @@ public class BasicAsyncITCase extends AbstractBaseTestITCase {
assertEquals(0, statusBody.byteLength());
// get async response (now finished)
TimeUnit.SECONDS.sleep(2);
HttpURLConnection result = getRequest(new URL(respondUri), Collections.<String, String>emptyMap());
HttpURLConnection result = waitTillDone(respondUri, 4);
StringHelper.Stream resultBody = StringHelper.toStream(result.getInputStream());
Map<String, List<String>> resultHeaderFields = result.getHeaderFields();
String resBody = resultBody.asString();
@ -131,8 +132,7 @@ public class BasicAsyncITCase extends AbstractBaseTestITCase {
assertEquals(0, statusBody.byteLength());
// get async response (now finished)
TimeUnit.SECONDS.sleep(2);
HttpURLConnection result = getRequest(new URL(respondUri), Collections.<String, String>emptyMap());
HttpURLConnection result = waitTillDone(respondUri, 4);
StringHelper.Stream resultBody = StringHelper.toStream(result.getInputStream());
Map<String, List<String>> resultHeaderFields = result.getHeaderFields();
String resBody = resultBody.asString();
@ -184,6 +184,27 @@ public class BasicAsyncITCase extends AbstractBaseTestITCase {
+ "--" + DEFAULT_BATCH_BOUNDARY + "--";
}
private HttpURLConnection waitTillDone(String location, int maxWaitInSeconds) throws Exception {
HttpURLConnection result = null;
int waitCounter = maxWaitInSeconds * 1000;
while(result == null && waitCounter > 0) {
HttpURLConnection statusRequest = getRequest(new URL(location), Collections.<String, String>emptyMap());
Map<String, List<String>> statusHeaderFields = statusRequest.getHeaderFields();
String statusHeader = statusHeaderFields.get(null).get(0);
if("HTTP/1.1 202 Accepted".equals(statusHeader)) {
TimeUnit.MILLISECONDS.sleep(SLEEP_TIMEOUT_IN_MS);
waitCounter -= SLEEP_TIMEOUT_IN_MS;
} else if("HTTP/1.1 200 OK".equals(statusHeader)) {
result = statusRequest;
} else {
throw new RuntimeException("Unexpected status header ('" + statusHeader +
"') for async status request on: " + location);
}
}
return result;
}
private HttpURLConnection postRequest(final URL url, final String content, final Map<String, String> headers)
throws IOException {
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();