samedi 10 janvier 2015

How to test my API client for request limitation or throttling

I am developing a client for an API and am using 'google http java client' for creating the http request and response. Below is the code for it. Also, the API would return '503' if there are more than 10 requests in a minute and I want my code to sleep for a minute if this happens. Now, the problem I am facing is that I do not know how to test it i.e how to create 10 mock request which are run under one minute and the 11th request results in 503 which then sleeps the thread. I am not comfartable with threads and testing so thats why the question.



public static class RequestInitializer implements HttpRequestInitializer, HttpUnsuccessfulResponseHandler {

@Override
public void initialize(HttpRequest request) throws IOException {
request.setUnsuccessfulResponseHandler(this);
}

@Override
public boolean handleResponse(HttpRequest request, HttpResponse response,
boolean retrySupported) throws IOException {
int statusCode = response.getStatusCode();
if (statusCode == 503) {
System.out.println("in 503");
try {
Thread.sleep(60000);
return true;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return false;
}


}

private static Future<HttpResponse> executeRequest(final Module module, final String prevDate, Executor executor) throws IOException{

HttpRequestFactory requestFactory =
HTTP_TRANSPORT.createRequestFactory(new RequestInitializer());

GenericUrl url = new GenericUrl(BASE_URL + ACCOUNT_ID + "/" + module.name + module.uriExtension);
url = url.set("load_relations", "all").set("timeStamp", ">=," + prevDate);
HttpRequest request = requestFactory.buildGetRequest(url);

HttpHeaders headers = new HttpHeaders();
headers.setAuthorization("OAuth " + ACCESS_TOKEN);
headers.setAccept("application/vnd.merchantos­v2+json");
request.setHeaders(headers);

return request.executeAsync(executor);

}

Aucun commentaire:

Enregistrer un commentaire