You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2022/01/06 05:28:07 UTC

[shardingsphere] branch master updated: add unit test for database discovery namespace module (#14522)

This is an automated email from the ASF dual-hosted git repository.

menghaoran 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 0a12162  add unit test for database discovery namespace module (#14522)
0a12162 is described below

commit 0a12162811aad3929f476a4ad96f0279a6448316
Author: liguoping <xd...@163.com>
AuthorDate: Thu Jan 6 13:27:17 2022 +0800

    add unit test for database discovery namespace module (#14522)
    
    * add unit test for database discovery module
    
    * rename MGR
    
    * rename mgr
    
    * delete blank line
    
    * add more detail, props
    
    * zjcnb let me use mgr
    
    * zjcnb let me use mgr2
---
 .../DatabaseDiscoverySpringNamespaceTest.java      | 80 ++++++++++++++++++++++
 .../database-discovery-application-context.xml     | 41 +++++++++++
 .../src/test/resources/logback-test.xml            | 33 +++++++++
 3 files changed, 154 insertions(+)

diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-spring/shardingsphere-db-discovery-spring-namespace/src/test/java/org/apache/shardingsphere/db/discovery/spring/namespace/DatabaseDiscoverySpringNamespaceTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-spring/shardingsphere-db-discovery-spring-namespace/src/test/java/org/apache/shardingsphere/db/discovery/spring/namespace/DatabaseDiscoverySpringNamespace [...]
new file mode 100644
index 0000000..1c3a5a2
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-spring/shardingsphere-db-discovery-spring-namespace/src/test/java/org/apache/shardingsphere/db/discovery/spring/namespace/DatabaseDiscoverySpringNamespaceTest.java
@@ -0,0 +1,80 @@
+/*
+ * 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.db.discovery.spring.namespace;
+
+import org.apache.shardingsphere.dbdiscovery.algorithm.config.AlgorithmProvidedDatabaseDiscoveryRuleConfiguration;
+import org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryDataSourceRuleConfiguration;
+import org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryHeartBeatConfiguration;
+import org.apache.shardingsphere.dbdiscovery.mgr.MGRDatabaseDiscoveryType;
+import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryType;
+import org.junit.Test;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
+
+import javax.annotation.Resource;
+import java.util.Arrays;
+import java.util.Map;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+
+@ContextConfiguration(locations = "classpath:META-INF/spring/database-discovery-application-context.xml")
+public final class DatabaseDiscoverySpringNamespaceTest extends AbstractJUnit4SpringContextTests {
+    
+    @Resource
+    private DatabaseDiscoveryType mgrDatabaseDiscoveryType;
+    
+    @Resource
+    private AlgorithmProvidedDatabaseDiscoveryRuleConfiguration mgrDatabaseDiscoveryRule;
+    
+    @Test
+    public void assertMGRDatabaseDiscoveryType() {
+        assertThat(mgrDatabaseDiscoveryType.getType(), is("MGR"));
+    }
+    
+    @Test
+    public void assertDefaultDataSource() {
+        assertDiscoveryTypes(mgrDatabaseDiscoveryRule.getDiscoveryTypes());
+        assertHeartbeats(mgrDatabaseDiscoveryRule.getDiscoveryHeartbeats());
+        assertThat(mgrDatabaseDiscoveryRule.getDataSources().size(), is(1));
+        assertDefaultDataSourceRule(mgrDatabaseDiscoveryRule.getDataSources().iterator().next());
+    }
+    
+    private void assertDiscoveryTypes(final Map<String, DatabaseDiscoveryType> discoveryTypes) {
+        assertThat(discoveryTypes.size(), is(1));
+        assertThat(discoveryTypes.get("mgr"), instanceOf(MGRDatabaseDiscoveryType.class));
+        assertNotNull(discoveryTypes.get("mgr").getProps());
+        assertThat(discoveryTypes.get("mgr").getProps().get("group-name"), is("92504d5b-6dec-11e8-91ea-246e9612aaf1"));
+    }
+    
+    private void assertHeartbeats(final Map<String, DatabaseDiscoveryHeartBeatConfiguration> heartbeats) {
+        assertThat(heartbeats.size(), is(1));
+        assertThat(heartbeats.get("mgr-heartbeat"), instanceOf(DatabaseDiscoveryHeartBeatConfiguration.class));
+        assertNotNull(heartbeats.get("mgr-heartbeat").getProps());
+        assertThat(heartbeats.get("mgr-heartbeat").getProps().get("keep-alive-cron"), is("0/5 * * * * ?"));
+    }
+    
+    private void assertDefaultDataSourceRule(final DatabaseDiscoveryDataSourceRuleConfiguration dataSourceRuleConfig) {
+        assertThat(dataSourceRuleConfig.getDataSourceNames(), is(Arrays.asList("ds_0", "ds_1", "ds_2")));
+        assertThat(dataSourceRuleConfig.getDiscoveryHeartbeatName(), is("mgr-heartbeat"));
+        assertThat(dataSourceRuleConfig.getDiscoveryTypeName(), is("mgr"));
+        assertThat(dataSourceRuleConfig.getGroupName(), is("pr_ds"));
+    }
+}
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-spring/shardingsphere-db-discovery-spring-namespace/src/test/resources/META-INF/spring/database-discovery-application-context.xml b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-spring/shardingsphere-db-discovery-spring-namespace/src/test/resources/META-INF/spring/database-discovery-application-context.xml
new file mode 100644
index 0000000..6251eb8
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-spring/shardingsphere-db-discovery-spring-namespace/src/test/resources/META-INF/spring/database-discovery-application-context.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns:database-discovery="http://shardingsphere.apache.org/schema/shardingsphere/database-discovery"
+       xsi:schemaLocation="http://www.springframework.org/schema/beans
+                           http://www.springframework.org/schema/beans/spring-beans.xsd 
+                           http://shardingsphere.apache.org/schema/shardingsphere/database-discovery
+                           http://shardingsphere.apache.org/schema/shardingsphere/database-discovery/database-discovery.xsd
+                           ">
+    <database-discovery:discovery-type id="mgr" type="MGR" >
+        <props>
+            <prop key="group-name">92504d5b-6dec-11e8-91ea-246e9612aaf1</prop>
+        </props>
+    </database-discovery:discovery-type>
+    
+    <database-discovery:rule id="mgrDatabaseDiscoveryRule">
+        <database-discovery:data-source-rule id="pr_ds" data-source-names="ds_0,ds_1,ds_2" discovery-heartbeat-name="mgr-heartbeat" discovery-type-name="mgr"/>
+        <database-discovery:discovery-heartbeat id="mgr-heartbeat">
+            <props>
+                <prop key="keep-alive-cron" > 0/5 * * * * ? </prop>
+            </props>
+        </database-discovery:discovery-heartbeat>
+    </database-discovery:rule>
+</beans>
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-spring/shardingsphere-db-discovery-spring-namespace/src/test/resources/logback-test.xml b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-spring/shardingsphere-db-discovery-spring-namespace/src/test/resources/logback-test.xml
new file mode 100644
index 0000000..2de4931
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-spring/shardingsphere-db-discovery-spring-namespace/src/test/resources/logback-test.xml
@@ -0,0 +1,33 @@
+<?xml version="1.0"?>
+<!--
+  ~ 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.
+  -->
+
+<configuration>
+    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
+        <encoder>
+            <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern>
+        </encoder>
+    </appender>
+    <logger name="org.apache.shardingsphere" level="warn" additivity="false">
+        <appender-ref ref="console" />
+    </logger>
+    
+    <root>
+        <level value="warn" />
+        <appender-ref ref="console" />
+    </root>
+</configuration>