Copilot commented on code in PR #58: URL: https://github.com/apache/pulsar-connectors/pull/58#discussion_r3554196566
########## debezium/mongodb/src/test/java/org/apache/pulsar/io/debezium/mongodb/DebeziumMongoDbSourceTest.java: ########## @@ -0,0 +1,142 @@ +/* + * 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.pulsar.io.debezium.mongodb; + +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; +import com.mongodb.client.MongoCollection; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import lombok.extern.slf4j.Slf4j; +import org.apache.pulsar.client.api.PulsarClient; +import org.apache.pulsar.common.schema.KeyValue; +import org.apache.pulsar.functions.api.Record; +import org.apache.pulsar.io.core.SourceContext; +import org.awaitility.Awaitility; +import org.bson.Document; +import org.testcontainers.containers.MongoDBContainer; +import org.testcontainers.containers.PulsarContainer; +import org.testcontainers.utility.DockerImageName; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +@Slf4j +public class DebeziumMongoDbSourceTest { + + private static final String PULSAR_IMAGE = + System.getenv().getOrDefault("PULSAR_TEST_IMAGE", "apachepulsar/pulsar:4.1.3"); + + private MongoDBContainer mongoContainer; + private PulsarContainer pulsarContainer; + private PulsarClient pulsarClient; + private DebeziumMongoDbSource source; + + @BeforeMethod + public void setup() throws Exception { + // MongoDBContainer starts a single-node replica set, which Debezium requires + // for change streams. + mongoContainer = new MongoDBContainer(DockerImageName.parse("mongo:7.0")); + mongoContainer.start(); + + pulsarContainer = new PulsarContainer(DockerImageName.parse(PULSAR_IMAGE)); + pulsarContainer.start(); + + pulsarClient = PulsarClient.builder() + .serviceUrl(pulsarContainer.getPulsarBrokerUrl()) + .build(); + + // Create test collection and insert initial data + try (MongoClient client = MongoClients.create(mongoContainer.getConnectionString())) { + MongoCollection<Document> products = client.getDatabase("testdb").getCollection("products"); + products.insertOne(new Document("name", "widget").append("description", "A small widget")); + products.insertOne(new Document("name", "gadget").append("description", "A fancy gadget")); + } + + source = new DebeziumMongoDbSource(); + } + + @AfterMethod(alwaysRun = true) + public void cleanup() throws Exception { + if (source != null) { + try { + source.close(); + } catch (Exception e) { + log.warn("Failed to close source", e); + } + } + if (pulsarClient != null) { + pulsarClient.close(); + } + if (pulsarContainer != null) { + pulsarContainer.stop(); + } + if (mongoContainer != null) { + mongoContainer.stop(); + } + } + + @Test + public void testMongoDbCdcEvents() throws Exception { + String pulsarServiceUrl = pulsarContainer.getPulsarBrokerUrl(); + + SourceContext sourceContext = mock(SourceContext.class); + when(sourceContext.getPulsarClient()).thenReturn(pulsarClient); + when(sourceContext.getPulsarClientBuilder()).thenReturn( + PulsarClient.builder().serviceUrl(pulsarServiceUrl)); + when(sourceContext.getTenant()).thenReturn("public"); + when(sourceContext.getNamespace()).thenReturn("default"); + when(sourceContext.getSourceName()).thenReturn("debezium-mongodb-test"); + when(sourceContext.getSecret(anyString())).thenReturn(null); + + Map<String, Object> config = new HashMap<>(); + config.put("mongodb.connection.string", mongoContainer.getConnectionString()); + config.put("topic.prefix", "mongodb1"); + config.put("collection.include.list", "testdb.products"); + + source.open(config, sourceContext); + + // Debezium performs an initial snapshot of existing data. + // We should receive CDC records for the 2 documents we inserted. + Awaitility.await() + .atMost(60, TimeUnit.SECONDS) + .pollInterval(500, TimeUnit.MILLISECONDS) + .untilAsserted(() -> { + int recordCount = 0; + Record<KeyValue<byte[], byte[]>> record; + while ((record = source.read()) != null) { + assertNotNull(record.getValue()); + log.info("Received CDC record: key={}", record.getKey().orElse(null)); + recordCount++; + record.ack(); + if (recordCount >= 2) { + break; + } + } + assertTrue(recordCount >= 2, + "Expected at least 2 CDC records from initial snapshot, got " + recordCount); + }); Review Comment: The Awaitility block can hang indefinitely: `source.read()` never returns `null` and can block until Debezium produces a record (see `AbstractKafkaConnectSource.read()` looping until `sourceTask.poll()` returns a non-empty batch). Because `read()` is called inside `untilAsserted`, the 60s `atMost` timeout may never be reached if Debezium fails to start or no records are produced. Run the blocking reads on a daemon thread and have Awaitility wait on a counter so the test reliably times out and doesn't stall CI. -- 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]
