JingsongLi commented on code in PR #1212:
URL: https://github.com/apache/incubator-paimon/pull/1212#discussion_r1204918095


##########
paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/sink/cdc/CdcRecordStoreMultiWriteOperatorTest.java:
##########
@@ -0,0 +1,696 @@
+/*
+ * 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.paimon.flink.sink.cdc;
+
+import org.apache.paimon.catalog.Catalog;
+import org.apache.paimon.catalog.CatalogContext;
+import org.apache.paimon.catalog.CatalogFactory;
+import org.apache.paimon.catalog.Identifier;
+import org.apache.paimon.flink.sink.MultiTableCommittable;
+import org.apache.paimon.flink.sink.MultiTableCommittableTypeInfo;
+import org.apache.paimon.flink.sink.StoreSinkWriteImpl;
+import org.apache.paimon.fs.FileIO;
+import org.apache.paimon.fs.Path;
+import org.apache.paimon.options.CatalogOptions;
+import org.apache.paimon.options.Options;
+import org.apache.paimon.schema.Schema;
+import org.apache.paimon.schema.SchemaChange;
+import org.apache.paimon.schema.SchemaManager;
+import org.apache.paimon.table.FileStoreTable;
+import org.apache.paimon.types.DataType;
+import org.apache.paimon.types.DataTypes;
+import org.apache.paimon.types.RowKind;
+import org.apache.paimon.types.RowType;
+import org.apache.paimon.utils.TraceableFileIO;
+
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.runtime.state.JavaSerializer;
+import org.apache.flink.streaming.util.OneInputStreamOperatorTestHarness;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.time.Duration;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Predicate;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/** Tests for {@link CdcRecordStoreMultiWriteOperator}. */
+public class CdcRecordStoreMultiWriteOperatorTest {
+
+    @TempDir java.nio.file.Path tempDir;
+
+    private String commitUser;
+    private Path warehouse;
+    private String databaseName;
+    private Identifier firstTable;
+    private Catalog catalog;
+    private Identifier secondTable;
+    private Catalog.Loader catalogLoader;
+    private Schema firstTableSchema;
+
+    @BeforeEach
+    public void before() throws Exception {
+        warehouse = new Path(TraceableFileIO.SCHEME + "://" + 
tempDir.toString());
+        commitUser = UUID.randomUUID().toString();
+
+        databaseName = "test_db";
+        firstTable = Identifier.create(databaseName, "test_table1");
+        secondTable = Identifier.create(databaseName, "test_table2");
+
+        catalogLoader = createCatalogLoader();
+        catalog = catalogLoader.load();
+        catalog.createDatabase(databaseName, true);
+        Options conf = new Options();
+        conf.set(CdcRecordStoreWriteOperator.RETRY_SLEEP_TIME, 
Duration.ofMillis(10));
+
+        RowType rowType1 =
+                RowType.of(
+                        new DataType[] {DataTypes.INT(), DataTypes.INT(), 
DataTypes.STRING()},
+                        new String[] {"pt", "k", "v"});
+        RowType rowType2 =
+                RowType.of(
+                        new DataType[] {
+                            DataTypes.INT(),
+                            DataTypes.INT(),
+                            DataTypes.FLOAT(),
+                            DataTypes.VARCHAR(5),
+                            DataTypes.VARBINARY(5)
+                        },
+                        new String[] {"k", "v1", "v2", "v3", "v4"});
+
+        firstTableSchema =
+                new Schema(
+                        rowType1.getFields(),
+                        Collections.singletonList("pt"),
+                        Arrays.asList("pt", "k"),
+                        conf.toMap(),
+                        "");
+        createTestTables(
+                catalog,
+                Tuple2.of(firstTable, firstTableSchema),
+                Tuple2.of(
+                        secondTable,
+                        new Schema(
+                                rowType2.getFields(),
+                                Collections.emptyList(),
+                                Collections.singletonList("k"),
+                                conf.toMap(),
+                                "")));
+    }
+
+    private void createTestTables(Catalog catalog, Tuple2<Identifier, 
Schema>... tableSpecs)
+            throws Exception {
+
+        for (Tuple2<Identifier, Schema> spec : tableSpecs) {
+            catalog.createTable(spec.f0, spec.f1, false);
+        }
+    }
+
+    @AfterEach
+    public void after() {
+        // assert all connections are closed
+        Predicate<Path> pathPredicate = path -> 
path.toString().contains(tempDir.toString());
+        assertThat(TraceableFileIO.openInputStreams(pathPredicate)).isEmpty();
+        assertThat(TraceableFileIO.openOutputStreams(pathPredicate)).isEmpty();
+    }
+
+    private static FileIO getFileIO(CatalogContext catalogContext, Path 
warehouse) {
+        FileIO fileIO;
+        try {
+            fileIO = FileIO.get(warehouse, catalogContext);
+        } catch (IOException e) {
+            throw new UncheckedIOException(e);
+        }
+        return fileIO;
+    }
+
+    @Test
+    @Timeout(30)
+    public void testAsyncTableCreate() throws Exception {
+        // the async table will have same row type, partitions, and pks as 
firstTable
+        Identifier tableId = Identifier.create(databaseName, 
"async_new_table");
+
+        OneInputStreamOperatorTestHarness<CdcMultiplexRecord, 
MultiTableCommittable> harness =
+                createTestHarness(catalogLoader);
+
+        harness.open();
+
+        Runner runner = new Runner(harness);
+        Thread t = new Thread(runner);
+        t.start();
+
+        // check that records should be processed after table is created
+        Map<String, String> fields = new HashMap<>();
+        fields.put("pt", "0");
+        fields.put("k", "1");
+        fields.put("v", "10");
+
+        CdcMultiplexRecord expected =
+                CdcMultiplexRecord.fromCdcRecord(
+                        databaseName,
+                        tableId.getObjectName(),
+                        new CdcRecord(RowKind.INSERT, fields));
+        runner.offer(expected);
+        CdcMultiplexRecord actual = runner.poll(1);
+
+        assertThat(actual).isNull();
+
+        catalog.createTable(tableId, firstTableSchema, true);
+        actual = runner.take();
+        assertThat(actual).isEqualTo(expected);
+
+        // after table is created, record should be processed immediately
+        fields = new HashMap<>();
+        fields.put("pt", "0");
+        fields.put("k", "3");
+        fields.put("v", "30");
+        expected =
+                CdcMultiplexRecord.fromCdcRecord(
+                        databaseName,
+                        tableId.getObjectName(),
+                        new CdcRecord(RowKind.INSERT, fields));
+        runner.offer(expected);
+        actual = runner.take();
+        assertThat(actual).isEqualTo(expected);
+
+        runner.stop();
+        t.join();
+        harness.close();
+    }
+
+    @Test
+    @Timeout(30)
+    public void testInitializeState() throws Exception {

Review Comment:
   I mean using `OneInputStreamOperatorTestHarness.initializeState` to test 
state recover for the operator.



-- 
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]

Reply via email to