You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2021/03/05 12:13:14 UTC

[GitHub] [kafka] mimaison commented on a change in pull request #10192: MINOR: Add missing unit tests for Mirror Connect

mimaison commented on a change in pull request #10192:
URL: https://github.com/apache/kafka/pull/10192#discussion_r588238922



##########
File path: connect/mirror/src/test/java/org/apache/kafka/connect/mirror/MirrorSourceTaskTest.java
##########
@@ -86,4 +96,66 @@ public void testZeroOffsetSync() {
         assertTrue(partitionState.update(4, 3));
         assertTrue(partitionState.update(5, 4));
     }
+
+    @Test
+    public void testPoll() {
+        // Create a consumer mock
+        byte[] key1 = "abc".getBytes();
+        byte[] value1 = "fgh".getBytes();
+        byte[] key2 = "123".getBytes();
+        byte[] value2 = "456".getBytes();
+        List<ConsumerRecord<byte[], byte[]>> consumerRecordsList =  new ArrayList<>();
+        String topicName = "test";
+        String headerKey = "key";
+        RecordHeaders headers = new RecordHeaders(new Header[] {
+            new RecordHeader(headerKey, "value".getBytes()),
+        });
+        consumerRecordsList.add(new ConsumerRecord<>(topicName, 0, 0, System.currentTimeMillis(),
+                TimestampType.CREATE_TIME, 0L, key1.length, value1.length, key1, value1, headers));
+        consumerRecordsList.add(new ConsumerRecord<>(topicName, 1, 1, System.currentTimeMillis(),
+                TimestampType.CREATE_TIME, 0L, key2.length, value2.length, key2, value2, headers));
+        ConsumerRecords<byte[], byte[]> consumerRecords =
+                new ConsumerRecords<>(Collections.singletonMap(new TopicPartition(topicName, 0), consumerRecordsList));
+
+        @SuppressWarnings("unchecked")
+        KafkaConsumer<byte[], byte[]> consumer = mock(KafkaConsumer.class);
+        when(consumer.poll(any())).thenReturn(consumerRecords);
+
+        MirrorMetrics metrics = mock(MirrorMetrics.class);
+
+        String sourceClusterName = "cluster1";
+        ReplicationPolicy replicationPolicy = new DefaultReplicationPolicy();
+        MirrorSourceTask mirrorSourceTask = new MirrorSourceTask(consumer, metrics, sourceClusterName,
+                replicationPolicy, 50);
+        List<SourceRecord> sourceRecords = mirrorSourceTask.poll();
+
+        assertEquals(2, sourceRecords.size());
+        for (int i = 0; i < sourceRecords.size(); i++) {
+            SourceRecord sourceRecord = sourceRecords.get(i);
+            ConsumerRecord consumerRecord = consumerRecordsList.get(i);

Review comment:
       `ConsumerRecord<byte[], byte[]> consumerRecord`

##########
File path: connect/mirror/src/test/java/org/apache/kafka/connect/mirror/MirrorSourceTaskTest.java
##########
@@ -86,4 +96,66 @@ public void testZeroOffsetSync() {
         assertTrue(partitionState.update(4, 3));
         assertTrue(partitionState.update(5, 4));
     }
+
+    @Test
+    public void testPoll() {
+        // Create a consumer mock
+        byte[] key1 = "abc".getBytes();
+        byte[] value1 = "fgh".getBytes();
+        byte[] key2 = "123".getBytes();
+        byte[] value2 = "456".getBytes();
+        List<ConsumerRecord<byte[], byte[]>> consumerRecordsList =  new ArrayList<>();
+        String topicName = "test";
+        String headerKey = "key";
+        RecordHeaders headers = new RecordHeaders(new Header[] {
+            new RecordHeader(headerKey, "value".getBytes()),
+        });
+        consumerRecordsList.add(new ConsumerRecord<>(topicName, 0, 0, System.currentTimeMillis(),
+                TimestampType.CREATE_TIME, 0L, key1.length, value1.length, key1, value1, headers));
+        consumerRecordsList.add(new ConsumerRecord<>(topicName, 1, 1, System.currentTimeMillis(),
+                TimestampType.CREATE_TIME, 0L, key2.length, value2.length, key2, value2, headers));
+        ConsumerRecords<byte[], byte[]> consumerRecords =
+                new ConsumerRecords<>(Collections.singletonMap(new TopicPartition(topicName, 0), consumerRecordsList));
+
+        @SuppressWarnings("unchecked")
+        KafkaConsumer<byte[], byte[]> consumer = mock(KafkaConsumer.class);
+        when(consumer.poll(any())).thenReturn(consumerRecords);
+
+        MirrorMetrics metrics = mock(MirrorMetrics.class);
+
+        String sourceClusterName = "cluster1";
+        ReplicationPolicy replicationPolicy = new DefaultReplicationPolicy();
+        MirrorSourceTask mirrorSourceTask = new MirrorSourceTask(consumer, metrics, sourceClusterName,
+                replicationPolicy, 50);
+        List<SourceRecord> sourceRecords = mirrorSourceTask.poll();
+
+        assertEquals(2, sourceRecords.size());
+        for (int i = 0; i < sourceRecords.size(); i++) {
+            SourceRecord sourceRecord = sourceRecords.get(i);
+            ConsumerRecord consumerRecord = consumerRecordsList.get(i);
+            assertEquals(consumerRecord.key(), sourceRecord.key());
+            assertEquals(consumerRecord.value(), sourceRecord.value());
+            // We expect that the topicname will be based on the replication policy currently used
+            assertEquals(replicationPolicy.formatRemoteTopic(sourceClusterName, topicName),
+                    sourceRecord.topic());
+            // We expect that MirrorMaker will keep the same partition assignment
+            assertEquals(consumerRecord.partition(), sourceRecord.kafkaPartition().intValue());
+            // Check header values
+            List<Header> headerList1 = new ArrayList<>();

Review comment:
       Maybe `expectedHeaders` is a better name?

##########
File path: connect/mirror/src/test/java/org/apache/kafka/connect/mirror/MirrorHeartbeatTaskTest.java
##########
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.connect.mirror;
+
+import org.apache.kafka.connect.source.SourceRecord;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class MirrorHeartbeatTaskTest {
+
+    @Test
+    public void testPollCreatesRecords() throws InterruptedException {
+        MirrorHeartbeatTask heartbeatTask = new MirrorHeartbeatTask();
+        heartbeatTask.start(TestUtils.makeProps("source.cluster.alias", "testSource",
+                "target.cluster.alias", "testTarget"));
+        List<SourceRecord> records = heartbeatTask.poll();
+        assertEquals(1, records.size());
+        Map sourcePartition = records.iterator().next().sourcePartition();

Review comment:
       Can we add types for `sourcePartition` -> `Map<String, ?>`

##########
File path: connect/mirror/src/test/java/org/apache/kafka/connect/mirror/MirrorSourceTaskTest.java
##########
@@ -86,4 +96,66 @@ public void testZeroOffsetSync() {
         assertTrue(partitionState.update(4, 3));
         assertTrue(partitionState.update(5, 4));
     }
+
+    @Test
+    public void testPoll() {
+        // Create a consumer mock
+        byte[] key1 = "abc".getBytes();
+        byte[] value1 = "fgh".getBytes();
+        byte[] key2 = "123".getBytes();
+        byte[] value2 = "456".getBytes();
+        List<ConsumerRecord<byte[], byte[]>> consumerRecordsList =  new ArrayList<>();
+        String topicName = "test";
+        String headerKey = "key";
+        RecordHeaders headers = new RecordHeaders(new Header[] {
+            new RecordHeader(headerKey, "value".getBytes()),
+        });
+        consumerRecordsList.add(new ConsumerRecord<>(topicName, 0, 0, System.currentTimeMillis(),
+                TimestampType.CREATE_TIME, 0L, key1.length, value1.length, key1, value1, headers));
+        consumerRecordsList.add(new ConsumerRecord<>(topicName, 1, 1, System.currentTimeMillis(),
+                TimestampType.CREATE_TIME, 0L, key2.length, value2.length, key2, value2, headers));
+        ConsumerRecords<byte[], byte[]> consumerRecords =
+                new ConsumerRecords<>(Collections.singletonMap(new TopicPartition(topicName, 0), consumerRecordsList));
+
+        @SuppressWarnings("unchecked")
+        KafkaConsumer<byte[], byte[]> consumer = mock(KafkaConsumer.class);
+        when(consumer.poll(any())).thenReturn(consumerRecords);
+
+        MirrorMetrics metrics = mock(MirrorMetrics.class);
+
+        String sourceClusterName = "cluster1";
+        ReplicationPolicy replicationPolicy = new DefaultReplicationPolicy();
+        MirrorSourceTask mirrorSourceTask = new MirrorSourceTask(consumer, metrics, sourceClusterName,
+                replicationPolicy, 50);
+        List<SourceRecord> sourceRecords = mirrorSourceTask.poll();
+
+        assertEquals(2, sourceRecords.size());
+        for (int i = 0; i < sourceRecords.size(); i++) {
+            SourceRecord sourceRecord = sourceRecords.get(i);
+            ConsumerRecord consumerRecord = consumerRecordsList.get(i);
+            assertEquals(consumerRecord.key(), sourceRecord.key());
+            assertEquals(consumerRecord.value(), sourceRecord.value());
+            // We expect that the topicname will be based on the replication policy currently used
+            assertEquals(replicationPolicy.formatRemoteTopic(sourceClusterName, topicName),
+                    sourceRecord.topic());
+            // We expect that MirrorMaker will keep the same partition assignment
+            assertEquals(consumerRecord.partition(), sourceRecord.kafkaPartition().intValue());
+            // Check header values
+            List<Header> headerList1 = new ArrayList<>();
+            consumerRecord.headers().forEach(headerList1::add);
+            List<org.apache.kafka.connect.header.Header> headerList2 = new ArrayList<>();
+            sourceRecord.headers().forEach(headerList2::add);
+            compareHeaders(headerList1, headerList2);
+        }
+    }
+
+    private void compareHeaders(List<Header> headers1, List<org.apache.kafka.connect.header.Header> headers2) {

Review comment:
       Let's rename `headers1` and `headers2` here too

##########
File path: connect/mirror/src/test/java/org/apache/kafka/connect/mirror/MirrorSourceTaskTest.java
##########
@@ -86,4 +96,66 @@ public void testZeroOffsetSync() {
         assertTrue(partitionState.update(4, 3));
         assertTrue(partitionState.update(5, 4));
     }
+
+    @Test
+    public void testPoll() {
+        // Create a consumer mock
+        byte[] key1 = "abc".getBytes();
+        byte[] value1 = "fgh".getBytes();
+        byte[] key2 = "123".getBytes();
+        byte[] value2 = "456".getBytes();
+        List<ConsumerRecord<byte[], byte[]>> consumerRecordsList =  new ArrayList<>();
+        String topicName = "test";
+        String headerKey = "key";
+        RecordHeaders headers = new RecordHeaders(new Header[] {
+            new RecordHeader(headerKey, "value".getBytes()),
+        });
+        consumerRecordsList.add(new ConsumerRecord<>(topicName, 0, 0, System.currentTimeMillis(),
+                TimestampType.CREATE_TIME, 0L, key1.length, value1.length, key1, value1, headers));
+        consumerRecordsList.add(new ConsumerRecord<>(topicName, 1, 1, System.currentTimeMillis(),
+                TimestampType.CREATE_TIME, 0L, key2.length, value2.length, key2, value2, headers));
+        ConsumerRecords<byte[], byte[]> consumerRecords =
+                new ConsumerRecords<>(Collections.singletonMap(new TopicPartition(topicName, 0), consumerRecordsList));
+
+        @SuppressWarnings("unchecked")
+        KafkaConsumer<byte[], byte[]> consumer = mock(KafkaConsumer.class);
+        when(consumer.poll(any())).thenReturn(consumerRecords);
+
+        MirrorMetrics metrics = mock(MirrorMetrics.class);
+
+        String sourceClusterName = "cluster1";
+        ReplicationPolicy replicationPolicy = new DefaultReplicationPolicy();
+        MirrorSourceTask mirrorSourceTask = new MirrorSourceTask(consumer, metrics, sourceClusterName,
+                replicationPolicy, 50);
+        List<SourceRecord> sourceRecords = mirrorSourceTask.poll();
+
+        assertEquals(2, sourceRecords.size());
+        for (int i = 0; i < sourceRecords.size(); i++) {
+            SourceRecord sourceRecord = sourceRecords.get(i);
+            ConsumerRecord consumerRecord = consumerRecordsList.get(i);
+            assertEquals(consumerRecord.key(), sourceRecord.key());
+            assertEquals(consumerRecord.value(), sourceRecord.value());
+            // We expect that the topicname will be based on the replication policy currently used
+            assertEquals(replicationPolicy.formatRemoteTopic(sourceClusterName, topicName),
+                    sourceRecord.topic());
+            // We expect that MirrorMaker will keep the same partition assignment
+            assertEquals(consumerRecord.partition(), sourceRecord.kafkaPartition().intValue());
+            // Check header values
+            List<Header> headerList1 = new ArrayList<>();
+            consumerRecord.headers().forEach(headerList1::add);
+            List<org.apache.kafka.connect.header.Header> headerList2 = new ArrayList<>();

Review comment:
       Maybe `taskHeaders` is a better name?




----------------------------------------------------------------
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:
users@infra.apache.org