Search in sources :

Example 1 with AmazonKinesisException

use of com.amazonaws.services.kinesis.model.AmazonKinesisException in project flink by apache.

the class KinesisProxyTest method testGetShardListRetry.

@Test
public void testGetShardListRetry() throws Exception {
    Properties kinesisConsumerConfig = new Properties();
    kinesisConsumerConfig.setProperty(ConsumerConfigConstants.AWS_REGION, "us-east-1");
    Shard shard = new Shard();
    shard.setShardId("fake-shard-000000000000");
    final ListShardsResult expectedResult = new ListShardsResult();
    expectedResult.withShards(shard);
    MutableInt exceptionCount = new MutableInt();
    final Throwable[] retriableExceptions = new Throwable[] { new AmazonKinesisException("attempt1"), new AmazonKinesisException("attempt2") };
    AmazonKinesisClient mockClient = mock(AmazonKinesisClient.class);
    Mockito.when(mockClient.listShards(any())).thenAnswer(new Answer<ListShardsResult>() {

        @Override
        public ListShardsResult answer(InvocationOnMock invocation) throws Throwable {
            if (exceptionCount.intValue() < retriableExceptions.length) {
                exceptionCount.increment();
                throw retriableExceptions[exceptionCount.intValue() - 1];
            }
            return expectedResult;
        }
    });
    KinesisProxy kinesisProxy = new KinesisProxy(kinesisConsumerConfig);
    Whitebox.getField(KinesisProxy.class, "kinesisClient").set(kinesisProxy, mockClient);
    HashMap<String, String> streamNames = new HashMap();
    streamNames.put("fake-stream", null);
    GetShardListResult result = kinesisProxy.getShardList(streamNames);
    assertEquals(retriableExceptions.length, exceptionCount.intValue());
    assertEquals(true, result.hasRetrievedShards());
    assertEquals(shard.getShardId(), result.getLastSeenShardOfStream("fake-stream").getShard().getShardId());
    // test max attempt count exceeded
    int maxRetries = 1;
    exceptionCount.setValue(0);
    kinesisConsumerConfig.setProperty(ConsumerConfigConstants.LIST_SHARDS_RETRIES, String.valueOf(maxRetries));
    kinesisProxy = new KinesisProxy(kinesisConsumerConfig);
    Whitebox.getField(KinesisProxy.class, "kinesisClient").set(kinesisProxy, mockClient);
    try {
        kinesisProxy.getShardList(streamNames);
        Assert.fail("exception expected");
    } catch (SdkClientException ex) {
        assertEquals(retriableExceptions[maxRetries], ex);
    }
    assertEquals(maxRetries + 1, exceptionCount.intValue());
}
Also used : ListShardsResult(com.amazonaws.services.kinesis.model.ListShardsResult) AmazonKinesisException(com.amazonaws.services.kinesis.model.AmazonKinesisException) AmazonKinesisClient(com.amazonaws.services.kinesis.AmazonKinesisClient) HashMap(java.util.HashMap) Properties(java.util.Properties) SdkClientException(com.amazonaws.SdkClientException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MutableInt(org.apache.commons.lang3.mutable.MutableInt) Shard(com.amazonaws.services.kinesis.model.Shard) Test(org.junit.Test)

Example 2 with AmazonKinesisException

use of com.amazonaws.services.kinesis.model.AmazonKinesisException in project flink by apache.

the class KinesisProxyTest method testGetRecordsRetry.

@Test
public void testGetRecordsRetry() throws Exception {
    Properties kinesisConsumerConfig = new Properties();
    kinesisConsumerConfig.setProperty(ConsumerConfigConstants.AWS_REGION, "us-east-1");
    final GetRecordsResult expectedResult = new GetRecordsResult();
    MutableInt retries = new MutableInt();
    final Throwable[] retriableExceptions = new Throwable[] { new AmazonKinesisException("mock") };
    AmazonKinesisClient mockClient = mock(AmazonKinesisClient.class);
    Mockito.when(mockClient.getRecords(any())).thenAnswer(new Answer<GetRecordsResult>() {

        @Override
        public GetRecordsResult answer(InvocationOnMock invocation) throws Throwable {
            if (retries.intValue() < retriableExceptions.length) {
                retries.increment();
                throw retriableExceptions[retries.intValue() - 1];
            }
            return expectedResult;
        }
    });
    KinesisProxy kinesisProxy = new KinesisProxy(kinesisConsumerConfig);
    Whitebox.getField(KinesisProxy.class, "kinesisClient").set(kinesisProxy, mockClient);
    GetRecordsResult result = kinesisProxy.getRecords("fakeShardIterator", 1);
    assertEquals(retriableExceptions.length, retries.intValue());
    assertEquals(expectedResult, result);
}
Also used : AmazonKinesisException(com.amazonaws.services.kinesis.model.AmazonKinesisException) GetRecordsResult(com.amazonaws.services.kinesis.model.GetRecordsResult) AmazonKinesisClient(com.amazonaws.services.kinesis.AmazonKinesisClient) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MutableInt(org.apache.commons.lang3.mutable.MutableInt) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

AmazonKinesisClient (com.amazonaws.services.kinesis.AmazonKinesisClient)2 AmazonKinesisException (com.amazonaws.services.kinesis.model.AmazonKinesisException)2 Properties (java.util.Properties)2 MutableInt (org.apache.commons.lang3.mutable.MutableInt)2 Test (org.junit.Test)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 SdkClientException (com.amazonaws.SdkClientException)1 GetRecordsResult (com.amazonaws.services.kinesis.model.GetRecordsResult)1 ListShardsResult (com.amazonaws.services.kinesis.model.ListShardsResult)1 Shard (com.amazonaws.services.kinesis.model.Shard)1 HashMap (java.util.HashMap)1