Skip to content

Commit

Permalink
IGNITE-23441 Test code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
xtern committed Nov 14, 2024
1 parent a67ab84 commit c942ddd
Showing 1 changed file with 12 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@

package org.apache.ignite.internal.sql.engine;

import static org.apache.ignite.internal.sql.engine.QueryProperty.ALLOWED_QUERY_TYPES;
import static org.apache.ignite.internal.testframework.IgniteTestUtils.await;
import static org.apache.ignite.internal.testframework.IgniteTestUtils.waitForCondition;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
Expand All @@ -29,22 +28,23 @@
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import org.apache.ignite.internal.TestWrappers;
import org.apache.ignite.internal.app.IgniteImpl;
import org.apache.ignite.internal.sql.engine.property.SqlProperties;
import org.apache.ignite.internal.sql.engine.property.SqlPropertiesHelper;
import org.apache.ignite.internal.testframework.IgniteTestUtils;
import org.apache.ignite.internal.tx.HybridTimestampTracker;
import org.apache.ignite.internal.util.AsyncCursor.BatchedResult;
import org.apache.ignite.lang.CancelHandle;
import org.apache.ignite.lang.CancellationToken;
import org.apache.ignite.lang.ErrorGroups.Sql;
import org.apache.ignite.sql.SqlException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

/** Set of test cases for multi-statement query cancellation. */
public class ItMultiStatementQueryCancelTest extends BaseSqlMultiStatementTest {

@AfterEach
void expectNoRunningQueries() {
assertThat(queryProcessor().runningQueries(), is(0));
}

@Test
public void cancelScriptBeforeExecution() {
CancelHandle cancelHandle = CancelHandle.create();
Expand All @@ -58,65 +58,30 @@ public void cancelScriptBeforeExecution() {
() -> runScript(token, "SELECT 1; SELECT 2;"),
"The query was cancelled while executing."
);

assertThat(queryProcessor().runningQueries(), is(0));
}

@Test
public void cancelBasicMultiStatementQuery() throws InterruptedException {
IgniteImpl igniteImpl = TestWrappers.unwrapIgniteImpl(CLUSTER.node(0));

SqlProperties properties = SqlPropertiesHelper.newBuilder()
.set(ALLOWED_QUERY_TYPES, SqlQueryType.ALL)
.build();

HybridTimestampTracker hybridTimestampTracker = igniteImpl.observableTimeTracker();

SqlQueryProcessor qryProc = queryProcessor();

public void cancelBasicMultiStatementQuery() {
StringBuilder query = new StringBuilder();

int statementsCount = 100;

for (int j = 0; j < statementsCount; j++) {
query.append("SELECT v FROM (VALUES ");
for (int i = 0; i < 100; i++) {
if (i > 0) {
query.append(", ");
}
query.append("(");
query.append(j * 100 + i);
query.append(")");
}
query.append(" ) t(v);");
query.append("SELECT ").append(j).append("; ");
}

CancelHandle cancelHandle = CancelHandle.create();
CancellationToken token = cancelHandle.token();

AsyncSqlCursor<InternalSqlRow> query1 = await(qryProc.queryAsync(
properties,
hybridTimestampTracker,
null,
token,
query.toString()
));

int expectedQueriesCount = statementsCount + /* SCRIPT */ 1;
List<AsyncSqlCursor<InternalSqlRow>> allCursors = fetchAllCursors(runScript(token, query.toString()));

waitForCondition(() -> qryProc.runningQueries() == expectedQueriesCount, 5_000);
assertThat(allCursors, hasSize(statementsCount));
assertThat(queryProcessor().runningQueries(), is(statementsCount + /* SCRIPT */ 1));

assertThat(qryProc.runningQueries(), is(expectedQueriesCount));

// Request cancellation.
CompletableFuture<Void> cancelled = cancelHandle.cancelAsync();

List<AsyncSqlCursor<InternalSqlRow>> allCursors = fetchAllCursors(query1);
allCursors.forEach(cursor -> expectQueryCancelled(new DrainCursor(cursor)));

await(cancelled);

assertThat(qryProc.runningQueries(), is(0));
}

private static void expectQueryCancelled(Runnable action) {
Expand Down

0 comments on commit c942ddd

Please sign in to comment.