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

zhangyonglun 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 94f123e  Add unit test for control-panel-cluster (#6815)
94f123e is described below

commit 94f123e44efd2b6ad8204e758e325b53672596ba
Author: Haoran Meng <[email protected]>
AuthorDate: Thu Aug 13 14:50:59 2020 +0800

    Add unit test for control-panel-cluster (#6815)
---
 .../heartbeat/ClusterHeartbeatInstance.java        | 12 +++-
 .../cluster/heartbeat/detect/HeartbeatHandler.java |  9 +--
 .../heartbeat/ClusterHeartbeatInstanceTest.java    | 28 ++++++++
 .../heartbeat/detect/HeartbeatHandlerTest.java     | 21 ++++++
 .../cluster/state/ClusterStateInstance.java        |  7 +-
 .../cluster/state/ClusterStateInstanceTest.java    | 78 +++++++++++++++++++++-
 6 files changed, 141 insertions(+), 14 deletions(-)

diff --git 
a/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-heartbeat/src/main/java/org/apache/shardingsphere/cluster/heartbeat/ClusterHeartbeatInstance.java
 
b/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-heartbeat/src/main/java/org/apache/shardingsphere/cluster/heartbeat/ClusterHeartbeatInstance.java
index e8dd24b..fdb398e 100644
--- 
a/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-heartbeat/src/main/java/org/apache/shardingsphere/cluster/heartbeat/ClusterHeartbeatInstance.java
+++ 
b/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-heartbeat/src/main/java/org/apache/shardingsphere/cluster/heartbeat/ClusterHeartbeatInstance.java
@@ -26,7 +26,9 @@ import 
org.apache.shardingsphere.cluster.heartbeat.task.HeartbeatTask;
 import org.apache.shardingsphere.cluster.heartbeat.task.HeartbeatTaskManager;
 import org.apache.shardingsphere.kernel.context.SchemaContext;
 import org.apache.shardingsphere.orchestration.core.facade.OrchestrationFacade;
+import org.apache.shardingsphere.orchestration.core.registry.RegistryCenter;
 
+import java.util.Collection;
 import java.util.Map;
 
 /**
@@ -36,7 +38,9 @@ public final class ClusterHeartbeatInstance {
     
     private HeartbeatTaskManager heartbeatTaskManager;
     
-    private final HeartbeatHandler heartbeatHandler = 
HeartbeatHandler.getInstance();
+    private HeartbeatHandler heartbeatHandler = HeartbeatHandler.getInstance();
+    
+    private RegistryCenter registryCenter = 
OrchestrationFacade.getInstance().getRegistryCenter();
     
     /**
      * Get cluster heartbeat instance.
@@ -67,7 +71,11 @@ public final class ClusterHeartbeatInstance {
      * @return heartbeat response
      */
     public HeartbeatResponse detect(final Map<String, SchemaContext> 
schemaContexts) {
-        return heartbeatHandler.handle(schemaContexts, 
OrchestrationFacade.getInstance().getRegistryCenter().loadAllDataSourcesNodes());
+        return heartbeatHandler.handle(schemaContexts, 
getDisabledDataSources());
+    }
+    
+    private Collection<String> getDisabledDataSources() {
+        return registryCenter.loadDisabledDataSources();
     }
     
     /**
diff --git 
a/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-heartbeat/src/main/java/org/apache/shardingsphere/cluster/heartbeat/detect/HeartbeatHandler.java
 
b/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-heartbeat/src/main/java/org/apache/shardingsphere/cluster/heartbeat/detect/HeartbeatHandler.java
index d8e0a41..e2993ce 100644
--- 
a/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-heartbeat/src/main/java/org/apache/shardingsphere/cluster/heartbeat/detect/HeartbeatHandler.java
+++ 
b/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-heartbeat/src/main/java/org/apache/shardingsphere/cluster/heartbeat/detect/HeartbeatHandler.java
@@ -26,8 +26,6 @@ import 
org.apache.shardingsphere.cluster.configuration.config.HeartbeatConfigura
 import org.apache.shardingsphere.cluster.heartbeat.response.HeartbeatResponse;
 import org.apache.shardingsphere.cluster.heartbeat.response.HeartbeatResult;
 import org.apache.shardingsphere.kernel.context.SchemaContext;
-import org.apache.shardingsphere.orchestration.core.facade.OrchestrationFacade;
-import 
org.apache.shardingsphere.orchestration.core.registry.RegistryCenterNodeStatus;
 
 import java.util.ArrayList;
 import java.util.Collection;
@@ -120,12 +118,7 @@ public final class HeartbeatHandler {
     }
     
     private boolean isDisabled(final String schemaName, final String 
dataSourceName) {
-        return disabledDataSources.isEmpty() ? Boolean.FALSE : 
isDisabled(Joiner.on(".").join(schemaName, dataSourceName));
-    }
-    
-    private boolean isDisabled(final String schemaDataSourceName) {
-        return disabledDataSources.contains(schemaDataSourceName) && 
RegistryCenterNodeStatus.DISABLED.toString()
-                
.equals(OrchestrationFacade.getInstance().getRegistryCenter().getDataSourcesNodeData(schemaDataSourceName));
+        return disabledDataSources.isEmpty() ? Boolean.FALSE : 
disabledDataSources.contains(Joiner.on(".").join(schemaName, dataSourceName));
     }
     
     @NoArgsConstructor(access = AccessLevel.PRIVATE)
diff --git 
a/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-heartbeat/src/test/java/org/apache/shardingsphere/cluster/heartbeat/ClusterHeartbeatInstanceTest.java
 
b/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-heartbeat/src/test/java/org/apache/shardingsphere/cluster/heartbeat/ClusterHeartbeatInstanceTest.java
index 0b1d469..001a5eb 100644
--- 
a/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-heartbeat/src/test/java/org/apache/shardingsphere/cluster/heartbeat/ClusterHeartbeatInstanceTest.java
+++ 
b/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-heartbeat/src/test/java/org/apache/shardingsphere/cluster/heartbeat/ClusterHeartbeatInstanceTest.java
@@ -17,20 +17,38 @@
 
 package org.apache.shardingsphere.cluster.heartbeat;
 
+import lombok.SneakyThrows;
 import 
org.apache.shardingsphere.cluster.configuration.config.HeartbeatConfiguration;
+import org.apache.shardingsphere.cluster.heartbeat.detect.HeartbeatHandler;
+import org.apache.shardingsphere.orchestration.core.registry.RegistryCenter;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.internal.util.reflection.FieldSetter;
 import org.mockito.junit.MockitoJUnitRunner;
 
+import java.util.Arrays;
+import java.util.HashMap;
+
 import static org.junit.Assert.assertNotNull;
+import static org.mockito.ArgumentMatchers.anyCollection;
+import static org.mockito.ArgumentMatchers.anyMap;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 @RunWith(MockitoJUnitRunner.class)
 public final class ClusterHeartbeatInstanceTest {
     
     private static final ClusterHeartbeatInstance INSTANCE = 
ClusterHeartbeatInstance.getInstance();
     
+    @Mock
+    private RegistryCenter registryCenter;
+    
+    @Mock
+    private HeartbeatHandler heartbeatHandler;
+    
     @Before
     public void setUp() {
         HeartbeatConfiguration heartBeatConfig = new HeartbeatConfiguration();
@@ -46,6 +64,16 @@ public final class ClusterHeartbeatInstanceTest {
         assertNotNull(ClusterHeartbeatInstance.getInstance());
     }
     
+    @SneakyThrows(ReflectiveOperationException.class)
+    @Test
+    public void assertDetect() {
+        FieldSetter.setField(INSTANCE, 
ClusterHeartbeatInstance.class.getDeclaredField("registryCenter"), 
registryCenter);
+        FieldSetter.setField(INSTANCE, 
ClusterHeartbeatInstance.class.getDeclaredField("heartbeatHandler"), 
heartbeatHandler);
+        
when(registryCenter.loadDisabledDataSources()).thenReturn(Arrays.asList("logic_db.ds_0"));
+        INSTANCE.detect(new HashMap<>());
+        verify(heartbeatHandler).handle(anyMap(), anyCollection());
+    }
+    
     @After
     public void close() {
         INSTANCE.close();
diff --git 
a/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-heartbeat/src/test/java/org/apache/shardingsphere/cluster/heartbeat/detect/HeartbeatHandlerTest.java
 
b/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-heartbeat/src/test/java/org/apache/shardingsphere/cluster/heartbeat/detect/HeartbeatHandlerTest.java
index 99214be..961d74e 100644
--- 
a/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-heartbeat/src/test/java/org/apache/shardingsphere/cluster/heartbeat/detect/HeartbeatHandlerTest.java
+++ 
b/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-heartbeat/src/test/java/org/apache/shardingsphere/cluster/heartbeat/detect/HeartbeatHandlerTest.java
@@ -23,6 +23,7 @@ import 
org.apache.shardingsphere.cluster.heartbeat.response.HeartbeatResponse;
 import org.apache.shardingsphere.cluster.heartbeat.response.HeartbeatResult;
 import org.apache.shardingsphere.kernel.context.SchemaContext;
 import org.apache.shardingsphere.kernel.context.schema.ShardingSphereSchema;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -141,6 +142,21 @@ public final class HeartbeatHandlerTest {
         
response.getHeartbeatResultMap().get(SCHEMA_NAME).iterator().forEachRemaining(each
 -> assertTrue(each.isEnable()));
     }
     
+    @Test
+    public void assertHandleWithDisableDataSource() {
+        handler.init(getHeartbeatConfiguration(true));
+        HeartbeatResponse response = handler.handle(getSchemaContext(), 
Arrays.asList("sharding_db.ds_0"));
+        assertNotNull(response);
+        assertNotNull(response.getHeartbeatResultMap());
+        assertTrue(response.getHeartbeatResultMap().containsKey(SCHEMA_NAME));
+        assertThat(response.getHeartbeatResultMap().get(SCHEMA_NAME).size(), 
is(1));
+        HeartbeatResult heartbeatResult = 
response.getHeartbeatResultMap().get(SCHEMA_NAME).iterator().next();
+        assertNotNull(heartbeatResult);
+        assertThat(heartbeatResult.getDataSourceName(), is(DATA_SOURCE_0));
+        assertFalse(heartbeatResult.isEnable());
+        assertTrue(heartbeatResult.isDisabled());
+    }
+    
     private HeartbeatConfiguration getHeartbeatConfiguration(final boolean 
retry) {
         HeartbeatConfiguration result = new HeartbeatConfiguration();
         result.setSql(DETECT_SQL);
@@ -194,4 +210,9 @@ public final class HeartbeatHandlerTest {
         }
         return result;
     }
+    
+    @After
+    public void close() {
+        handler.close();
+    }
 }
diff --git 
a/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-state/src/main/java/org/apache/shardingsphere/cluster/state/ClusterStateInstance.java
 
b/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-state/src/main/java/org/apache/shardingsphere/cluster/state/ClusterStateInstance.java
index e4e7ba7..1ec2f38 100644
--- 
a/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-state/src/main/java/org/apache/shardingsphere/cluster/state/ClusterStateInstance.java
+++ 
b/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-state/src/main/java/org/apache/shardingsphere/cluster/state/ClusterStateInstance.java
@@ -25,6 +25,7 @@ import 
org.apache.shardingsphere.cluster.state.enums.NodeState;
 import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
 import 
org.apache.shardingsphere.orchestration.core.common.eventbus.OrchestrationEventBus;
 import org.apache.shardingsphere.orchestration.core.facade.OrchestrationFacade;
+import org.apache.shardingsphere.orchestration.core.registry.RegistryCenter;
 import 
org.apache.shardingsphere.orchestration.core.registry.event.DisabledStateChangedEvent;
 
 /**
@@ -32,6 +33,8 @@ import 
org.apache.shardingsphere.orchestration.core.registry.event.DisabledState
  */
 public final class ClusterStateInstance {
     
+    private RegistryCenter registryCenter = 
OrchestrationFacade.getInstance().getRegistryCenter();
+    
     private ClusterStateInstance() {
         OrchestrationEventBus.getInstance().register(this);
     }
@@ -52,7 +55,7 @@ public final class ClusterStateInstance {
      */
     public void persistInstanceState(final InstanceState instanceState) {
         Preconditions.checkNotNull(instanceState, "instance state can not be 
null.");
-        
OrchestrationFacade.getInstance().getRegistryCenter().persistInstanceData(YamlEngine.marshal(instanceState));
+        registryCenter.persistInstanceData(YamlEngine.marshal(instanceState));
     }
     
     /**
@@ -61,7 +64,7 @@ public final class ClusterStateInstance {
      * @return instance state
      */
     public InstanceState loadInstanceState() {
-        String instanceData = 
OrchestrationFacade.getInstance().getRegistryCenter().loadInstanceData();
+        String instanceData = registryCenter.loadInstanceData();
         Preconditions.checkState(!Strings.isNullOrEmpty(instanceData), "Can 
not load instance state from registry center");
         return YamlEngine.unmarshal(instanceData, InstanceState.class);
     }
diff --git 
a/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-state/src/test/java/org/apache/shardingsphere/cluster/state/ClusterStateInstanceTest.java
 
b/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-state/src/test/java/org/apache/shardingsphere/cluster/state/ClusterStateInstanceTest.java
index 9e7cc1b..3bd5847 100644
--- 
a/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-state/src/test/java/org/apache/shardingsphere/cluster/state/ClusterStateInstanceTest.java
+++ 
b/shardingsphere-control-panel/shardingsphere-cluster/shardingsphere-cluster-state/src/test/java/org/apache/shardingsphere/cluster/state/ClusterStateInstanceTest.java
@@ -17,14 +17,88 @@
 
 package org.apache.shardingsphere.cluster.state;
 
+import com.google.common.collect.Sets;
+import lombok.SneakyThrows;
+import org.apache.shardingsphere.cluster.state.enums.NodeState;
+import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
+import org.apache.shardingsphere.orchestration.core.registry.RegistryCenter;
+import 
org.apache.shardingsphere.orchestration.core.registry.event.DisabledStateChangedEvent;
+import 
org.apache.shardingsphere.orchestration.core.registry.schema.OrchestrationSchema;
+import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.internal.util.reflection.FieldSetter;
+import org.mockito.junit.MockitoJUnitRunner;
 
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
+@RunWith(MockitoJUnitRunner.class)
 public final class ClusterStateInstanceTest {
     
+    private static final ClusterStateInstance INSTANCE = 
ClusterStateInstance.getInstance();
+    
+    @Mock
+    private RegistryCenter registryCenter;
+    
+    @SneakyThrows(ReflectiveOperationException.class)
+    @Before
+    public void setUp() {
+        FieldSetter.setField(INSTANCE, 
ClusterStateInstance.class.getDeclaredField("registryCenter"), registryCenter);
+    }
+    
+    @Test
+    public void assertPersistInstanceState() {
+        InstanceState instanceState = new InstanceState();
+        instanceState.setState(NodeState.ONLINE);
+        instanceState.setDataSources(buildDataSources());
+        INSTANCE.persistInstanceState(instanceState);
+        
verify(registryCenter).persistInstanceData(YamlEngine.marshal(instanceState));
+    }
+    
+    private Map<String, DataSourceState> buildDataSources() {
+        DataSourceState dataSourceState = new DataSourceState();
+        dataSourceState.setState(NodeState.ONLINE);
+        dataSourceState.setLastConnect(System.currentTimeMillis());
+        dataSourceState.setRetryCount(1);
+        Map<String, DataSourceState> result = new HashMap<>();
+        result.put("sharding_db.ds_0", dataSourceState);
+        return result;
+    }
+    
+    @Test
+    public void assertLoadInstanceState() {
+        
when(registryCenter.loadInstanceData()).thenReturn(buildInstanceData());
+        InstanceState instanceState = INSTANCE.loadInstanceState();
+        assertNotNull(instanceState);
+        assertThat(instanceState.getState(), is(NodeState.ONLINE));
+        assertThat(instanceState.getDataSources().keySet(), 
is(Sets.newHashSet("sharding_db.ds_1")));
+        
assertThat(instanceState.getDataSources().get("sharding_db.ds_1").getState(), 
is(NodeState.ONLINE));
+    }
+    
+    private String buildInstanceData() {
+        return "state: ONLINE\n"
+                + "dataSources:\n"
+                + "    sharding_db.ds_1:\n"
+                + "        state: ONLINE\n"
+                + "        lastConnect: \n"
+                + "        retryCount: 1";
+    }
+    
     @Test
-    public void assertGetInstance() {
-        assertNotNull(ClusterStateInstance.getInstance());
+    public void assertDataSourceStateChanged() {
+        DisabledStateChangedEvent event = new DisabledStateChangedEvent(new 
OrchestrationSchema("sharding_db", "ds_1"), true);
+        
when(registryCenter.loadInstanceData()).thenReturn(buildInstanceData());
+        INSTANCE.dataSourceStateChanged(event);
+        InstanceState instanceState = 
YamlEngine.unmarshal(registryCenter.loadInstanceData(), InstanceState.class);
+        instanceState.getDataSources().forEach((key, value) -> 
value.setState(NodeState.DISABLED));
+        
verify(registryCenter).persistInstanceData(YamlEngine.marshal(instanceState));
     }
 }

Reply via email to