Skip to content

Commit

Permalink
Refactor: test-e2e-sql use the awaitility framework replace sleep in …
Browse files Browse the repository at this point in the history
…RDLE2EIT class (apache#24106)
  • Loading branch information
wyx committed Jul 28, 2023
1 parent 2d2e5a9 commit 9fdf1f0
Show file tree
Hide file tree
Showing 78 changed files with 2,493 additions and 111 deletions.
4 changes: 4 additions & 0 deletions test/e2e/sql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,9 @@
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ private Stream<Arguments> getBatchTestCaseArguments(final SQLCommandType type) {
private Stream<Arguments> getSingleTestCaseArguments(final SQLCommandType type) {
Collection<AssertionTestParameter> result = E2ETestParameterFactory.getAssertionTestParameters(type);
// TODO make sure test case can not be null
return result.isEmpty() ? Stream.of(Arguments.of(new AssertionTestParameter(null, null, null, null, null, null, null, null))) : result.stream().map(Arguments::of);
return result.isEmpty() ? Stream.of(Arguments.of(new AssertionTestParameter(null, null, null, null, null, null, null, null, null))) : result.stream().map(Arguments::of);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.apache.shardingsphere.test.e2e.cases.value.SQLValue;
import org.apache.shardingsphere.test.e2e.framework.param.model.AssertionTestParameter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -41,6 +43,10 @@ public final class SingleE2EContainerComposer extends E2EContainerComposer {

private final IntegrationTestCaseAssertion assertion;

private final Collection<IntegrationTestCaseAssertion> assertions;

private final List<DataSet> dataSets = new ArrayList<>();

private final DataSet dataSet;

private final DataSet generatedKeyDataSet;
Expand All @@ -49,7 +55,16 @@ public SingleE2EContainerComposer(final AssertionTestParameter testParam) {
super(testParam);
sql = testParam.getTestCaseContext().getTestCase().getSql();
sqlExecuteType = testParam.getSqlExecuteType();
assertions = testParam.getAssertions();
assertion = testParam.getAssertion();

for (IntegrationTestCaseAssertion caseAssertion : assertions) {
dataSets.add(null == caseAssertion || null == caseAssertion.getExpectedDataFile()
? null
: DataSetLoader.load(testParam.getTestCaseContext().getParentPath(), testParam.getScenario(), testParam.getDatabaseType(), testParam.getMode(),
caseAssertion.getExpectedDataFile()));
}

dataSet = null == assertion || null == assertion.getExpectedDataFile()
? null
: DataSetLoader.load(testParam.getTestCaseContext().getParentPath(), testParam.getScenario(), testParam.getDatabaseType(), testParam.getMode(), assertion.getExpectedDataFile());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

package org.apache.shardingsphere.test.e2e.engine.type;

import com.google.common.base.Splitter;
import org.apache.shardingsphere.test.e2e.cases.SQLCommandType;
import org.apache.shardingsphere.test.e2e.cases.assertion.IntegrationTestCaseAssertion;
import org.apache.shardingsphere.test.e2e.cases.dataset.metadata.DataSetColumn;
import org.apache.shardingsphere.test.e2e.cases.dataset.metadata.DataSetMetaData;
import org.apache.shardingsphere.test.e2e.cases.dataset.row.DataSetRow;
Expand All @@ -27,25 +27,23 @@
import org.apache.shardingsphere.test.e2e.engine.composer.SingleE2EContainerComposer;
import org.apache.shardingsphere.test.e2e.framework.param.array.E2ETestParameterFactory;
import org.apache.shardingsphere.test.e2e.framework.param.model.AssertionTestParameter;
import org.awaitility.Awaitility;
import org.awaitility.Durations;
import org.junit.jupiter.api.condition.EnabledIf;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.testcontainers.shaded.org.awaitility.Awaitility;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@E2ETestCaseSettings(SQLCommandType.RDL)
Expand All @@ -60,79 +58,108 @@ void assertExecute(final AssertionTestParameter testParam) throws SQLException {
return;
}
SingleE2EContainerComposer containerComposer = new SingleE2EContainerComposer(testParam);
init(containerComposer);
assertExecute(testParam, containerComposer);
tearDown(containerComposer);

if (null == containerComposer.getAssertions()) {
return;
}
int dataSetIndex = 0;
String mode = testParam.getMode();

for (IntegrationTestCaseAssertion each : containerComposer.getAssertions()) {
if (each.getInitialSQL() != null && each.getAssertionSQL() != null) {
init(containerComposer, each, dataSetIndex, mode);
}
if (each.getAssertionSQL() != null && each.getDestroySQL() == null && each.getInitialSQL() == null) {
executeSQLCase(containerComposer, each, dataSetIndex, mode);
}
if (each.getDestroySQL() != null && each.getAssertionSQL() != null) {
tearDown(containerComposer, each, dataSetIndex, mode);
}
dataSetIndex++;
}
}

private void assertExecute(final AssertionTestParameter testParam, final SingleE2EContainerComposer containerComposer) throws SQLException {
assertNotNull(testParam.getAssertion().getAssertionSQL(), "Assertion SQL is required");
private void executeSQLCase(final SingleE2EContainerComposer containerComposer, final IntegrationTestCaseAssertion testCaseExecuteSql,
final int dataSetIndex, final String mode) throws SQLException {
try (Connection connection = containerComposer.getTargetDataSource().getConnection()) {
try (Statement statement = connection.createStatement()) {
executeSQLCase(containerComposer, statement);
Awaitility.await().pollDelay(2L, TimeUnit.SECONDS).until(() -> true);
assertResultSet(containerComposer, statement);
statement.execute(containerComposer.getSQL());

executeAssertionSQL(containerComposer, statement, testCaseExecuteSql, dataSetIndex, mode);
}
}
}

private void executeSQLCase(final SingleE2EContainerComposer containerComposer, final Statement statement) throws SQLException {
statement.execute(containerComposer.getSQL());
}

private void init(final SingleE2EContainerComposer containerComposer) throws SQLException {
private void init(final SingleE2EContainerComposer containerComposer, final IntegrationTestCaseAssertion testCaseInitSql,
final int dataSetIndex, final String mode) throws SQLException {
try (Connection connection = containerComposer.getTargetDataSource().getConnection()) {
executeInitSQLs(containerComposer, connection);
executeInitSQLs(containerComposer, connection, testCaseInitSql, dataSetIndex, mode);
}
}

private void tearDown(final SingleE2EContainerComposer containerComposer) throws SQLException {
if (null != containerComposer.getAssertion().getDestroySQL()) {
try (Connection connection = containerComposer.getTargetDataSource().getConnection()) {
executeDestroySQLs(containerComposer, connection);
}
private void tearDown(final SingleE2EContainerComposer containerComposer, final IntegrationTestCaseAssertion testCaseDestroySQL,
final int dataSetIndex, final String mode) throws SQLException {
try (Connection connection = containerComposer.getTargetDataSource().getConnection()) {
executeDestroySQLs(containerComposer, connection, testCaseDestroySQL, dataSetIndex, mode);
}
Awaitility.await().pollDelay(2L, TimeUnit.SECONDS).until(() -> true);
}

private void executeInitSQLs(final SingleE2EContainerComposer containerComposer, final Connection connection) throws SQLException {
if (null == containerComposer.getAssertion().getInitialSQL() || null == containerComposer.getAssertion().getInitialSQL().getSql()) {
private void executeInitSQLs(final SingleE2EContainerComposer containerComposer, final Connection connection,
final IntegrationTestCaseAssertion testCaseInitSql, int dataSetIndex, String mode) throws SQLException {
if (null == testCaseInitSql.getInitialSQL() || null == testCaseInitSql.getInitialSQL().getSql()) {
return;
}
for (String each : Splitter.on(";").trimResults().splitToList(containerComposer.getAssertion().getInitialSQL().getSql())) {
try (PreparedStatement preparedStatement = connection.prepareStatement(each)) {
preparedStatement.executeUpdate();
Awaitility.await().pollDelay(2L, TimeUnit.SECONDS).until(() -> true);
}
try (Statement statement = connection.createStatement()) {
statement.executeUpdate(testCaseInitSql.getInitialSQL().getSql());

executeAssertionSQL(containerComposer, statement, testCaseInitSql, dataSetIndex, mode);
}
}

private void executeDestroySQLs(final SingleE2EContainerComposer containerComposer, final Connection connection) throws SQLException {
if (null == containerComposer.getAssertion().getDestroySQL().getSql()) {
private void executeAssertionSQL(final SingleE2EContainerComposer containerComposer, final Statement statement,
final IntegrationTestCaseAssertion testCaseAssertionSQL,
final int dataSetIndex, final String mode) {
if ("Cluster".equals(mode)) {
Awaitility.await().atMost(Durations.ONE_MINUTE).until(() -> assertResultSet(containerComposer, statement, testCaseAssertionSQL, dataSetIndex));
} else if ("Standalone".equals(mode)) {
assertResultSet(containerComposer, statement, testCaseAssertionSQL, dataSetIndex);
}
}

private void executeDestroySQLs(final SingleE2EContainerComposer containerComposer, final Connection connection,
final IntegrationTestCaseAssertion testCaseDestroySQL,
final int dataSetIndex, final String mode) throws SQLException {
if (null == testCaseDestroySQL.getDestroySQL() || null == testCaseDestroySQL.getDestroySQL().getSql()) {
return;
}
for (String each : Splitter.on(";").trimResults().splitToList(containerComposer.getAssertion().getDestroySQL().getSql())) {
try (PreparedStatement preparedStatement = connection.prepareStatement(each)) {
preparedStatement.executeUpdate();
Awaitility.await().pollDelay(2L, TimeUnit.SECONDS).until(() -> true);
}
try (Statement statement = connection.createStatement()) {
statement.executeUpdate(testCaseDestroySQL.getDestroySQL().getSql());

executeAssertionSQL(containerComposer, statement, testCaseDestroySQL, dataSetIndex, mode);
}
}

private void assertResultSet(final SingleE2EContainerComposer containerComposer, final Statement statement) throws SQLException {
try (ResultSet resultSet = statement.executeQuery(containerComposer.getAssertion().getAssertionSQL().getSql())) {
assertResultSet(containerComposer, resultSet);
private boolean assertResultSet(final SingleE2EContainerComposer containerComposer, final Statement statement,
final IntegrationTestCaseAssertion testCaseAssertionSql,
final int dataSetIndex) {
try (ResultSet resultSet = statement.executeQuery(testCaseAssertionSql.getAssertionSQL().getSql())) {
assertResultSet(containerComposer, resultSet, dataSetIndex);
return true;
} catch (final SQLException ignored) {
return false;
}
}

private void assertResultSet(final SingleE2EContainerComposer containerComposer, final ResultSet resultSet) throws SQLException {
assertMetaData(resultSet.getMetaData(), getExpectedColumns(containerComposer));
assertRows(resultSet, containerComposer.getDataSet().getRows());
private void assertResultSet(final SingleE2EContainerComposer containerComposer, final ResultSet resultSet,
final int dataSetIndex) throws SQLException {
assertMetaData(resultSet.getMetaData(), getExpectedColumns(containerComposer, dataSetIndex));
assertRows(resultSet, containerComposer.getDataSets().get(dataSetIndex).getRows());
}

private Collection<DataSetColumn> getExpectedColumns(final SingleE2EContainerComposer containerComposer) {
private Collection<DataSetColumn> getExpectedColumns(final SingleE2EContainerComposer containerComposer, final int dataSetIndex) {
Collection<DataSetColumn> result = new LinkedList<>();
for (DataSetMetaData each : containerComposer.getDataSet().getMetaDataList()) {
List<DataSetMetaData> dataSetMetaDataList = containerComposer.getDataSets().get(dataSetIndex).getMetaDataList();
for (DataSetMetaData each : dataSetMetaDataList) {
result.addAll(each.getColumns());
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,30 +94,33 @@ private Collection<AssertionTestParameter> getAssertionTestParameter(final Integ
private Collection<AssertionTestParameter> getAssertionTestParameter(final IntegrationTestCaseContext testCaseContext,
final DatabaseType databaseType, final SQLExecuteType sqlExecuteType, final SQLCommandType sqlCommandType) {
Collection<AssertionTestParameter> result = new LinkedList<>();
for (IntegrationTestCaseAssertion each : testCaseContext.getTestCase().getAssertions()) {
result.addAll(getAssertionTestParameter(testCaseContext, databaseType, sqlExecuteType, each, sqlCommandType));
}
IntegrationTestCaseAssertion each = testCaseContext.getTestCase().getAssertions().iterator().next();
result.addAll(getAssertionTestParameter(testCaseContext, databaseType, sqlExecuteType, each, testCaseContext.getTestCase().getAssertions(), sqlCommandType));

return result;
}

private Collection<AssertionTestParameter> getAssertionTestParameter(final IntegrationTestCaseContext testCaseContext,
final DatabaseType databaseType, final SQLExecuteType sqlExecuteType,
final IntegrationTestCaseAssertion assertion, final SQLCommandType sqlCommandType) {
final IntegrationTestCaseAssertion assertion,
final Collection<IntegrationTestCaseAssertion> assertions,
final SQLCommandType sqlCommandType) {
Collection<AssertionTestParameter> result = new LinkedList<>();
for (String each : getEnvAdapters(testCaseContext.getTestCase().getAdapters())) {
if (sqlCommandType.getRunningAdaptors().contains(each) && envAdapters.contains(each)) {
result.addAll(getAssertionTestParameter(testCaseContext, assertion, each, databaseType, sqlExecuteType, sqlCommandType));
result.addAll(getAssertionTestParameter(testCaseContext, assertion, assertions, each, databaseType, sqlExecuteType, sqlCommandType));
}
}
return result;
}

private Collection<AssertionTestParameter> getAssertionTestParameter(final IntegrationTestCaseContext testCaseContext, final IntegrationTestCaseAssertion assertion,
final Collection<IntegrationTestCaseAssertion> assertions,
final String adapter, final DatabaseType databaseType,
final SQLExecuteType sqlExecuteType, final SQLCommandType sqlCommandType) {
Collection<String> scenarios = null == testCaseContext.getTestCase().getScenarioTypes() ? Collections.emptyList() : Arrays.asList(testCaseContext.getTestCase().getScenarioTypes().split(","));
return envScenarios.stream().filter(each -> filterScenarios(each, scenarios, sqlCommandType.getSqlStatementClass()))
.map(each -> new AssertionTestParameter(testCaseContext, assertion, adapter, each, envMode, databaseType, sqlExecuteType, sqlCommandType)).collect(Collectors.toList());
.map(each -> new AssertionTestParameter(testCaseContext, assertion, assertions, adapter, each, envMode, databaseType, sqlExecuteType, sqlCommandType)).collect(Collectors.toList());
}

private Collection<String> getEnvAdapters(final String envAdapters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.shardingsphere.test.e2e.cases.SQLExecuteType;
import org.apache.shardingsphere.test.e2e.cases.assertion.IntegrationTestCaseAssertion;

import java.util.Collection;
/**
* Assertion test parameter.
*/
Expand All @@ -36,6 +37,8 @@ public final class AssertionTestParameter implements E2ETestParameter {

private final IntegrationTestCaseAssertion assertion;

private final Collection<IntegrationTestCaseAssertion> assertions;

private final String adapter;

private final String scenario;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!--
~ 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.
-->

<dataset>
<metadata>
<column name="name" />
<column name="sharding_table_reference" />
</metadata>
<row values="reference_0| t_order,t_order_item" />
</dataset>
Loading

0 comments on commit 9fdf1f0

Please sign in to comment.