Github user tzulitai commented on a diff in the pull request:

    https://github.com/apache/flink/pull/3314#discussion_r102898307
  
    --- Diff: 
flink-connectors/flink-connector-kafka-0.9/src/test/java/org/apache/flink/streaming/connectors/kafka/Kafka09FetcherTest.java
 ---
    @@ -422,6 +429,99 @@ public void run() {
                assertFalse("fetcher threads did not properly finish", 
sourceContext.isStillBlocking());
        }
     
    +   @Test
    +   public void testRichDeserializationSchema() throws Exception {
    +           final String topic = "test-topic";
    +           final int partition = 3;
    +           final byte[] payload = new byte[] {1, 2, 3, 4};
    +           final byte[] endPayload = 
"end".getBytes(StandardCharsets.UTF_8);
    +
    +           final List<ConsumerRecord<byte[], byte[]>> records = 
Arrays.asList(
    +                   new ConsumerRecord<>(topic, partition, 15, payload, 
payload),
    +                   new ConsumerRecord<>(topic, partition, 16, payload, 
payload),
    +                   new ConsumerRecord<>(topic, partition, 17, payload, 
endPayload));
    +
    +           final Map<TopicPartition, List<ConsumerRecord<byte[], byte[]>>> 
data = new HashMap<>();
    +           data.put(new TopicPartition(topic, partition), records);
    +
    +           final ConsumerRecords<byte[], byte[]> consumerRecords = new 
ConsumerRecords<>(data);
    +
    +           // ----- the test consumer -----
    +
    +           final KafkaConsumer<?, ?> mockConsumer = 
mock(KafkaConsumer.class);
    +           when(mockConsumer.poll(anyLong())).thenAnswer(new 
Answer<ConsumerRecords<?, ?>>() {
    +                   @Override
    +                   public ConsumerRecords<?, ?> answer(InvocationOnMock 
invocation) {
    +                           return consumerRecords;
    +                   }
    +           });
    +
    +           
whenNew(KafkaConsumer.class).withAnyArguments().thenReturn(mockConsumer);
    +
    +           // ----- build a fetcher -----
    +
    +           ArrayList<String> results = new ArrayList<>();
    +           SourceContext<String> sourceContext = new 
CollectingSourceContext<>(results, results);
    +           List<KafkaTopicPartition> topics = 
Collections.singletonList(new KafkaTopicPartition(topic, partition));
    +           RichKeyedDeserializationSchema<String> schema = new 
RichKeyedDeserializationSchema<String>() {
    +                   @Override
    +                   public void deserialize(
    +                           byte[] messageKey, byte[] message, String 
topic, int partition,
    +                           long offset, Collector<String> collector) 
throws IOException {
    +                           if (offset != 16) {
    +                                   collector.collect(new String(message));
    +                           }
    +                   }
    +
    +                   @Override
    +                   public boolean isEndOfStream(String nextElement) {
    +                           return nextElement.equals("end");
    +                   }
    +
    +                   @Override
    +                   public TypeInformation<String> getProducedType() {
    +                           return BasicTypeInfo.STRING_TYPE_INFO;
    +                   }
    +           };
    +
    +           final Kafka09Fetcher<String> fetcher = new Kafka09Fetcher<>(
    +                   sourceContext,
    +                   topics,
    +                   null, /* no restored state */
    +                   null, /* periodic watermark extractor */
    +                   null, /* punctuated watermark extractor */
    +                   new TestProcessingTimeService(),
    +                   10, /* watermark interval */
    +                   this.getClass().getClassLoader(),
    +                   false, /* checkpointing */
    +                   "task_name",
    +                   new UnregisteredMetricsGroup(),
    +                   schema,
    +                   new Properties(),
    +                   0L,
    +                   StartupMode.GROUP_OFFSETS,
    +                   false);
    +
    +
    +           // ----- run the fetcher -----
    +
    +           final AtomicReference<Throwable> error = new 
AtomicReference<>();
    +           final Thread fetcherRunner = new Thread("fetcher runner") {
    --- End diff --
    
    We have a nice utility `CheckedThread` that serves for the tested purpose 
here (catching errors and storing its reference).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to