You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by su...@apache.org on 2023/03/11 13:26:47 UTC
[shardingsphere] branch master updated: Upgrade junit 5 for ShardingRouteCacheableCheckerTest (#24554)
This is an automated email from the ASF dual-hosted git repository.
sunnianjun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 97dd038e9c0 Upgrade junit 5 for ShardingRouteCacheableCheckerTest (#24554)
97dd038e9c0 is described below
commit 97dd038e9c0c2edd97f7def33aeed14398ebb834
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Mar 11 21:26:38 2023 +0800
Upgrade junit 5 for ShardingRouteCacheableCheckerTest (#24554)
* Upgrade junit 5 for ShardingRouteCacheableCheckerTest
* Upgrade junit 5 for ShardingRouteCacheableCheckerTest
---
.../checker/ShardingRouteCacheableCheckerTest.java | 117 ++++++++++-----------
1 file changed, 54 insertions(+), 63 deletions(-)
diff --git a/features/sharding/plugin/cache/src/test/java/org/apache/shardingsphere/sharding/cache/checker/ShardingRouteCacheableCheckerTest.java b/features/sharding/plugin/cache/src/test/java/org/apache/shardingsphere/sharding/cache/checker/ShardingRouteCacheableCheckerTest.java
index a33194983f4..7d3d2820ff5 100644
--- a/features/sharding/plugin/cache/src/test/java/org/apache/shardingsphere/sharding/cache/checker/ShardingRouteCacheableCheckerTest.java
+++ b/features/sharding/plugin/cache/src/test/java/org/apache/shardingsphere/sharding/cache/checker/ShardingRouteCacheableCheckerTest.java
@@ -17,10 +17,8 @@
package org.apache.shardingsphere.sharding.cache.checker;
-import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.binder.QueryContext;
import org.apache.shardingsphere.infra.binder.SQLStatementContextFactory;
-import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
@@ -49,10 +47,11 @@ import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.apache.shardingsphere.timeservice.api.config.TimeServiceRuleConfiguration;
import org.apache.shardingsphere.timeservice.core.rule.TimeServiceRule;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameters;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.ArgumentsProvider;
+import org.junit.jupiter.params.provider.ArgumentsSource;
import java.sql.Types;
import java.util.Arrays;
@@ -61,73 +60,31 @@ import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.function.Function;
-import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
-@RunWith(Parameterized.class)
-@RequiredArgsConstructor
public final class ShardingRouteCacheableCheckerTest {
private static final String DATABASE_NAME = "sharding_db";
private static final String SCHEMA_NAME = "public";
- private final String sql;
-
- private final List<Object> parameters;
-
- private final boolean expectedProbablyCacheable;
-
- private final List<Integer> expectedShardingConditionParameterMarkerIndexes;
-
- @Parameters(name = "probably cacheable: {2}, SQL: {0}")
- public static Iterable<Object[]> parameters() {
- Collection<Object[]> probablyCacheableCases = Arrays.asList(
- new Object[]{"insert into t_broadcast_table (broadcast_table_id, broadcast_table_col1) values (?, ?)", Arrays.asList(1, "foo"), true, Collections.emptyList()},
- new Object[]{"insert into t_warehouse (id) values (?)", Collections.singletonList(1), true, Collections.singletonList(0)},
- new Object[]{"select * from t_warehouse where id = ?", Collections.singletonList(1), true, Collections.singletonList(0)},
- new Object[]{"select * from t_warehouse where id in (?, ?, ?)", Arrays.asList(1, 2, 3), true, Arrays.asList(0, 1, 2)},
- new Object[]{"select * from t_warehouse where id between ? and ?", Arrays.asList(1, 10), true, Arrays.asList(0, 1)},
- new Object[]{"select * from t_warehouse where id between ? and ? limit ? offset ?", Arrays.asList(1, 10, 100, 50), true, Arrays.asList(0, 1)},
- new Object[]{"update t_broadcast_table set broadcast_table_col1 = ?", Collections.singletonList(0), true, Collections.emptyList()},
- new Object[]{"update t_broadcast_table set broadcast_table_col1 = ? where broadcast_table_id = ?", Arrays.asList(0, 1), true, Collections.emptyList()},
- new Object[]{"update t_warehouse set warehouse_name = ? where id = ?", Arrays.asList("foo", 1), true, Collections.singletonList(1)},
- new Object[]{"delete from t_broadcast_table", Collections.emptyList(), true, Collections.emptyList()},
- new Object[]{"delete from t_warehouse where id = ?", Collections.singletonList(1), true, Collections.singletonList(0)});
- Collection<Object[]> nonCacheableCases = Arrays.asList(
- new Object[]{"create table t_warehouse (id int4 not null primary key)", Collections.emptyList(), false, Collections.emptyList()},
- new Object[]{"insert into t_warehouse (id) select warehouse_id from t_order", Collections.emptyList(), false, Collections.emptyList()},
- new Object[]{"insert into t_broadcast_table (broadcast_table_id, broadcast_table_col1) values (?, ?), (?, ?)", Arrays.asList(1, "foo", 2, "bar"), false, Collections.emptyList()},
- new Object[]{"insert into t_warehouse (id) values (?), (?)", Arrays.asList(1, 2), false, Collections.emptyList()},
- new Object[]{"insert into t_non_sharding_table (id) values (?)", Collections.singletonList(1), false, Collections.emptyList()},
- new Object[]{"insert into t_non_cacheable_database_sharding (id) values (?)", Collections.singletonList(1), false, Collections.emptyList()},
- new Object[]{"insert into t_non_cacheable_table_sharding (id) values (?)", Collections.singletonList(1), false, Collections.emptyList()},
- new Object[]{"insert into t_warehouse (id) values (now())", Collections.emptyList(), false, Collections.emptyList()},
- new Object[]{"select * from t_broadcast_table where broadcast_table_id = ?", Collections.singletonList(1), false, Collections.emptyList()},
- new Object[]{"select * from t_warehouse w join t_order o on w.id = o.warehouse_id where w.id = ?", Collections.singletonList(1), false, Collections.emptyList()},
- new Object[]{"update t_warehouse set warehouse_name = ? where id = (select max(warehouse_id) from t_order)", Collections.singletonList("foo"), false, Collections.emptyList()},
- new Object[]{"delete from t_order where warehouse_id in (1, 2, now())", Collections.emptyList(), false, Collections.emptyList()},
- new Object[]{"delete from t_order where warehouse_id between now() and now()", Collections.emptyList(), false, Collections.emptyList()},
- new Object[]{"delete from t_order o where o.warehouse_id in (select w.id from t_warehouse w)", Collections.emptyList(), false, Collections.emptyList()});
- return Stream.of(probablyCacheableCases.stream(), nonCacheableCases.stream()).flatMap(Function.identity()).collect(Collectors.toList());
- }
-
- @Test
- public void assertCheckCacheable() {
- ShardingRule shardingRule = prepareShardingRule();
- ShardingCacheRule shardingCacheRule = prepareShardingCacheRule(shardingRule);
- TimeServiceRule timeServiceRule = prepareTimeServiceRule();
- ShardingSphereDatabase database = prepareDatabase(shardingRule, shardingCacheRule, timeServiceRule);
- ShardingRouteCacheableCheckResult actual = new ShardingRouteCacheableChecker(shardingCacheRule).check(database, prepareQueryContext(database, sql, parameters));
+ @ParameterizedTest(name = "probably cacheable: {2}, SQL: {0}")
+ @ArgumentsSource(TestCaseArgumentsProvider.class)
+ public void assertCheckCacheable(final String sql, final List<Object> parameters, final boolean expectedProbablyCacheable, final List<Integer> expectedShardingConditionParameterMarkerIndexes) {
+ ShardingRule shardingRule = createShardingRule();
+ ShardingCacheRule shardingCacheRule = createShardingCacheRule(shardingRule);
+ TimeServiceRule timeServiceRule = createTimeServiceRule();
+ ShardingSphereDatabase database = createDatabase(shardingRule, shardingCacheRule, timeServiceRule);
+ ShardingRouteCacheableCheckResult actual = new ShardingRouteCacheableChecker(shardingCacheRule).check(database, createQueryContext(database, sql, parameters));
assertThat(actual.isProbablyCacheable(), is(expectedProbablyCacheable));
assertThat(actual.getShardingConditionParameterMarkerIndexes(), is(expectedShardingConditionParameterMarkerIndexes));
}
- private ShardingRule prepareShardingRule() {
+ private ShardingRule createShardingRule() {
ShardingRuleConfiguration ruleConfig = new ShardingRuleConfiguration();
ruleConfig.getBroadcastTables().add("t_broadcast_table");
ruleConfig.getBindingTableGroups().add(new ShardingTableReferenceRuleConfiguration("foo", "t_order,t_order_item"));
@@ -148,16 +105,16 @@ public final class ShardingRouteCacheableCheckerTest {
return new ShardingRule(ruleConfig, Arrays.asList("ds_0", "ds_1"), new InstanceContext(mock(ComputeNodeInstance.class), props -> 0, null, null, null, null));
}
- private ShardingCacheRule prepareShardingCacheRule(final ShardingRule shardingRule) {
+ private ShardingCacheRule createShardingCacheRule(final ShardingRule shardingRule) {
return new ShardingCacheRule(new ShardingCacheRuleConfiguration(100, new ShardingCacheOptions(true, 0, 0)), shardingRule,
new TimeServiceRule(new TimeServiceRuleConfiguration("System", new Properties())));
}
- private TimeServiceRule prepareTimeServiceRule() {
+ private TimeServiceRule createTimeServiceRule() {
return new TimeServiceRule(new TimeServiceRuleConfiguration("System", new Properties()));
}
- private ShardingSphereDatabase prepareDatabase(final ShardingRule shardingRule, final ShardingCacheRule shardingCacheRule, final TimeServiceRule timeServiceRule) {
+ private ShardingSphereDatabase createDatabase(final ShardingRule shardingRule, final ShardingCacheRule shardingCacheRule, final TimeServiceRule timeServiceRule) {
ShardingSphereSchema schema = new ShardingSphereSchema();
schema.getTables().put("t_broadcast_table", new ShardingSphereTable("t_broadcast_table", Arrays.asList(
new ShardingSphereColumn("broadcast_table_id", Types.INTEGER, true, false, false, true, false),
@@ -179,9 +136,8 @@ public final class ShardingRouteCacheableCheckerTest {
Collections.singletonMap(SCHEMA_NAME, schema));
}
- private QueryContext prepareQueryContext(final ShardingSphereDatabase database, final String sql, final List<Object> params) {
- SQLStatementContext<?> sqlStatementContext = SQLStatementContextFactory.newInstance(createShardingSphereMetaData(database), params, parse(sql), DATABASE_NAME);
- return new QueryContext(sqlStatementContext, sql, params);
+ private QueryContext createQueryContext(final ShardingSphereDatabase database, final String sql, final List<Object> params) {
+ return new QueryContext(SQLStatementContextFactory.newInstance(createShardingSphereMetaData(database), params, parse(sql), DATABASE_NAME), sql, params);
}
private ShardingSphereMetaData createShardingSphereMetaData(final ShardingSphereDatabase database) {
@@ -192,4 +148,39 @@ public final class ShardingRouteCacheableCheckerTest {
CacheOption cacheOption = new CacheOption(0, 0);
return new SQLStatementParserEngine("PostgreSQL", cacheOption, cacheOption, false).parse(sql, false);
}
+
+ private static class TestCaseArgumentsProvider implements ArgumentsProvider {
+
+ @Override
+ public Stream<? extends Arguments> provideArguments(final ExtensionContext extensionContext) {
+ Collection<? extends Arguments> probablyCacheableCases = Arrays.asList(
+ Arguments.of("insert into t_broadcast_table (broadcast_table_id, broadcast_table_col1) values (?, ?)", Arrays.asList(1, "foo"), true, Collections.emptyList()),
+ Arguments.of("insert into t_warehouse (id) values (?)", Collections.singletonList(1), true, Collections.singletonList(0)),
+ Arguments.of("select * from t_warehouse where id = ?", Collections.singletonList(1), true, Collections.singletonList(0)),
+ Arguments.of("select * from t_warehouse where id in (?, ?, ?)", Arrays.asList(1, 2, 3), true, Arrays.asList(0, 1, 2)),
+ Arguments.of("select * from t_warehouse where id between ? and ?", Arrays.asList(1, 10), true, Arrays.asList(0, 1)),
+ Arguments.of("select * from t_warehouse where id between ? and ? limit ? offset ?", Arrays.asList(1, 10, 100, 50), true, Arrays.asList(0, 1)),
+ Arguments.of("update t_broadcast_table set broadcast_table_col1 = ?", Collections.singletonList(0), true, Collections.emptyList()),
+ Arguments.of("update t_broadcast_table set broadcast_table_col1 = ? where broadcast_table_id = ?", Arrays.asList(0, 1), true, Collections.emptyList()),
+ Arguments.of("update t_warehouse set warehouse_name = ? where id = ?", Arrays.asList("foo", 1), true, Collections.singletonList(1)),
+ Arguments.of("delete from t_broadcast_table", Collections.emptyList(), true, Collections.emptyList()),
+ Arguments.of("delete from t_warehouse where id = ?", Collections.singletonList(1), true, Collections.singletonList(0)));
+ Collection<? extends Arguments> nonCacheableCases = Arrays.asList(
+ Arguments.of("create table t_warehouse (id int4 not null primary key)", Collections.emptyList(), false, Collections.emptyList()),
+ Arguments.of("insert into t_warehouse (id) select warehouse_id from t_order", Collections.emptyList(), false, Collections.emptyList()),
+ Arguments.of("insert into t_broadcast_table (broadcast_table_id, broadcast_table_col1) values (?, ?), (?, ?)", Arrays.asList(1, "foo", 2, "bar"), false, Collections.emptyList()),
+ Arguments.of("insert into t_warehouse (id) values (?), (?)", Arrays.asList(1, 2), false, Collections.emptyList()),
+ Arguments.of("insert into t_non_sharding_table (id) values (?)", Collections.singletonList(1), false, Collections.emptyList()),
+ Arguments.of("insert into t_non_cacheable_database_sharding (id) values (?)", Collections.singletonList(1), false, Collections.emptyList()),
+ Arguments.of("insert into t_non_cacheable_table_sharding (id) values (?)", Collections.singletonList(1), false, Collections.emptyList()),
+ Arguments.of("insert into t_warehouse (id) values (now())", Collections.emptyList(), false, Collections.emptyList()),
+ Arguments.of("select * from t_broadcast_table where broadcast_table_id = ?", Collections.singletonList(1), false, Collections.emptyList()),
+ Arguments.of("select * from t_warehouse w join t_order o on w.id = o.warehouse_id where w.id = ?", Collections.singletonList(1), false, Collections.emptyList()),
+ Arguments.of("update t_warehouse set warehouse_name = ? where id = (select max(warehouse_id) from t_order)", Collections.singletonList("foo"), false, Collections.emptyList()),
+ Arguments.of("delete from t_order where warehouse_id in (1, 2, now())", Collections.emptyList(), false, Collections.emptyList()),
+ Arguments.of("delete from t_order where warehouse_id between now() and now()", Collections.emptyList(), false, Collections.emptyList()),
+ Arguments.of("delete from t_order o where o.warehouse_id in (select w.id from t_warehouse w)", Collections.emptyList(), false, Collections.emptyList()));
+ return Stream.of(probablyCacheableCases.stream(), nonCacheableCases.stream()).flatMap(Function.identity());
+ }
+ }
}