locchipinti commented on code in PR #4357: URL: https://github.com/apache/flink-cdc/pull/4357#discussion_r3037342939
########## flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mysql-cdc/src/test/java/org/apache/flink/cdc/connectors/mysql/debezium/task/MySqlSnapshotSplitReadTaskConnectionTest.java: ########## @@ -0,0 +1,210 @@ +/* + * 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.flink.cdc.connectors.mysql.debezium.task; + +import org.apache.flink.cdc.connectors.mysql.debezium.DebeziumUtils; +import org.apache.flink.cdc.connectors.mysql.debezium.task.context.StatefulTaskContext; +import org.apache.flink.cdc.connectors.mysql.source.config.MySqlSourceConfig; +import org.apache.flink.cdc.connectors.mysql.source.config.MySqlSourceConfigFactory; +import org.apache.flink.cdc.connectors.mysql.table.StartupOptions; + +import com.github.shyiko.mysql.binlog.BinaryLogClient; +import io.debezium.connector.mysql.MySqlConnection; +import org.junit.jupiter.api.Test; + +import java.time.Duration; +import java.time.ZoneId; +import java.util.Properties; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +/** + * Tests for {@link MySqlSnapshotSplitReadTask} to verify correct usage of writer and reader + * connections. + * + * <p>The key behavior being tested: + * + * <ul> + * <li>Writer connection is used for binlog position retrieval (SHOW MASTER STATUS) + * <li>Reader connection is used for snapshot data queries (SELECT statements) + * <li>When reader hostname is not configured, reader connection falls back to writer + * </ul> + */ +class MySqlSnapshotSplitReadTaskConnectionTest { + + private static final String PRIMARY_HOSTNAME = "writer-host"; + private static final String SNAPSHOT_HOSTNAME = "reader-host"; + private static final int PORT = 3306; + + @Test + void testStatefulTaskContextWithSeparateConnections() { + MySqlSourceConfig config = createConfig(PRIMARY_HOSTNAME, SNAPSHOT_HOSTNAME); + BinaryLogClient binaryLogClient = mock(BinaryLogClient.class); + + // Create separate writer and reader connections + MySqlConnection writerConnection = DebeziumUtils.createMySqlConnection(config); + MySqlConnection readerConnection = DebeziumUtils.createSnapshotMySqlConnection(config); + + // Create StatefulTaskContext with both connections + StatefulTaskContext context = + new StatefulTaskContext(config, binaryLogClient, writerConnection, readerConnection); + + // Verify the context returns the correct connections + assertThat(context.getConnection()).isSameAs(writerConnection); + assertThat(context.getSnapshotConnection()).isSameAs(readerConnection); + + // Verify writer and reader connections point to different hosts + assertThat(writerConnection.connectionString()).contains(PRIMARY_HOSTNAME); + assertThat(readerConnection.connectionString()).contains(SNAPSHOT_HOSTNAME); + } + + @Test + void testStatefulTaskContextWithSameConnectionWhenNoReaderConfigured() { + MySqlSourceConfig config = createConfig(PRIMARY_HOSTNAME, null); + BinaryLogClient binaryLogClient = mock(BinaryLogClient.class); + + // When no reader hostname is configured, createReaderMySqlConnection falls back to writer + MySqlConnection writerConnection = DebeziumUtils.createMySqlConnection(config); + MySqlConnection readerConnection = DebeziumUtils.createSnapshotMySqlConnection(config); + + // Create StatefulTaskContext + StatefulTaskContext context = + new StatefulTaskContext(config, binaryLogClient, writerConnection, readerConnection); + + // Verify both connections point to the same host (writer) + assertThat(context.getConnection().connectionString()).contains(PRIMARY_HOSTNAME); + assertThat(context.getSnapshotConnection().connectionString()).contains(PRIMARY_HOSTNAME); + } + + @Test + void testStatefulTaskContextLegacyConstructorUsesSameConnection() { + MySqlSourceConfig config = createConfig(PRIMARY_HOSTNAME, SNAPSHOT_HOSTNAME); + BinaryLogClient binaryLogClient = mock(BinaryLogClient.class); + + // Use the legacy constructor that takes only one connection + MySqlConnection connection = DebeziumUtils.createMySqlConnection(config); + StatefulTaskContext context = + new StatefulTaskContext(config, binaryLogClient, connection); + + // Both getConnection() and getReaderConnection() should return the same instance Review Comment: Fixed. Updated the comment to reference getSnapshotConnection() which is the actual method 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
