xtern commented on code in PR #2674: URL: https://github.com/apache/ignite-3/pull/2674#discussion_r1356692257
########## modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/AsyncSqlScriptCursorTest.java: ########## @@ -0,0 +1,146 @@ +/* + * 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.ignite.internal.sql.engine; + +import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willBe; +import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.util.Collections; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import org.apache.ignite.internal.sql.api.ResultSetMetadataImpl; +import org.apache.ignite.internal.sql.engine.framework.NoOpTransaction; +import org.apache.ignite.internal.testframework.IgniteTestUtils.RunnableX; +import org.apache.ignite.internal.util.AsyncCursor; +import org.apache.ignite.internal.util.AsyncCursor.BatchedResult; +import org.apache.ignite.internal.util.AsyncWrapper; +import org.apache.ignite.sql.ResultSetMetadata; +import org.junit.jupiter.api.Test; + +/** + * Tests for {@link AsyncSqlScriptCursor}. + */ +public class AsyncSqlScriptCursorTest { + private static final int PAGE_SIZE = 1; + + @Test + public void listenerIsCalledOnFirstPageOnly() throws Exception { + TestPageLoadListener listener = new TestPageLoadListener(() -> {}); + AsyncCursor<Integer> cursor = prepareTestCursor(listener); + + assertThat(listener.callsCounter.get(), is(0)); + + assertThat(cursor.requestNextAsync(PAGE_SIZE), willCompleteSuccessfully()); + assertThat(listener.callsCountAsync(), willBe(1)); + + assertThat(fetchAll(cursor), is(true)); + assertThat(listener.callsCountAsync(), willBe(1)); + } + + @Test + public void testErrorInPrefetchListenerDoesNotAffectExecution() { + TestPageLoadListener listener = new TestPageLoadListener(() -> { + throw new RuntimeException("Expected"); + }); + + AsyncCursor<Integer> cursor = prepareTestCursor(listener); + + // Ensure request future completes without errors. + assertThat(cursor.requestNextAsync(PAGE_SIZE), willCompleteSuccessfully()); + } + + @Test + public void testPrefetchListenerExecutesAsynchronously() throws Exception { + CountDownLatch latch = new CountDownLatch(1); + TestPageLoadListener listener = new TestPageLoadListener(() -> latch.await(30, TimeUnit.SECONDS)); + + // Make sure all data is fetched successfully. + assertThat(fetchAll(prepareTestCursor(listener)), is(true)); + + assertThat(listener.callsCountAsync(), willBe(0)); + + latch.countDown(); + + assertThat(listener.callsCountAsync(), willBe(1)); + } + + private static AsyncCursor<Integer> prepareTestCursor(PageLoadListener listener) { + AsyncCursor<Integer> data = new AsyncWrapper<>(IntStream.range(0, 10).boxed().collect(Collectors.toList()).iterator()); + QueryTransactionWrapper tx = new QueryTransactionWrapper(NoOpTransaction.readOnly("TX"), true); + ResultSetMetadata rsMeta = new ResultSetMetadataImpl(Collections.emptyList()); + + return new AsyncSqlScriptCursor<>(SqlQueryType.QUERY, rsMeta, tx, data, listener); + } + + private static boolean fetchAll(AsyncCursor<Integer> cursor) throws ExecutionException, InterruptedException, TimeoutException { Review Comment: Done -- 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]
