kumarpritam863 commented on code in PR #14630:
URL: https://github.com/apache/kafka/pull/14630#discussion_r1380523741


##########
connect/runtime/src/test/java/org/apache/kafka/connect/runtime/WorkerSinkTaskTest.java:
##########
@@ -1999,6 +2004,81 @@ public void 
testErrorReporterConfigurationExceptionPropagation() {
         PowerMock.verifyAll();
     }
 
+    @Test
+    public void testPartitionCountInCaseOfPartitionRevocation() {
+        mockConsumer = new MockConsumer<>(OffsetResetStrategy.EARLIEST);
+        // Setting up Worker Sink Task to check metrics
+        workerTask = new WorkerSinkTask(
+                taskId, sinkTask, statusListener, TargetState.PAUSED, 
workerConfig, ClusterConfigState.EMPTY, metrics,
+                keyConverter, valueConverter, errorHandlingMetrics, 
headerConverter,
+                transformationChain, mockConsumer, pluginLoader, time,
+                RetryWithToleranceOperatorTest.NOOP_OPERATOR, null, 
statusBackingStore, Collections::emptyList);
+        // Subscribing to Topic = "test" with "MockHandleRebalance"
+        mockConsumer.subscribe(asList(TOPIC), new MockHandleRebalancePass());
+        // Initial Rebalance to assign INITIAL_ASSIGNMENT which is 
"TOPIC_PARTITION" and "TOPIC_PARTITION2"
+        ((MockConsumer<byte[], byte[]>) 
mockConsumer).rebalance(INITIAL_ASSIGNMENT);
+        assertSinkMetricValue("partition-count", 2);
+        // Revoked "TOPIC_PARTITION" and second rebalnce with 
"TOPIC_PARTITION2"
+        ((MockConsumer<byte[], byte[]>) 
mockConsumer).rebalance(Collections.singleton(TOPIC_PARTITION2));
+        assertSinkMetricValue("partition-count", 1);
+    }
+
+    @Test
+    public void testPartitionCountInCaseOfPartitionRevocationFail() {
+        mockConsumer = new MockConsumer<>(OffsetResetStrategy.EARLIEST);
+        // Setting up Worker Sink Task to check metrics
+        workerTask = new WorkerSinkTask(
+                taskId, sinkTask, statusListener, TargetState.PAUSED, 
workerConfig, ClusterConfigState.EMPTY, metrics,
+                keyConverter, valueConverter, errorHandlingMetrics, 
headerConverter,
+                transformationChain, mockConsumer, pluginLoader, time,
+                RetryWithToleranceOperatorTest.NOOP_OPERATOR, null, 
statusBackingStore, Collections::emptyList);
+        // Subscribing to Topic = "test" with "MockHandleRebalance"
+        mockConsumer.subscribe(asList(TOPIC), new MockHandleRebalanceFail());
+        // Initial Rebalance to assign INITIAL_ASSIGNMENT which is 
"TOPIC_PARTITION" and "TOPIC_PARTITION2"
+        ((MockConsumer<byte[], byte[]>) 
mockConsumer).rebalance(INITIAL_ASSIGNMENT);
+        assertSinkMetricValue("partition-count", 2);
+        // Revoked "TOPIC_PARTITION" and second rebalnce with 
"TOPIC_PARTITION2"
+        ((MockConsumer<byte[], byte[]>) 
mockConsumer).rebalance(Collections.singleton(TOPIC_PARTITION2));
+        Assertions.assertThrows(AssertionError.class,
+                () -> assertSinkMetricValue("partition-count", 1));
+    }
+
+    /*
+    Correct Order to Call the updatePartitionCount
+     */
+    private class MockHandleRebalancePass implements ConsumerRebalanceListener 
{
+        @Override
+        public void onPartitionsAssigned(Collection<TopicPartition> 
partitions) {
+            
workerTask.sinkTaskMetricsGroup().recordPartitionCount(mockConsumer.assignment().size());
+            if (partitions.isEmpty()) {
+                return;
+            }
+            // Not doing anything as the objective is to test only Partition 
Count
+        }
+        @Override
+        public void onPartitionsRevoked(Collection<TopicPartition> partitions) 
{
+            // Not doing anything as we do not updatePartitionCount here.
+        }
+    }
+    /*
+    Existing flow which calls updatePartitionCount before updation of 
Subscription.
+     */
+    private class MockHandleRebalanceFail implements ConsumerRebalanceListener 
{
+        @Override
+        public void onPartitionsAssigned(Collection<TopicPartition> 
partitions) {
+            if (partitions.isEmpty()) {
+                return;
+            }
+            // Not doing anything as the objective is to test only Partition 
Count
+            
workerTask.sinkTaskMetricsGroup().recordPartitionCount(mockConsumer.assignment().size());
+        }
+        @Override
+        public void onPartitionsRevoked(Collection<TopicPartition> partitions) 
{
+            // Not doing anything as we do not updatePartitionCount here.
+            
workerTask.sinkTaskMetricsGroup().recordPartitionCount(mockConsumer.assignment().size());
+        }
+    }

Review Comment:
   Yes, @C0urante , we do not need. Actually missed this. I have added this 
change.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to