You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2021/11/23 00:36:00 UTC

[shardingsphere] branch master updated: Update MGRDatabaseDiscoveryTypeTest#13534 (#13727)

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

panjuan 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 d421d8b  Update MGRDatabaseDiscoveryTypeTest#13534 (#13727)
d421d8b is described below

commit d421d8bfbd314386166f970b86e939d548f6dea3
Author: Simple <36...@users.noreply.github.com>
AuthorDate: Tue Nov 23 08:34:58 2021 +0800

    Update MGRDatabaseDiscoveryTypeTest#13534 (#13727)
    
    * add unit test for MGRDatabaseDiscoveryType.
    
    * add unit test for MGRDatabaseDiscoveryType.
    
    * add unit test for MGRDatabaseDiscoveryType
    
    * add unit test for MGRDatabaseDiscoveryType
    
    * delete assert not
    
    * change assert type
    
    * Resolve code conflicts
    
    Co-authored-by: cuishifeng <cu...@163.com>
---
 .../shardingsphere-db-discovery-mgr/pom.xml        |  5 ++
 .../mgr/MGRDatabaseDiscoveryTypeTest.java          | 80 +++++++++++++++++++++-
 2 files changed, 83 insertions(+), 2 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/pom.xml b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/pom.xml
index d6700c2..6a79c03 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/pom.xml
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/pom.xml
@@ -59,5 +59,10 @@
             <groupId>com.zaxxer</groupId>
             <artifactId>HikariCP</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.curator</groupId>
+            <artifactId>curator-test</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/test/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRDatabaseDiscoveryTypeTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/test/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRDatabaseDiscoveryTypeTest.java
index e6105b4..8f0c86a 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/test/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRDatabaseDiscoveryTypeTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mgr/src/test/java/org/apache/shardingsphere/dbdiscovery/mgr/MGRDatabaseDiscoveryTypeTest.java
@@ -18,14 +18,27 @@
 package org.apache.shardingsphere.dbdiscovery.mgr;
 
 import com.google.common.eventbus.EventBus;
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.curator.framework.CuratorFrameworkFactory;
+import org.apache.curator.retry.ExponentialBackoffRetry;
+import org.apache.curator.test.TestingServer;
+import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.ScheduleJobBootstrap;
+import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
+import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperConfiguration;
+import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
 import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
 import org.apache.shardingsphere.infra.exception.ShardingSphereException;
 import org.apache.shardingsphere.infra.rule.event.impl.DataSourceDisabledEvent;
+import org.apache.zookeeper.CreateMode;
+import org.hamcrest.Matchers;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.mockito.Mockito;
 
 import javax.sql.DataSource;
 import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
 import java.sql.Connection;
 import java.sql.DatabaseMetaData;
 import java.sql.ResultSet;
@@ -34,19 +47,26 @@ import java.sql.Statement;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.Map;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Map;
+import java.util.Properties;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.mockStatic;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 public final class MGRDatabaseDiscoveryTypeTest {
     
+    private static TestingServer server;
+    
+    private static CuratorFramework client;
+    
     private static final String PLUGIN_STATUS = "SELECT * FROM information_schema.PLUGINS WHERE PLUGIN_NAME='group_replication'";
     
     private static final String MEMBER_COUNT = "SELECT count(*) FROM performance_schema.replication_group_members";
@@ -57,6 +77,21 @@ public final class MGRDatabaseDiscoveryTypeTest {
     
     private final MGRDatabaseDiscoveryType mgrHaType = new MGRDatabaseDiscoveryType();
     
+    @BeforeClass
+    public static void before() throws Exception {
+        server = new TestingServer(2181, true);
+        server.start();
+        client = CuratorFrameworkFactory.newClient("127.0.0.1",
+                new ExponentialBackoffRetry(1000, 3));
+        client.start();
+    }
+    
+    @AfterClass
+    public static void after() throws Exception {
+        server.stop();
+        client.close();
+    }
+    
     @Test
     public void checkHAConfig() {
         DataSource dataSource = mock(DataSource.class);
@@ -173,4 +208,45 @@ public final class MGRDatabaseDiscoveryTypeTest {
         mgrHaType.updateMemberState("discovery_db", dataSourceMap, disabledDataSourceNames);
         verify(eventBus).post(Mockito.refEq(new DataSourceDisabledEvent("discovery_db", "ds_2", true)));
     }
+    
+    @Test
+    public void startPeriodicalUpdate() throws NoSuchFieldException, IllegalAccessException {
+        Properties props = mock(Properties.class);
+        when(props.getProperty("zkServerLists")).thenReturn("127.0.0.1:2181");
+        when(props.getProperty("keepAliveCron")).thenReturn("0/5 * * * * ?");
+        Field propsFiled = MGRDatabaseDiscoveryType.class.getDeclaredField("props");
+        propsFiled.setAccessible(true);
+        propsFiled.set(mgrHaType, props);
+        final Map<String, ScheduleJobBootstrap> scheduleJobHashMap = spy(HashMap.class);
+        Field scheduleJobBootstrapMapFiled = MGRDatabaseDiscoveryType.class.getDeclaredField("SCHEDULE_JOB_BOOTSTRAP_MAP");
+        scheduleJobBootstrapMapFiled.setAccessible(true);
+        Field modifiersField = Field.class.getDeclaredField("modifiers");
+        modifiersField.setAccessible(true);
+        modifiersField.setInt(scheduleJobBootstrapMapFiled, scheduleJobBootstrapMapFiled.getModifiers() & ~Modifier.FINAL);
+        scheduleJobBootstrapMapFiled.set(mgrHaType, scheduleJobHashMap);
+        Map<String, DataSource> originalDataSourceMap = new HashMap<>(3, 1);
+        mgrHaType.startPeriodicalUpdate("discovery_db", originalDataSourceMap, null, "group_name");
+        verify(scheduleJobHashMap, times(2)).get("group_name");
+        assertThat(scheduleJobHashMap.get("group_name").getClass(), Matchers.equalTo(ScheduleJobBootstrap.class));
+        scheduleJobHashMap.get("group_name").shutdown();
+    }
+    
+    @Test
+    public void updateProperties() throws Exception {
+        Properties props = mock(Properties.class);
+        when(props.getProperty("zkServerLists")).thenReturn("127.0.0.1:2181");
+        when(props.getProperty("keepAliveCron")).thenReturn("0/5 * * * * ?");
+        ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(props.getProperty("zkServerLists"), "");
+        CoordinatorRegistryCenter coordinatorRegistryCenter = new ZookeeperRegistryCenter(zkConfig);
+        coordinatorRegistryCenter.init();
+        Field propsFiled = MGRDatabaseDiscoveryType.class.getDeclaredField("coordinatorRegistryCenter");
+        propsFiled.setAccessible(true);
+        propsFiled.set(mgrHaType, coordinatorRegistryCenter);
+        ((CuratorFramework) coordinatorRegistryCenter.getRawClient()).create().withMode(CreateMode.PERSISTENT).forPath("/MGR-group_name", "123".getBytes("utf-8"));
+        ((CuratorFramework) coordinatorRegistryCenter.getRawClient()).create().withMode(CreateMode.PERSISTENT).forPath("/MGR-group_name/config", "123".getBytes("utf-8"));
+        mgrHaType.updateProperties("group_name", props);
+        assertThat(coordinatorRegistryCenter.get("/MGR-group_name/config"), is("cron: 0/5 * * * * ?\n" + "disabled: false\n"
+                + "failover: false\n" + "jobName: MGR-group_name\n" + "maxTimeDiffSeconds: -1\n" + "misfire: false\n"
+                + "monitorExecution: false\n" + "overwrite: false\n" + "reconcileIntervalMinutes: 0\n" + "shardingTotalCount: 1\n" + "staticSharding: false\n"));
+    }
 }