// set up async client manager
TAsyncClientManager acm = new TAsyncClientManager();
// connect an async client
TNonblockingSocket clientSock = new TNonblockingSocket(
ServerTestBase.HOST, ServerTestBase.PORT);
Srv.AsyncClient client = new Srv.AsyncClient(new TBinaryProtocol.Factory(), acm, clientSock);
// make a standard method call
standardCallTest(client);
// make a standard method call that succeeds within timeout
assertFalse(s.isStopped());
client.setTimeout(5000);
standardCallTest(client);
// make a void method call
assertFalse(s.isStopped());
final CountDownLatch voidLatch = new CountDownLatch(1);
final AtomicBoolean voidMethodReturned = new AtomicBoolean(false);
client.voidMethod(new FailureLessCallback<Srv.AsyncClient.voidMethod_call>() {
@Override
public void onComplete(voidMethod_call response) {
try {
response.getResult();
voidMethodReturned.set(true);
} catch (TException e) {
fail(e);
} finally {
voidLatch.countDown();
}
}
});
voidLatch.await(1, TimeUnit.SECONDS);
assertTrue(voidMethodReturned.get());
// make a oneway method call
assertFalse(s.isStopped());
final CountDownLatch onewayLatch = new CountDownLatch(1);
final AtomicBoolean onewayReturned = new AtomicBoolean(false);
client.onewayMethod(new FailureLessCallback<onewayMethod_call>() {
@Override
public void onComplete(onewayMethod_call response) {
try {
response.getResult();
onewayReturned.set(true);
} catch (TException e) {
fail(e);
} finally {
onewayLatch.countDown();
}
}
});
onewayLatch.await(1, TimeUnit.SECONDS);
assertTrue(onewayReturned.get());
// make another standard method call
assertFalse(s.isStopped());
final CountDownLatch voidAfterOnewayLatch = new CountDownLatch(1);
final AtomicBoolean voidAfterOnewayReturned = new AtomicBoolean(false);
client.voidMethod(new FailureLessCallback<voidMethod_call>() {
@Override
public void onComplete(voidMethod_call response) {
try {
response.getResult();
voidAfterOnewayReturned.set(true);
} catch (TException e) {
fail(e);
} finally {
voidAfterOnewayLatch.countDown();
}
}
});
voidAfterOnewayLatch.await(1, TimeUnit.SECONDS);
assertTrue(voidAfterOnewayReturned.get());
// make multiple calls with deserialization in the selector thread (repro Eric's issue)
assertFalse(s.isStopped());
int numThreads = 50;
int numCallsPerThread = 100;
List<JankyRunnable> runnables = new ArrayList<JankyRunnable>();
List<Thread> threads = new ArrayList<Thread>();
for (int i = 0; i < numThreads; i++) {
JankyRunnable runnable = new JankyRunnable(acm, numCallsPerThread);
Thread thread = new Thread(runnable);
thread.start();
threads.add(thread);
runnables.add(runnable);
}
for (Thread thread : threads) {
thread.join();
}
int numSuccesses = 0;
for (JankyRunnable runnable : runnables) {
numSuccesses += runnable.getNumSuccesses();
}
assertEquals(numThreads * numCallsPerThread, numSuccesses);
// check that timeouts work
assertFalse(s.isStopped());
assertTrue(clientSock.isOpen());
final CountDownLatch timeoutLatch = new CountDownLatch(1);
client.setTimeout(100);
client.primitiveMethod(new AsyncMethodCallback<primitiveMethod_call>() {
@Override
public void onError(Throwable throwable) {
try {
if (!(throwable instanceof TimeoutException)) {
StringWriter sink = new StringWriter();
throwable.printStackTrace(new PrintWriter(sink, true));
fail("expected TimeoutException but got " + sink.toString());
}
} finally {
timeoutLatch.countDown();
}
}
@Override
public void onComplete(primitiveMethod_call response) {
try {
fail("should not have finished timed out call.");
} finally {
timeoutLatch.countDown();
}
}
});
timeoutLatch.await(2, TimeUnit.SECONDS);
assertTrue(client.hasError());
assertTrue(client.getError() instanceof TimeoutException);
// error closes socket and make sure isOpen reflects that
assertFalse(clientSock.isOpen());
}