Search in sources :

Example 1 with ForwardingExecutorService

use of com.twitter.common.util.concurrent.ForwardingExecutorService in project commons by twitter.

the class ThriftTest method testDoCallDeadlineExpired.

@Test
@Ignore("Flaky: https://trac.twitter.com/twttr/ticket/11474")
public void testDoCallDeadlineExpired() throws Exception {
    TestService testService = expectServiceCall(true);
    // Setup a way to verify the callable was cancelled by Thrift when timeout elapsed
    final CountDownLatch remoteCallComplete = new CountDownLatch(1);
    final CountDownLatch remoteCallStarted = new CountDownLatch(1);
    final Command verifyCancelled = control.createMock(Command.class);
    verifyCancelled.execute();
    final Object block = new Object();
    expect(testService.calculateMass("jake")).andAnswer(new IAnswer<Integer>() {

        @Override
        public Integer answer() throws TException {
            try {
                synchronized (block) {
                    remoteCallStarted.countDown();
                    block.wait();
                }
                fail("Expected late work to be cancelled and interrupted");
            } catch (InterruptedException e) {
                verifyCancelled.execute();
            } finally {
                remoteCallComplete.countDown();
            }
            throw new TTransportException();
        }
    });
    requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.TIMEOUT), anyLong());
    ExecutorService executorService = new ForwardingExecutorService<ExecutorService>(Executors.newSingleThreadExecutor()) {

        @Override
        public <T> Future<T> submit(Callable<T> task) {
            Future<T> future = super.submit(task);
            // make sure the task is started so we can verify it gets cancelled
            try {
                remoteCallStarted.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return future;
        }
    };
    Thrift<TestService> thrift = createThrift(executorService);
    control.replay();
    try {
        thrift.builder().withRequestTimeout(Amount.of(1L, Time.NANOSECONDS)).create().calculateMass("jake");
        fail("Expected a timeout");
    } catch (TTimeoutException e) {
    // expected
    } finally {
        remoteCallComplete.await();
    }
    assertRequestsTotal(thrift, 0);
    assertErrorsTotal(thrift, 0);
    assertReconnectsTotal(thrift, 0);
    assertTimeoutsTotal(thrift, 1);
    control.verify();
}
Also used : TException(org.apache.thrift.TException) TTransportException(org.apache.thrift.transport.TTransportException) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) ForwardingExecutorService(com.twitter.common.util.concurrent.ForwardingExecutorService) Command(com.twitter.common.base.Command) ForwardingExecutorService(com.twitter.common.util.concurrent.ForwardingExecutorService) ExecutorService(java.util.concurrent.ExecutorService) EasyMock.anyObject(org.easymock.EasyMock.anyObject) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Command (com.twitter.common.base.Command)1 ForwardingExecutorService (com.twitter.common.util.concurrent.ForwardingExecutorService)1 Callable (java.util.concurrent.Callable)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 TException (org.apache.thrift.TException)1 TTransportException (org.apache.thrift.transport.TTransportException)1 EasyMock.anyObject (org.easymock.EasyMock.anyObject)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1