fsk119 commented on code in PR #19823: URL: https://github.com/apache/flink/pull/19823#discussion_r903417787
########## flink-table/flink-sql-gateway/src/test/java/org/apache/flink/table/gateway/service/result/ResultFetcherTest.java: ########## @@ -0,0 +1,355 @@ +/* + * 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.table.gateway.service.result; + +import org.apache.flink.core.testutils.CommonTestUtils; +import org.apache.flink.core.testutils.FlinkAssertions; +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.catalog.Column; +import org.apache.flink.table.catalog.ResolvedSchema; +import org.apache.flink.table.data.GenericRowData; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.gateway.api.operation.OperationHandle; +import org.apache.flink.table.gateway.api.results.ResultSet; +import org.apache.flink.table.gateway.api.utils.SqlGatewayException; +import org.apache.flink.types.RowKind; +import org.apache.flink.util.CloseableIterator; + +import org.apache.commons.collections.iterators.IteratorChain; +import org.junit.Before; +import org.junit.Test; + +import java.math.BigDecimal; +import java.sql.Timestamp; +import java.time.Duration; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.atomic.AtomicReference; + +import static org.apache.flink.util.Preconditions.checkNotNull; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** Test for {@link ResultFetcher}. */ +public class ResultFetcherTest { + + private ResolvedSchema schema; + private List<RowData> data; + + @Before + public void setUp() { + schema = + ResolvedSchema.of( + Column.physical("boolean", DataTypes.BOOLEAN()), + Column.physical("int", DataTypes.INT()), + Column.physical("bigint", DataTypes.BIGINT()), + Column.physical("varchar", DataTypes.STRING()), + Column.physical("decimal(10, 5)", DataTypes.DECIMAL(10, 5)), + Column.physical( + "timestamp", DataTypes.TIMESTAMP(6).bridgedTo(Timestamp.class)), + Column.physical("binary", DataTypes.BYTES())); + data = + Arrays.asList( + GenericRowData.ofKind( + RowKind.INSERT, + null, + 1, + 2L, + "abc", + BigDecimal.valueOf(1.23), + Timestamp.valueOf("2020-03-01 18:39:14"), + new byte[] {50, 51, 52, -123, 54, 93, 115, 126}), + GenericRowData.ofKind( + RowKind.UPDATE_BEFORE, + false, + null, + 0L, + "", + BigDecimal.valueOf(1), + Timestamp.valueOf("2020-03-01 18:39:14.1"), + new byte[] {100, -98, 32, 121, -125}), + GenericRowData.ofKind( + RowKind.UPDATE_AFTER, + true, + Integer.MAX_VALUE, + null, + "abcdefg", + BigDecimal.valueOf(12345), + Timestamp.valueOf("2020-03-01 18:39:14.12"), + new byte[] {-110, -23, 1, 2}), + GenericRowData.ofKind( + RowKind.DELETE, + false, + Integer.MIN_VALUE, + Long.MAX_VALUE, + null, + BigDecimal.valueOf(12345.06789), + Timestamp.valueOf("2020-03-01 18:39:14.123"), + new byte[] {50, 51, 52, -123, 54, 93, 115, 126}), + GenericRowData.ofKind( + RowKind.INSERT, + true, + 100, + Long.MIN_VALUE, + "abcdefg111", + null, + Timestamp.valueOf("2020-03-01 18:39:14.123456"), + new byte[] {110, 23, -1, -2}), + GenericRowData.ofKind( + RowKind.DELETE, + null, + -1, + -1L, + "abcdefghijklmnopqrstuvwxyz", + BigDecimal.valueOf(-12345.06789), + null, + null), + GenericRowData.ofKind( + RowKind.INSERT, + null, + -1, + -1L, + "这是一段中文", + BigDecimal.valueOf(-12345.06789), + Timestamp.valueOf("2020-03-04 18:39:14"), + new byte[] {-3, -2, -1, 0, 1, 2, 3}), + GenericRowData.ofKind( + RowKind.DELETE, + null, + -1, + -1L, + "これは日本語をテストするための文です", + BigDecimal.valueOf(-12345.06789), + Timestamp.valueOf("2020-03-04 18:39:14"), + new byte[] {-3, -2, -1, 0, 1, 2, 3})); + } + + @Test + public void testFetchResultsMultipleTimesWithLimitedBufferSize() { + int bufferSize = data.size() / 2; + ResultFetcher fetcher = + buildResultFetcher(Collections.singletonList(data.iterator()), bufferSize); + + runFetchMultipleTimes(fetcher, bufferSize, data.size()); + } + + @Test + public void testFetchResultsMultipleTimesWithLimitedFetchSize() { + int bufferSize = data.size(); + ResultFetcher fetcher = + buildResultFetcher(Collections.singletonList(data.iterator()), bufferSize); + + runFetchMultipleTimes(fetcher, bufferSize, data.size() / 2); + } + + @Test + public void testFetchResultInParallel() { + int bufferSize = data.size() / 2; + ResultFetcher fetcher = + buildResultFetcher(Collections.singletonList(data.iterator()), bufferSize); + + AtomicReference<Boolean> isEqual = new AtomicReference<>(true); + + for (int i = 0; i < 10; i++) { + new Thread( + () -> { + ResultSet resultSet = fetcher.fetchResults(0, Integer.MAX_VALUE); + + if (!data.subList(0, bufferSize).equals(resultSet.getData())) { + isEqual.set(false); + } + fetcher.close(); + }) + .start(); + } + Review Comment: Thread#join watis until the thread to die. Use CountDownLatch to make sure all sub thread comes to end. -- 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]
