Search in sources :

Example 1 with AsyncIface

use of com.twitter.common.thrift.ThriftFactoryTest.GoodService.AsyncIface in project commons by twitter.

the class ThriftFactoryTest method testCreateAsync.

@Test
public void testCreateAsync() throws IOException, InterruptedException, ThriftFactory.ThriftFactoryException {
    final String[] responseHolder = new String[] { null };
    final CountDownLatch done = new CountDownLatch(1);
    AsyncMethodCallback<String> callback = new AsyncMethodCallback<String>() {

        @Override
        public void onComplete(String response) {
            responseHolder[0] = response;
            done.countDown();
        }

        @Override
        public void onError(Throwable throwable) {
            responseHolder[0] = throwable.toString();
            done.countDown();
        }
    };
    final Thrift<AsyncIface> thrift = ThriftFactory.create(GoodService.AsyncIface.class).withMaxConnectionsPerEndpoint(1).useFramedTransport(true).buildAsync(ImmutableSet.of(new InetSocketAddress(1234)));
    addTearDown(new TearDown() {

        @Override
        public void tearDown() {
            thrift.close();
        }
    });
    GoodService.AsyncIface client = thrift.builder().blocking().create();
    client.doWork(callback);
    assertTrue("wasn't called back in time, callback got " + responseHolder[0], done.await(5000, TimeUnit.MILLISECONDS));
    assertEquals(GoodService.DONE, responseHolder[0]);
}
Also used : TearDown(com.google.common.testing.TearDown) AsyncIface(com.twitter.common.thrift.ThriftFactoryTest.GoodService.AsyncIface) InetSocketAddress(java.net.InetSocketAddress) AsyncIface(com.twitter.common.thrift.ThriftFactoryTest.GoodService.AsyncIface) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncMethodCallback(org.apache.thrift.async.AsyncMethodCallback) Test(org.junit.Test)

Aggregations

TearDown (com.google.common.testing.TearDown)1 AsyncIface (com.twitter.common.thrift.ThriftFactoryTest.GoodService.AsyncIface)1 InetSocketAddress (java.net.InetSocketAddress)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AsyncMethodCallback (org.apache.thrift.async.AsyncMethodCallback)1 Test (org.junit.Test)1