use of org.apache.thrift.transport.TTransportException in project commons by twitter.
the class ThriftTest method testAsyncRetrySelection.
@Test
public void testAsyncRetrySelection() throws Exception {
// verify subclasses pass the retry filter
class HopelesslyLost extends NotFoundException {
}
Capture<AsyncMethodCallback<Integer>> callbackCapture1 = new Capture<AsyncMethodCallback<Integer>>();
expectAsyncServiceCall(true).calculateMass(eq("jake"), capture(callbackCapture1));
requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED), anyLong());
Capture<AsyncMethodCallback<Integer>> callbackCapture2 = new Capture<AsyncMethodCallback<Integer>>();
expectAsyncServiceRetry(true).calculateMass(eq("jake"), capture(callbackCapture2));
requestTracker.requestResult((InetSocketAddress) anyObject(), eq(RequestTracker.RequestResult.FAILED), anyLong());
// Verifies that our callback was called.
TTransportException nonRetryableException = new TTransportException();
callback.onError(nonRetryableException);
Thrift<TestServiceAsync> thrift = createAsyncThrift(expectUnusedExecutorService());
control.replay();
TestServiceAsync testService = thrift.builder().withRetries(2).retryOn(NotFoundException.class).withConnectTimeout(ASYNC_CONNECT_TIMEOUT).create();
testService.calculateMass("jake", callback);
callbackCapture1.getValue().onError(new HopelesslyLost());
callbackCapture2.getValue().onError(nonRetryableException);
assertRequestsTotal(thrift, 1);
assertErrorsTotal(thrift, 1);
assertReconnectsTotal(thrift, 1);
assertTimeoutsTotal(thrift, 0);
control.verify();
}
use of org.apache.thrift.transport.TTransportException in project commons by twitter.
the class ThriftTest method testAsyncRetriesRecover.
@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
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();
}
use of org.apache.thrift.transport.TTransportException in project commons by twitter.
the class ThriftTest method testRetriesRecover.
@Test
public void testRetriesRecover() throws Exception {
// 1st call
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();
}
use of org.apache.thrift.transport.TTransportException in project commons by twitter.
the class ThriftTest method testDoCallThriftException.
@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());
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();
}
use of org.apache.thrift.transport.TTransportException in project commons by twitter.
the class TTextProtocol method writeSequenceBegin.
/**
* Helper shared by write{List/Set}Begin
*/
private void writeSequenceBegin(int size) throws TException {
getCurrentContext().write();
if (getCurrentContext().isMapKey()) {
throw new TException(SEQUENCE_AS_KEY_ILLEGAL);
}
pushContext(new SequenceContext(null));
try {
getCurrentWriter().beginArray();
} catch (IOException ex) {
throw new TTransportException(ex);
}
}
Aggregations