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_r257523115
########## File path: samza-kafka/src/test/java/org/apache/samza/system/kafka/TestKafkaSystemConsumer.java ########## @@ -205,6 +215,156 @@ public void testFetchThresholdBytesDiabled() { consumer.stop(); } + @Test + public void testStartpointSpecificOffsetVisitorShouldUpdateTheFetchOffsetInConsumer() { + // Define dummy variables for testing. + final Integer testPartitionId = 0; + final String offset = "0"; + final TopicPartition testTopicPartition = new TopicPartition(TEST_STREAM, testPartitionId); + final Partition testPartition = new Partition(testPartitionId); + final SystemStreamPartition testSystemStreamPartition = new SystemStreamPartition(TEST_SYSTEM, TEST_STREAM, testPartition); + + final KafkaConsumer consumer = Mockito.mock(KafkaConsumer.class); + KafkaStartpointRegistrationHandler + kafkaStartpointRegistrationHandler = new KafkaSystemConsumer.KafkaStartpointRegistrationHandler(consumer); + + final StartpointSpecific testStartpointSpecific = new StartpointSpecific(offset); + + // Mock the consumer interactions. + Mockito.doNothing().when(consumer).seek(testTopicPartition, Long.valueOf(offset)); + + // Invoke the consumer with startpoint. + kafkaStartpointRegistrationHandler.visit(testSystemStreamPartition, testStartpointSpecific); + + // Mock verifications. + Mockito.verify(consumer).seek(testTopicPartition, Long.valueOf(offset)); + } + + @Test + public void testStartpointTimestampVisitorShouldUpdateTheFetchOffsetInConsumer() { + // Define dummy variables for testing. + final Integer testPartitionId = 0; + final TopicPartition testTopicPartition = new TopicPartition(TEST_STREAM, testPartitionId); + final Partition testPartition = new Partition(testPartitionId); + final SystemStreamPartition testSystemStreamPartition = new SystemStreamPartition(TEST_SYSTEM, TEST_STREAM, testPartition); + final Long testTimeStamp = 10L; + final String testOffset = "10"; + + final KafkaConsumer consumer = Mockito.mock(KafkaConsumer.class); + KafkaStartpointRegistrationHandler + kafkaStartpointRegistrationHandler = new KafkaSystemConsumer.KafkaStartpointRegistrationHandler(consumer); + + final StartpointTimestamp startpointTimestamp = new StartpointTimestamp(testTimeStamp); + final Map<TopicPartition, OffsetAndTimestamp> offsetForTimesResult = ImmutableMap.of(testTopicPartition, new OffsetAndTimestamp(Long.valueOf(testOffset), testTimeStamp)); + + // Mock the consumer interactions. + Mockito.when(consumer.offsetsForTimes(Mockito.anyMap())).thenReturn(offsetForTimesResult); + Mockito.doNothing().when(consumer).seek(testTopicPartition, Long.valueOf(testOffset)); + + kafkaStartpointRegistrationHandler.visit(testSystemStreamPartition, startpointTimestamp); + + // Mock verifications. + Mockito.verify(consumer).seek(testTopicPartition, Long.valueOf(testOffset)); + Mockito.verify(consumer).offsetsForTimes(Mockito.anyMap()); + } + + @Test + public void testStartpointOldestVisitorShouldUpdateTheFetchOffsetInConsumer() { + // Define dummy variables for testing. + final Integer testPartitionId = 0; + final TopicPartition testTopicPartition = new TopicPartition(TEST_STREAM, testPartitionId); + final Partition testPartition = new Partition(testPartitionId); + final SystemStreamPartition testSystemStreamPartition = new SystemStreamPartition(TEST_SYSTEM, TEST_STREAM, testPartition); + + final KafkaConsumer consumer = Mockito.mock(KafkaConsumer.class); + final KafkaStartpointRegistrationHandler + kafkaStartpointRegistrationHandler = new KafkaSystemConsumer.KafkaStartpointRegistrationHandler(consumer); + + final StartpointOldest testStartpointSpecific = new StartpointOldest(); + + // Mock the consumer interactions. + Mockito.doNothing().when(consumer).seekToBeginning(ImmutableList.of(testTopicPartition)); + + // Invoke the consumer with startpoint. + kafkaStartpointRegistrationHandler.visit(testSystemStreamPartition, testStartpointSpecific); + + // Mock verifications. + Mockito.verify(consumer).seekToBeginning(ImmutableList.of(testTopicPartition)); + } + + @Test + public void testStartpointUpcomingVisitorShouldUpdateTheFetchOffsetInConsumer() { + // Define dummy variables for testing. + final Integer testPartitionId = 0; + final TopicPartition testTopicPartition = new TopicPartition(TEST_STREAM, testPartitionId); + final Partition testPartition = new Partition(testPartitionId); + final SystemStreamPartition testSystemStreamPartition = new SystemStreamPartition(TEST_SYSTEM, TEST_STREAM, testPartition); + + final KafkaConsumer consumer = Mockito.mock(KafkaConsumer.class); + final KafkaSystemConsumer.KafkaStartpointRegistrationHandler + kafkaStartpointRegistrationHandler = new KafkaSystemConsumer.KafkaStartpointRegistrationHandler(consumer); + + final StartpointUpcoming testStartpointSpecific = new StartpointUpcoming(); + + // Mock the consumer interactions. + Mockito.doNothing().when(consumer).seekToEnd(ImmutableList.of(testTopicPartition)); + + // Invoke the consumer with startpoint. + kafkaStartpointRegistrationHandler.visit(testSystemStreamPartition, testStartpointSpecific); + + // Mock verifications. + Mockito.verify(consumer).seekToEnd(ImmutableList.of(testTopicPartition)); + } + + @Test + public void testStartInvocationAfterStartPointsRegistrationShouldInvokeTheStartPointApplyMethod() { + // Initialize the constants required for the test. + Consumer mockConsumer = Mockito.mock(Consumer.class); + String testSystemName = "testSystem"; Review comment: Switched to the existing variables. Done. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on 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