Example usage for org.apache.thrift.transport TTransportException TTransportException

List of usage examples for org.apache.thrift.transport TTransportException TTransportException

Introduction

In this page you can find the example usage for org.apache.thrift.transport TTransportException TTransportException.

Prototype

public TTransportException() 

Source Link

Usage

From source file:com.palantir.atlasdb.keyvalue.cassandra.CassandraClientPoolTest.java

License:Open Source License

@Test
public void testIsConnectionException() {
    Assert.assertFalse(CassandraClientPool.isConnectionException(new TimedOutException()));
    Assert.assertFalse(CassandraClientPool.isConnectionException(new TTransportException()));
    Assert.assertTrue(/*  w w  w . jav a2 s  .c  o m*/
            CassandraClientPool.isConnectionException(new TTransportException(new SocketTimeoutException())));
}

From source file:com.palantir.atlasdb.keyvalue.cassandra.CassandraClientPoolTest.java

License:Open Source License

@Test
public void testIsRetriableException() {
    Assert.assertTrue(CassandraClientPool.isRetriableException(new TimedOutException()));
    Assert.assertTrue(CassandraClientPool.isRetriableException(new TTransportException()));
    Assert.assertTrue(//from  w  ww.  j av  a 2 s  .co m
            CassandraClientPool.isRetriableException(new TTransportException(new SocketTimeoutException())));
}

From source file:com.twitter.common.thrift.ThriftTest.java

License:Apache License

@Test
public void testDoCallThriftException() throws Exception {
    Capture<TTransport> transportCapture = new Capture<TTransport>();
    TestService testService = expectThriftError(transportCapture);
    TTransportException tException = new TTransportException();
    expect(testService.calculateMass("jake")).andThrow(tException);
    requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED),
            anyLong());//from  w w w .j a v  a2s. c o m

    Thrift<TestService> thrift = createThrift(expectUnusedExecutorService());

    control.replay();

    try {
        thrift.builder().blocking().create().calculateMass("jake");
        fail("Expected thrift exception to bubble unmodified");
    } catch (TException e) {
        assertSame(tException, e);
    }

    assertRequestsTotal(thrift, 1);
    assertErrorsTotal(thrift, 1);
    assertReconnectsTotal(thrift, 1);
    assertTimeoutsTotal(thrift, 0);

    assertTrue(transportCapture.hasCaptured());
    assertFalse("Expected the transport to be forcibly closed when a thrift error is encountered",
            transportCapture.getValue().isOpen());

    control.verify();
}

From source file:com.twitter.common.thrift.ThriftTest.java

License:Apache License

@Test
public void doCallAsyncThriftException() throws Exception {
    TTransportException tException = new TTransportException();

    expectAsyncServiceCall(true).calculateMass(eq("jake"), (AsyncMethodCallback) anyObject());
    expectLastCall().andThrow(tException);
    requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED),
            anyLong());//from ww w  .  j  a va 2s .  co m

    Thrift<TestServiceAsync> thrift = createAsyncThrift(expectUnusedExecutorService());

    callback.onError(tException);

    control.replay();

    thrift.builder().withConnectTimeout(ASYNC_CONNECT_TIMEOUT).create().calculateMass("jake", callback);

    assertRequestsTotal(thrift, 1);
    assertErrorsTotal(thrift, 1);
    assertReconnectsTotal(thrift, 1);
    assertTimeoutsTotal(thrift, 0);

    control.verify();
}

From source file:com.twitter.common.thrift.ThriftTest.java

License:Apache License

@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();/* w w w. jav a2s  .co m*/
    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();
}

From source file:com.twitter.common.thrift.ThriftTest.java

License:Apache License

@Test
public void testRetriesRecover() throws Exception {
    // 1st call//from   w w w  .j a  va 2  s  .  c  o m
    expect(expectServiceCall(true).calculateMass("jake")).andThrow(new TTransportException());
    requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED),
            anyLong());

    // 1st retry recovers
    expect(expectServiceCall(false).calculateMass("jake")).andReturn(42);
    requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.SUCCESS),
            anyLong());

    Thrift<TestService> thrift = createThrift(expectUnusedExecutorService());

    control.replay();

    TestService testService = thrift.builder().blocking().withRetries(1).create();

    assertEquals(42, testService.calculateMass("jake"));

    assertRequestsTotal(thrift, 1);
    assertErrorsTotal(thrift, 0);
    assertReconnectsTotal(thrift, 0);
    assertTimeoutsTotal(thrift, 0);

    control.verify();
}

From source file:com.twitter.common.thrift.ThriftTest.java

License:Apache License

@Test
public void testAsyncRetriesRecover() throws Exception {
    // Capture the callback that Thift has wrapped around our callback.
    Capture<AsyncMethodCallback<Integer>> callbackCapture = new Capture<AsyncMethodCallback<Integer>>();

    // 1st call//from   w  w  w.  j a v a  2 s .  com
    expectAsyncServiceCall(true).calculateMass(eq("jake"), capture(callbackCapture));
    expectLastCall().andThrow(new TTransportException());
    requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED),
            anyLong());

    // 1st retry recovers
    expectAsyncServiceRetry(false).calculateMass(eq("jake"), capture(callbackCapture));
    requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.SUCCESS),
            anyLong());

    // Verifies that our callback was called.
    callback.onComplete(42);

    Thrift<TestServiceAsync> thrift = createAsyncThrift(expectUnusedExecutorService());

    control.replay();

    thrift.builder().withRetries(1).withConnectTimeout(ASYNC_CONNECT_TIMEOUT).create().calculateMass("jake",
            callback);

    // Mimicks the async response from the server.
    callbackCapture.getValue().onComplete(42);

    assertRequestsTotal(thrift, 1);
    assertErrorsTotal(thrift, 0);
    assertReconnectsTotal(thrift, 0);
    assertTimeoutsTotal(thrift, 0);

    control.verify();
}

From source file:com.twitter.common.thrift.ThriftTest.java

License:Apache License

@Test
public void testRetriesFailure() throws Exception {
    // 1st call/*from   ww w .  j av a2s .  c om*/
    expect(expectServiceCall(true).calculateMass("jake")).andThrow(new TTransportException());
    requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED),
            anyLong());

    // 1st retry
    expect(expectServiceCall(true).calculateMass("jake")).andThrow(new TTransportException());
    requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED),
            anyLong());

    // 2nd retry
    TTransportException finalRetryException = new TTransportException();
    expect(expectServiceCall(true).calculateMass("jake")).andThrow(finalRetryException);
    requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED),
            anyLong());

    Thrift<TestService> thrift = createThrift(expectUnusedExecutorService());

    control.replay();

    TestService testService = thrift.builder().blocking().withRetries(2).create();

    try {
        testService.calculateMass("jake");
        fail("Expected an exception to be thrown since all retires failed");
    } catch (TException e) {
        assertSame(finalRetryException, e);
    }

    assertRequestsTotal(thrift, 1);
    assertErrorsTotal(thrift, 1);
    assertReconnectsTotal(thrift, 1);
    assertTimeoutsTotal(thrift, 0);

    control.verify();
}

From source file:com.twitter.common.thrift.ThriftTest.java

License:Apache License

@Test
public void testAsyncRetriesFailure() throws Exception {
    // 1st call//ww w.  j  a va 2  s  .  c o  m
    Capture<AsyncMethodCallback<Integer>> callbackCapture1 = new Capture<AsyncMethodCallback<Integer>>();
    expectAsyncServiceCall(true).calculateMass(eq("jake"), capture(callbackCapture1));
    requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED),
            anyLong());

    // 1st retry
    Capture<AsyncMethodCallback<Integer>> callbackCapture2 = new Capture<AsyncMethodCallback<Integer>>();
    expectAsyncServiceRetry(true).calculateMass(eq("jake"), capture(callbackCapture2));
    requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED),
            anyLong());

    // 2nd retry
    Capture<AsyncMethodCallback<Integer>> callbackCapture3 = new Capture<AsyncMethodCallback<Integer>>();
    expectAsyncServiceRetry(true).calculateMass(eq("jake"), capture(callbackCapture3));
    requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED),
            anyLong());

    // Verifies that our callback was called.
    TTransportException returnedException = new TTransportException();
    callback.onError(returnedException);

    Thrift<TestServiceAsync> thrift = createAsyncThrift(expectUnusedExecutorService());

    control.replay();

    thrift.builder().withRetries(2).withConnectTimeout(ASYNC_CONNECT_TIMEOUT).create().calculateMass("jake",
            callback);

    callbackCapture1.getValue().onError(new TTransportException());
    callbackCapture2.getValue().onError(new IOException());
    callbackCapture3.getValue().onError(returnedException);

    assertRequestsTotal(thrift, 1);
    assertErrorsTotal(thrift, 1);
    assertReconnectsTotal(thrift, 1);
    assertTimeoutsTotal(thrift, 0);

    control.verify();
}

From source file:com.twitter.common.thrift.ThriftTest.java

License:Apache License

@Test
public void testRetrySelection() throws Exception {
    expect(expectServiceCall(true).calculateMass("jake")).andThrow(new NotFoundException());
    requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED),
            anyLong());/*from  ww w  .j ava 2s .  c om*/

    // verify subclasses pass the retry filter
    class HopelesslyLost extends NotFoundException {
    }
    expect(expectServiceCall(true).calculateMass("jake")).andThrow(new HopelesslyLost());
    requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED),
            anyLong());

    TTransportException nonRetryableException = new TTransportException();
    expect(expectServiceCall(true).calculateMass("jake")).andThrow(nonRetryableException);
    requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED),
            anyLong());

    Thrift<TestService> thrift = createThrift(expectUnusedExecutorService());

    control.replay();

    TestService testService = thrift.builder().blocking().withRetries(2).retryOn(NotFoundException.class)
            .create();

    try {
        testService.calculateMass("jake");
        fail("Expected n exception to be thrown since all retires failed");
    } catch (TException e) {
        assertSame(nonRetryableException, e);
    }

    assertRequestsTotal(thrift, 1);
    assertErrorsTotal(thrift, 1);
    assertReconnectsTotal(thrift, 1);
    assertTimeoutsTotal(thrift, 0);

    control.verify();
}