Search in sources :

Example 1 with AlwaysThrowsDeserializationSchema

use of org.apache.flink.streaming.connectors.kinesis.testutils.AlwaysThrowsDeserializationSchema in project flink by apache.

the class KinesisDataFetcherTest method testOriginalExceptionIsPreservedWhenInterruptedDuringShutdown.

@Test
public void testOriginalExceptionIsPreservedWhenInterruptedDuringShutdown() throws Exception {
    String stream = "fakeStream";
    Map<String, List<BlockingQueue<String>>> streamsToShardQueues = new HashMap<>();
    LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>(10);
    queue.put("item1");
    streamsToShardQueues.put(stream, singletonList(queue));
    AlwaysThrowsDeserializationSchema deserializationSchema = new AlwaysThrowsDeserializationSchema();
    KinesisProxyInterface fakeKinesis = FakeKinesisBehavioursFactory.blockingQueueGetRecords(streamsToShardQueues);
    TestableKinesisDataFetcherForShardConsumerException<String> fetcher = new TestableKinesisDataFetcherForShardConsumerException<>(singletonList(stream), new TestSourceContext<>(), TestUtils.getStandardProperties(), new KinesisDeserializationSchemaWrapper<>(deserializationSchema), 10, 2, new AtomicReference<>(), new LinkedList<>(), new HashMap<>(), fakeKinesis, (sequence, properties, metricGroup, streamShardHandle) -> mock(RecordPublisher.class));
    DummyFlinkKinesisConsumer<String> consumer = new DummyFlinkKinesisConsumer<>(TestUtils.getStandardProperties(), fetcher, 1, 0);
    CheckedThread consumerThread = new CheckedThread("FlinkKinesisConsumer") {

        @Override
        public void go() throws Exception {
            consumer.run(new TestSourceContext<>());
        }
    };
    consumerThread.start();
    fetcher.waitUntilRun();
    // ShardConsumer exception (from deserializer) will result in fetcher being shut down.
    fetcher.waitUntilShutdown(20, TimeUnit.SECONDS);
    // Ensure that KinesisDataFetcher has exited its while(running) loop and is inside its
    // awaitTermination()
    // method before we interrupt its thread, so that our interrupt doesn't get absorbed by any
    // other mechanism.
    fetcher.waitUntilAwaitTermination(20, TimeUnit.SECONDS);
    // Interrupt the thread so that KinesisDataFetcher#awaitTermination() will throw
    // InterruptedException.
    consumerThread.interrupt();
    try {
        consumerThread.sync();
    } catch (InterruptedException e) {
        fail("Expected exception from deserializer, but got InterruptedException, probably from " + "KinesisDataFetcher, which obscures the cause of the failure. " + e);
    } catch (RuntimeException e) {
        if (!e.getMessage().equals(AlwaysThrowsDeserializationSchema.EXCEPTION_MESSAGE)) {
            fail("Expected exception from deserializer, but got: " + e);
        }
    } catch (Exception e) {
        fail("Expected exception from deserializer, but got: " + e);
    }
    assertTrue("Expected Fetcher to have been interrupted. This test didn't accomplish its goal.", fetcher.wasInterrupted);
}
Also used : TestableKinesisDataFetcherForShardConsumerException(org.apache.flink.streaming.connectors.kinesis.testutils.TestableKinesisDataFetcherForShardConsumerException) HashMap(java.util.HashMap) AlwaysThrowsDeserializationSchema(org.apache.flink.streaming.connectors.kinesis.testutils.AlwaysThrowsDeserializationSchema) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CheckedThread(org.apache.flink.core.testutils.CheckedThread) TestableKinesisDataFetcherForShardConsumerException(org.apache.flink.streaming.connectors.kinesis.testutils.TestableKinesisDataFetcherForShardConsumerException) RecordPublisher(org.apache.flink.streaming.connectors.kinesis.internals.publisher.RecordPublisher) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) KinesisProxyInterface(org.apache.flink.streaming.connectors.kinesis.proxy.KinesisProxyInterface) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 Collections.singletonList (java.util.Collections.singletonList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 CheckedThread (org.apache.flink.core.testutils.CheckedThread)1 RecordPublisher (org.apache.flink.streaming.connectors.kinesis.internals.publisher.RecordPublisher)1 KinesisProxyInterface (org.apache.flink.streaming.connectors.kinesis.proxy.KinesisProxyInterface)1 AlwaysThrowsDeserializationSchema (org.apache.flink.streaming.connectors.kinesis.testutils.AlwaysThrowsDeserializationSchema)1 TestableKinesisDataFetcherForShardConsumerException (org.apache.flink.streaming.connectors.kinesis.testutils.TestableKinesisDataFetcherForShardConsumerException)1 Test (org.junit.Test)1