This is an automated email from the ASF dual-hosted git repository.
soulasuna 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 d7a7e24bfe2 Add unit test for #18473 (#18511)
d7a7e24bfe2 is described below
commit d7a7e24bfe26a186f9d0859dc52a5043cefada88
Author: zhaojinchao <[email protected]>
AuthorDate: Wed Jun 22 18:34:31 2022 +0800
Add unit test for #18473 (#18511)
---
...eDiscoveryDynamicDataSourceStrategyFixture.java | 40 +++++++++++++++++++++
.../route/ReadwriteSplittingSQLRouterTest.java | 41 ++++++++++++++++++++++
...a.datasource.strategy.DynamicDataSourceStrategy | 18 ++++++++++
3 files changed, 99 insertions(+)
diff --git
a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/fixture/DatabaseDiscoveryDynamicDataSourceStrategyFixture.java
b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/fixture/DatabaseDiscoveryDynamicDataSourceStrategyFixture.java
new file mode 100644
index 00000000000..275df756dc7
--- /dev/null
+++
b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/fixture/DatabaseDiscoveryDynamicDataSourceStrategyFixture.java
@@ -0,0 +1,40 @@
+/*
+ * 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.shardingsphere.readwritesplitting.fixture;
+
+import
org.apache.shardingsphere.infra.datasource.strategy.DynamicDataSourceStrategy;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+
+import java.util.Collection;
+import java.util.Collections;
+
+public class DatabaseDiscoveryDynamicDataSourceStrategyFixture implements
DynamicDataSourceStrategy {
+
+ @Override
+ public void init(final ShardingSphereRule rule) { }
+
+ @Override
+ public String getPrimaryDataSourceName(final String dataSourceName) {
+ return "write";
+ }
+
+ @Override
+ public Collection<String> getReplicaDataSourceNames(final String
dataSourceName) {
+ return Collections.emptyList();
+ }
+}
diff --git
a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/route/ReadwriteSplittingSQLRouterTest.java
b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/route/ReadwriteSplittingSQLRouterTest.java
index d48c8a5c12d..97801199fbb 100644
---
a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/route/ReadwriteSplittingSQLRouterTest.java
+++
b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/java/org/apache/shardingsphere/readwritesplitting/route/ReadwriteSplittingSQLRouterTest.java
@@ -70,16 +70,23 @@ public final class ReadwriteSplittingSQLRouterTest {
private ReadwriteSplittingRule rule;
+ private ReadwriteSplittingRule dynamicRule;
+
@Mock
private CommonSQLStatementContext<SQLStatement> sqlStatementContext;
private ReadwriteSplittingSQLRouter sqlRouter;
+ private ReadwriteSplittingSQLRouter dynamicSqlRouter;
+
@Before
public void setUp() {
rule = new ReadwriteSplittingRule(new
ReadwriteSplittingRuleConfiguration(Collections.singleton(
new
ReadwriteSplittingDataSourceRuleConfiguration(DATASOURCE_NAME, "Static",
createProperties(), "")), Collections.emptyMap()));
sqlRouter = (ReadwriteSplittingSQLRouter)
SQLRouterFactory.getInstances(Collections.singleton(rule)).get(rule);
+ dynamicRule = new ReadwriteSplittingRule(new
ReadwriteSplittingRuleConfiguration(Collections.singleton(
+ new
ReadwriteSplittingDataSourceRuleConfiguration(DATASOURCE_NAME, "Dynamic",
createDynamicProperties(), "")), Collections.emptyMap()));
+ dynamicSqlRouter = (ReadwriteSplittingSQLRouter)
SQLRouterFactory.getInstances(Collections.singleton(dynamicRule)).get(dynamicRule);
}
private Properties createProperties() {
@@ -89,6 +96,12 @@ public final class ReadwriteSplittingSQLRouterTest {
return result;
}
+ private Properties createDynamicProperties() {
+ Properties result = new Properties();
+ result.setProperty("auto-aware-data-source-name", "readwrite_ds");
+ return result;
+ }
+
@Test
public void assertCreateRouteContextToPrimaryWithoutRouteUnits() {
LogicSQL logicSQL = new LogicSQL(mock(SQLStatementContext.class), "",
Collections.emptyList());
@@ -229,6 +242,34 @@ public final class ReadwriteSplittingSQLRouterTest {
assertThat(routedDataSourceNames.next(), is(WRITE_DATASOURCE));
}
+ @Test
+ public void
assertCreateRouteContextToPrimaryDataSourceWithWriteDataSourceQueryEnabled() {
+ MySQLSelectStatement selectStatement =
mock(MySQLSelectStatement.class);
+
when(sqlStatementContext.getSqlStatement()).thenReturn(selectStatement);
+ LogicSQL logicSQL = new LogicSQL(sqlStatementContext, "",
Collections.emptyList());
+ ShardingSphereRuleMetaData ruleMetaData = new
ShardingSphereRuleMetaData(Collections.singleton(dynamicRule));
+ ShardingSphereDatabase database = new
ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME,
+ mock(DatabaseType.class), mock(ShardingSphereResource.class,
RETURNS_DEEP_STUBS), ruleMetaData, Collections.emptyMap());
+ RouteContext actual = dynamicSqlRouter.createRouteContext(logicSQL,
database, dynamicRule, new ConfigurationProperties(new Properties()));
+ Iterator<String> routedDataSourceNames =
actual.getActualDataSourceNames().iterator();
+ assertThat(routedDataSourceNames.next(), is(WRITE_DATASOURCE));
+ }
+
+ @Test
+ public void
assertDecorateRouteContextToPrimaryDataSourceWithWriteDataSourceQueryEnabled() {
+ RouteContext actual = mockRouteContext();
+ MySQLSelectStatement selectStatement =
mock(MySQLSelectStatement.class);
+
when(sqlStatementContext.getSqlStatement()).thenReturn(selectStatement);
+ LogicSQL logicSQL = new LogicSQL(sqlStatementContext, "",
Collections.emptyList());
+ ShardingSphereRuleMetaData ruleMetaData = new
ShardingSphereRuleMetaData(Collections.singleton(dynamicRule));
+ ShardingSphereDatabase database = new
ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME,
+ mock(DatabaseType.class), mock(ShardingSphereResource.class,
RETURNS_DEEP_STUBS), ruleMetaData, Collections.emptyMap());
+ dynamicSqlRouter.decorateRouteContext(actual, logicSQL, database,
dynamicRule, new ConfigurationProperties(new Properties()));
+ Iterator<String> routedDataSourceNames =
actual.getActualDataSourceNames().iterator();
+ assertThat(routedDataSourceNames.next(),
is(NONE_READWRITE_SPLITTING_DATASOURCE_NAME));
+ assertThat(routedDataSourceNames.next(), is(WRITE_DATASOURCE));
+ }
+
private RouteContext mockRouteContext() {
RouteContext result = new RouteContext();
RouteUnit routeUnit = new RouteUnit(new RouteMapper(DATASOURCE_NAME,
DATASOURCE_NAME), Collections.singletonList(new RouteMapper("table",
"table_0")));
diff --git
a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.datasource.strategy.DynamicDataSourceStrategy
b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.datasource.strategy.DynamicDataSourceStrategy
new file mode 100644
index 00000000000..6f2a366ee2a
--- /dev/null
+++
b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.datasource.strategy.DynamicDataSourceStrategy
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.readwritesplitting.fixture.DatabaseDiscoveryDynamicDataSourceStrategyFixture