shanthoosh commented on a change in pull request #918: SAMZA-2094: Implement the StartpointVisitor for the KafkaSystemConsumer. URL: https://github.com/apache/samza/pull/918#discussion_r267022260
########## File path: samza-kafka/src/test/java/org/apache/samza/system/kafka/TestKafkaSystemConsumer.java ########## @@ -206,6 +225,166 @@ public void testFetchThresholdBytesDiabled() { consumer.stop(); } + @Test + public void testStartpointSpecificOffsetVisitorShouldUpdateTheFetchOffsetInConsumer() { + final KafkaConsumer consumer = Mockito.mock(KafkaConsumer.class); + final KafkaConsumerProxy kafkaConsumerProxy = Mockito.mock(KafkaConsumerProxy.class); + KafkaSystemConsumer kafkaSystemConsumer = new KafkaSystemConsumer(consumer, TEST_SYSTEM, new MapConfig(), TEST_CLIENT_ID, kafkaConsumerProxy, Mockito.mock(KafkaSystemConsumerMetrics.class), new TestClock(), REGISTRATION_HANDLER); + KafkaStartpointRegistrationHandler kafkaStartpointRegistrationHandler = kafkaSystemConsumer.new KafkaStartpointRegistrationHandler(); + + final StartpointSpecific testStartpointSpecific = new StartpointSpecific(TEST_OFFSET); + + // Mock the consumer interactions. + Mockito.doNothing().when(consumer).seek(TEST_TOPIC_PARTITION, Long.valueOf(TEST_OFFSET)); + + // Invoke the consumer with startpoint. + kafkaStartpointRegistrationHandler.visit(TEST_SYSTEM_STREAM_PARTITION, testStartpointSpecific); + + // Mock verifications. + Mockito.verify(consumer).seek(TEST_TOPIC_PARTITION, Long.valueOf(TEST_OFFSET)); + Mockito.verify(kafkaConsumerProxy).addTopicPartition(Mockito.any(SystemStreamPartition.class), Mockito.anyLong()); + } + + @Test + public void testStartpointTimestampVisitorShouldUpdateTheFetchOffsetInConsumer() { + // Define dummy variables for testing. + final Long testTimeStamp = 10L; + + final KafkaConsumer consumer = Mockito.mock(KafkaConsumer.class); + final KafkaConsumerProxy kafkaConsumerProxy = Mockito.mock(KafkaConsumerProxy.class); + KafkaSystemConsumer kafkaSystemConsumer = new KafkaSystemConsumer(consumer, TEST_SYSTEM, new MapConfig(), TEST_CLIENT_ID, kafkaConsumerProxy, Mockito.mock(KafkaSystemConsumerMetrics.class), new TestClock(), REGISTRATION_HANDLER); + + KafkaStartpointRegistrationHandler kafkaStartpointRegistrationHandler = kafkaSystemConsumer.new KafkaStartpointRegistrationHandler(); + + final StartpointTimestamp startpointTimestamp = new StartpointTimestamp(testTimeStamp); + final Map<TopicPartition, OffsetAndTimestamp> offsetForTimesResult = ImmutableMap.of( + TEST_TOPIC_PARTITION, new OffsetAndTimestamp(Long.valueOf(TEST_OFFSET), testTimeStamp)); + + // Mock the consumer interactions. + Mockito.when(consumer.offsetsForTimes(Mockito.anyMap())).thenReturn(offsetForTimesResult); + Mockito.doNothing().when(consumer).seek(TEST_TOPIC_PARTITION, Long.valueOf(TEST_OFFSET)); + + kafkaStartpointRegistrationHandler.visit(TEST_SYSTEM_STREAM_PARTITION, startpointTimestamp); + + // Mock verifications. + Mockito.verify(consumer).seek(TEST_TOPIC_PARTITION, Long.valueOf(TEST_OFFSET)); + Mockito.verify(consumer).offsetsForTimes(Mockito.anyMap()); + Mockito.verify(kafkaConsumerProxy).addTopicPartition(Mockito.any(SystemStreamPartition.class), Mockito.anyLong()); + } + + @Test + public void testStartpointTimestampVisitorShouldMoveTheConsumerToEndWhenTimestampDoesNotExist() { + final KafkaConsumer consumer = Mockito.mock(KafkaConsumer.class); + final KafkaConsumerProxy kafkaConsumerProxy = Mockito.mock(KafkaConsumerProxy.class); + + + KafkaSystemConsumer kafkaSystemConsumer = new KafkaSystemConsumer(consumer, TEST_SYSTEM, new MapConfig(), TEST_CLIENT_ID, kafkaConsumerProxy, Mockito.mock(KafkaSystemConsumerMetrics.class), new TestClock(), REGISTRATION_HANDLER); + KafkaStartpointRegistrationHandler kafkaStartpointRegistrationHandler = kafkaSystemConsumer.new KafkaStartpointRegistrationHandler(); + + final StartpointTimestamp startpointTimestamp = new StartpointTimestamp(0L); + final Map<TopicPartition, OffsetAndTimestamp> offsetForTimesResult = new HashMap<>(); + offsetForTimesResult.put(TEST_TOPIC_PARTITION, null); + + // Mock the consumer interactions. + Mockito.when(consumer.offsetsForTimes(Mockito.anyMap())).thenReturn(offsetForTimesResult); + + kafkaStartpointRegistrationHandler.visit(TEST_SYSTEM_STREAM_PARTITION, startpointTimestamp); + + // Mock verifications. + Mockito.verify(consumer).seekToEnd(ImmutableList.of(TEST_TOPIC_PARTITION)); + Mockito.verify(consumer).offsetsForTimes(Mockito.anyMap()); + Mockito.verify(kafkaConsumerProxy).addTopicPartition(Mockito.any(SystemStreamPartition.class), Mockito.anyLong()); + } + + @Test + public void testStartpointOldestVisitorShouldUpdateTheFetchOffsetInConsumer() { + // Define dummy variables for testing. + final KafkaConsumer consumer = Mockito.mock(KafkaConsumer.class); + final KafkaConsumerProxy kafkaConsumerProxy = Mockito.mock(KafkaConsumerProxy.class); + KafkaSystemConsumer kafkaSystemConsumer = new KafkaSystemConsumer(consumer, TEST_SYSTEM, new MapConfig(), TEST_CLIENT_ID, kafkaConsumerProxy, Mockito.mock(KafkaSystemConsumerMetrics.class), new TestClock(), REGISTRATION_HANDLER); + + KafkaStartpointRegistrationHandler kafkaStartpointRegistrationHandler = kafkaSystemConsumer.new KafkaStartpointRegistrationHandler(); + + final StartpointOldest testStartpointSpecific = new StartpointOldest(); + + // Mock the consumer interactions. + Mockito.doNothing().when(consumer).seekToBeginning(ImmutableList.of(TEST_TOPIC_PARTITION)); + + // Invoke the consumer with startpoint. + kafkaStartpointRegistrationHandler.visit(TEST_SYSTEM_STREAM_PARTITION, testStartpointSpecific); + + // Mock verifications. + Mockito.verify(consumer).seekToBeginning(ImmutableList.of(TEST_TOPIC_PARTITION)); + Mockito.verify(kafkaConsumerProxy).addTopicPartition(Mockito.any(SystemStreamPartition.class), Mockito.anyLong()); + } + + @Test + public void testStartpointUpcomingVisitorShouldUpdateTheFetchOffsetInConsumer() { + // Define dummy variables for testing. + final KafkaConsumer consumer = Mockito.mock(KafkaConsumer.class); + final KafkaConsumerProxy kafkaConsumerProxy = Mockito.mock(KafkaConsumerProxy.class); + KafkaSystemConsumer kafkaSystemConsumer = new KafkaSystemConsumer(consumer, TEST_SYSTEM, new MapConfig(), TEST_CLIENT_ID, kafkaConsumerProxy, Mockito.mock(KafkaSystemConsumerMetrics.class), new TestClock(), REGISTRATION_HANDLER); + + KafkaStartpointRegistrationHandler kafkaStartpointRegistrationHandler = kafkaSystemConsumer.new KafkaStartpointRegistrationHandler(); + + final StartpointUpcoming testStartpointSpecific = new StartpointUpcoming(); + + // Mock the consumer interactions. + Mockito.doNothing().when(consumer).seekToEnd(ImmutableList.of(TEST_TOPIC_PARTITION)); + + // Invoke the consumer with startpoint. + kafkaStartpointRegistrationHandler.visit(TEST_SYSTEM_STREAM_PARTITION, testStartpointSpecific); + + // Mock verifications. + Mockito.verify(consumer).seekToEnd(ImmutableList.of(TEST_TOPIC_PARTITION)); + Mockito.verify(kafkaConsumerProxy).addTopicPartition(Mockito.any(SystemStreamPartition.class), Mockito.anyLong()); + } + + @Test + public void testStartInvocationAfterStartPointsRegistrationShouldInvokeTheStartPointApplyMethod() { + // Initialize the constants required for the test. + Consumer mockConsumer = Mockito.mock(Consumer.class); + KafkaSystemConsumerMetrics kafkaSystemConsumerMetrics = new KafkaSystemConsumerMetrics(TEST_SYSTEM, new NoOpMetricsRegistry()); + + // Test system stream partitions. + SystemStreamPartition testSystemStreamPartition1 = new SystemStreamPartition(TEST_SYSTEM, TEST_STREAM, new Partition(0)); + SystemStreamPartition testSystemStreamPartition2 = new SystemStreamPartition(TEST_SYSTEM, TEST_STREAM, new Partition(1)); + SystemStreamPartition testSystemStreamPartition3 = new SystemStreamPartition(TEST_SYSTEM, TEST_STREAM, new Partition(2)); + SystemStreamPartition testSystemStreamPartition4 = new SystemStreamPartition(TEST_SYSTEM, TEST_STREAM, new Partition(3)); + + // Different kinds of {@code Startpoint}. + StartpointSpecific startPointSpecific = new StartpointSpecific("100"); + StartpointTimestamp startpointTimestamp = new StartpointTimestamp(100L); + StartpointOldest startpointOldest = new StartpointOldest(); + StartpointUpcoming startpointUpcoming = new StartpointUpcoming(); + + // Mock the visit methods of KafkaStartpointRegistrationHandler. Review comment: At the end of the test, it is verified that these API methods are invoked with appropriate parameters. They're not mocked for nothing. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services