Copilot commented on code in PR #4390: URL: https://github.com/apache/flink-cdc/pull/4390#discussion_r3207639969
########## flink-cdc-connect/flink-cdc-source-connectors/flink-cdc-base/src/test/java/org/apache/flink/cdc/connectors/base/source/reader/external/IncrementalSourceScanFetcherTest.java: ########## @@ -0,0 +1,117 @@ +/* + * 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.base.source.reader.external; + +import org.apache.flink.cdc.connectors.base.source.meta.split.SnapshotSplit; +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.types.logical.RowType; + +import io.debezium.relational.TableId; +import org.apache.kafka.connect.source.SourceRecord; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Collections; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** Tests for {@link IncrementalSourceScanFetcher}. */ +class IncrementalSourceScanFetcherTest { + + private static final TableId CURRENT_SPLIT_TABLE = TableId.parse("test_db.table_a"); + private static final TableId OTHER_TABLE = TableId.parse("test_db.table_b"); + + /** + * Reproduces the NPE seen in PostgreSQL backfill when the WAL stream carries change records for + * a captured table other than the one currently being snapshotted. Before the fix, the fetcher + * passed the foreign-table record to {@code isRecordBetween}, which dereferenced a null + * Debezium {@code Table} from the schema cache and threw NPE. After the fix, the foreign-table + * record is filtered out by tableId and {@code isRecordBetween} is never invoked. + */ + @Test + void testIsChangeRecordInChunkRangeFiltersOutForeignTableRecord() { + FetchTask.Context taskContext = mock(FetchTask.Context.class); + SourceRecord foreignTableRecord = mock(SourceRecord.class); + + when(taskContext.isDataChangeRecord(foreignTableRecord)).thenReturn(true); + when(taskContext.getTableId(foreignTableRecord)).thenReturn(OTHER_TABLE); + // Mirrors the production failure mode: the record is for a table whose schema is not in + // the cache, so any downstream lookup explodes with NPE. The test asserts we never get + // here. + when(taskContext.isRecordBetween(any(), any(), any())) + .thenThrow( + new NullPointerException( + "Cannot invoke \"io.debezium.relational.Table.primaryKeyColumns()\" because \"table\" is null")); + + IncrementalSourceScanFetcher fetcher = new IncrementalSourceScanFetcher(taskContext, 0); + fetcher.setCurrentSnapshotSplit(newSnapshotSplit(CURRENT_SPLIT_TABLE)); + + Assertions.assertThatCode(() -> fetcher.isChangeRecordInChunkRange(foreignTableRecord)) + .doesNotThrowAnyException(); + Assertions.assertThat(fetcher.isChangeRecordInChunkRange(foreignTableRecord)).isFalse(); Review Comment: The test invokes fetcher.isChangeRecordInChunkRange(foreignTableRecord) twice (once in assertThatCode and once in assertThat). This duplicates interactions with the mocked taskContext and makes the test less strict if the method later gains side effects; consider calling it once and asserting both “does not throw” and the boolean result from the same invocation. -- 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]
