DefaultHttpClient client = new DefaultHttpClient(mgr, params);
HttpContext[] contexts = new HttpContext[workerCount];
HttpWorker[] workers = new HttpWorker[workerCount];
for (int i = 0; i < contexts.length; i++) {
HttpContext context = new BasicHttpContext();
context.setAttribute("user", Integer.valueOf(i));
contexts[i] = context;
workers[i] = new HttpWorker(context, requestCount, target, client);
}
client.setUserTokenHandler(new UserTokenHandler() {
public Object getUserToken(final HttpContext context) {
Integer id = (Integer) context.getAttribute("user");
return id;
}
});
for (int i = 0; i < workers.length; i++) {
workers[i].start();
}
for (int i = 0; i < workers.length; i++) {
workers[i].join(10000);
}
for (int i = 0; i < workers.length; i++) {
Exception ex = workers[i].getException();
if (ex != null) {
throw ex;
}
assertEquals(requestCount, workers[i].getCount());
}
for (int i = 0; i < contexts.length; i++) {
HttpContext context = contexts[i];
Integer id = (Integer) context.getAttribute("user");
for (int r = 0; r < requestCount; r++) {
Integer state = (Integer) context.getAttribute("r" + r);
assertNotNull(state);
assertEquals(id, state);
}
}