locchipinti commented on code in PR #4357: URL: https://github.com/apache/flink-cdc/pull/4357#discussion_r3037342686
########## flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mysql-cdc/src/test/java/org/apache/flink/cdc/connectors/mysql/source/connection/ReaderConnectionTest.java: ########## @@ -0,0 +1,162 @@ +/* + * 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.source.connection; + +import org.apache.flink.cdc.connectors.mysql.debezium.DebeziumUtils; +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 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; + +/** + * Tests for reader hostname connection handling. Verifies that the reader connection is used when + * configured, and falls back to the writer connection when not configured. + */ +class ReaderConnectionTest { + + private static final String WRITER_HOSTNAME = "writer-host"; + private static final String READER_HOSTNAME = "reader-host"; + private static final int PORT = 3306; + + @Test + void testReaderHostnameConfigured() { + MySqlSourceConfig config = createConfig(WRITER_HOSTNAME, READER_HOSTNAME); + + assertThat(config.getHostname()).isEqualTo(WRITER_HOSTNAME); + assertThat(config.getSnapshotHostname()).isEqualTo(READER_HOSTNAME); + } + + @Test + void testReaderHostnameNotConfigured() { + MySqlSourceConfig config = createConfig(WRITER_HOSTNAME, null); + + assertThat(config.getHostname()).isEqualTo(WRITER_HOSTNAME); + assertThat(config.getSnapshotHostname()).isNull(); + } + + @Test + void testCreateReaderMySqlConnectionWithReaderHostname() { + MySqlSourceConfig config = createConfig(WRITER_HOSTNAME, READER_HOSTNAME); + + // Create reader connection - should use reader hostname + MySqlConnection readerConnection = DebeziumUtils.createSnapshotMySqlConnection(config); + String readerConnectionString = readerConnection.connectionString(); + + // Create writer connection - should use writer hostname + MySqlConnection writerConnection = DebeziumUtils.createMySqlConnection(config); + String writerConnectionString = writerConnection.connectionString(); + + // Reader connection should contain reader hostname + assertThat(readerConnectionString).contains(READER_HOSTNAME); + assertThat(readerConnectionString).doesNotContain(WRITER_HOSTNAME); + + // Writer connection should contain writer hostname + assertThat(writerConnectionString).contains(WRITER_HOSTNAME); + assertThat(writerConnectionString).doesNotContain(READER_HOSTNAME); + } + + @Test + void testCreateReaderMySqlConnectionFallsBackToWriterWhenNoReaderConfigured() { + MySqlSourceConfig config = createConfig(WRITER_HOSTNAME, null); + + // Create reader connection - should fall back to writer hostname + MySqlConnection readerConnection = DebeziumUtils.createSnapshotMySqlConnection(config); + String readerConnectionString = readerConnection.connectionString(); + + // Create writer connection + MySqlConnection writerConnection = DebeziumUtils.createMySqlConnection(config); + String writerConnectionString = writerConnection.connectionString(); + + // Both should use the writer hostname when reader is not configured + assertThat(readerConnectionString).contains(WRITER_HOSTNAME); + assertThat(writerConnectionString).contains(WRITER_HOSTNAME); + } + + @Test + void testJdbcConnectionFactoryUsesHostnameOverride() { + MySqlSourceConfig config = createConfig(WRITER_HOSTNAME, null); + + // Factory without hostname override - should use config hostname + JdbcConnectionFactory factoryWithoutOverride = new JdbcConnectionFactory(config); + + // Factory with hostname override - should use override + JdbcConnectionFactory factoryWithOverride = + new JdbcConnectionFactory(config, READER_HOSTNAME); + + // Verify the factories are created with correct configuration + assertThat(factoryWithoutOverride).isNotNull(); + assertThat(factoryWithOverride).isNotNull(); + } Review Comment: Fixed. Added a package-private getHostnameOverride() getter to JdbcConnectionFactory and updated the test to assert that the override hostname is correctly stored (isEqualTo(READER_HOSTNAME)) and that it differs from the primary config hostname. The test will now fail if override routing regresses. -- 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]
