Skip to content

Instantly share code, notes, and snippets.

@bluerogue
Created July 7, 2015 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluerogue/c29e4f10bc8240c8bfea to your computer and use it in GitHub Desktop.
Save bluerogue/c29e4f10bc8240c8bfea to your computer and use it in GitHub Desktop.
HTTP calls testing for concurrency issues within an API
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
/**
* Tests to ensure concurrent requests to the API are processed properly. IDs
* passed in are compared with the ID returned. If the IDs do not match, this
* indicates a concurrency issue within the API.
*
* Uses Jersey for HTTP calls.
*
* @author Matthew Roberts
* matthew.bowen.roberts@gmail.com
* matthewroberts.org
*
*/
public class RestConcurrencyTest {
private static final String[] IDS = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21" };
private static final CountDownLatch countDownLatch = new CountDownLatch(IDS.length);
private static final String API_ENDPOINT = "http://localhost:8080/api/";
private static final String USERNAME = "user";
private static final String PASSWORD = "pass";
private static Map<String, String> results = new HashMap<String, String>();
public static void main(String[] args) {
/*
for (int i = 0; i < IDS.length; i++) {
runSegmentedRequests(IDS[i]);
}
*/
ExecutorService executorService = Executors.newFixedThreadPool(IDS.length);
for (int i = 0; i < IDS.length; i++) {
Runnable workerThread = new ApiThread(IDS[i]);
executorService.execute(workerThread);
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
executorService.shutdown();
System.out.println();
String completeMatch = "SUCCESS";
for (String key : results.keySet()) {
if (!key.equals(results.get(key))) {
System.out.println(key + " != " + results.get(key));
completeMatch = "FAILURE";
}
}
System.out.println(completeMatch);
}
private static void runSegmentedRequests(String id) {
System.out.println("STARTING " + Thread.currentThread().getName() + " for ID " + id);
try {
Client client = Client.create();
client.addFilter(new com.sun.jersey.api.client.filter.HTTPBasicAuthFilter(USERNAME, PASSWORD));
WebResource webResource = client.resource(API_ENDPOINT + id);
ClientResponse response = webResource.type("application/json")
.get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus() + " " + response.toString());
}
String output = response.getEntity(String.class);
results.put(id, output.substring(8, 15));
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("FINISHING " + Thread.currentThread().getName() + " for ID " + id);
}
private static class ApiThread implements Runnable {
private String id;
public ApiThread(String id) {
this.id = id;
}
@Override
public void run() {
System.out.println("STARTING " + Thread.currentThread().getName() + " for ID " + id);
getResponse(id);
System.out.println("FINISHING " + Thread.currentThread().getName() + " for ID " + id);
countDownLatch.countDown();
}
private static void getResponse(String id) {
Client client = Client.create();
client.addFilter(new com.sun.jersey.api.client.filter.HTTPBasicAuthFilter(USERNAME, PASSWORD));
WebResource webResource = client.resource(API_ENDPOINT + id);
ClientResponse response = webResource.type("application/json").get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus() + " " + response.toString());
}
String output = response.getEntity(String.class);
results.put(id, output.substring(8, 15));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment