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 178ca31  Added tests for ClusterContextManagerCoordinator (#15216)
178ca31 is described below

commit 178ca31f34cc780c40079b1bd3c369a34d36a9c3
Author: Shankeerthan Kasilingam 
<[email protected]>
AuthorDate: Sat Feb 5 12:54:54 2022 +0530

    Added tests for ClusterContextManagerCoordinator (#15216)
    
    * Added tests for the following methods(Git issue : 15185)
    *renew(final PrimaryStateChangedEvent event)
    *renew(final StateEvent event)
    *renew(final WorkerIdEvent event)
    *renew(final LabelsEvent event)
    
    * Changed the code as per the comments(Git issue : 15185)
    
    * Changed the code as per the suggestion(Git issue : 15185)
---
 .../ClusterContextManagerCoordinatorTest.java      | 52 +++++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)

diff --git 
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinatorTest.java
 
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinatorTest.java
index f69a236..158548c 100644
--- 
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinatorTest.java
+++ 
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinatorTest.java
@@ -40,6 +40,8 @@ import 
org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
 import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
 import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import 
org.apache.shardingsphere.infra.rule.identifier.type.StatusContainedRule;
+import org.apache.shardingsphere.infra.state.StateType;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter;
 import 
org.apache.shardingsphere.mode.manager.cluster.ClusterContextManagerBuilder;
@@ -51,7 +53,11 @@ import 
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.confi
 import 
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.schema.SchemaChangedEvent;
 import 
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.metadata.event.SchemaAddedEvent;
 import 
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.metadata.event.SchemaDeletedEvent;
+import 
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.LabelsEvent;
+import 
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.StateEvent;
+import 
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.WorkerIdEvent;
 import 
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.storage.event.DisabledStateChangedEvent;
+import 
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.storage.event.PrimaryStateChangedEvent;
 import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
 import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
 import 
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration;
@@ -82,6 +88,7 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
@@ -112,7 +119,7 @@ public final class ClusterContextManagerCoordinatorTest {
         contextManager = 
builder.build(ContextManagerBuilderParameter.builder().modeConfig(modeConfig).schemaConfigs(new
 HashMap<>())
                 .globalRuleConfigs(new LinkedList<>()).props(new 
Properties()).instanceDefinition(new InstanceDefinition(InstanceType.PROXY, 
3307)).build());
         
assertTrue(contextManager.getMetaDataContexts().getMetaDataPersistService().isPresent());
-        contextManager.renewMetaDataContexts(new 
MetaDataContexts(contextManager.getMetaDataContexts().getMetaDataPersistService().get(),
 createMetaDataMap(), globalRuleMetaData, 
+        contextManager.renewMetaDataContexts(new 
MetaDataContexts(contextManager.getMetaDataContexts().getMetaDataPersistService().get(),
 createMetaDataMap(), globalRuleMetaData,
                 mock(ExecutorEngine.class), createOptimizerContext(), new 
ConfigurationProperties(new Properties())));
         
contextManager.renewTransactionContexts(mock(TransactionContexts.class, 
RETURNS_DEEP_STUBS));
         coordinator = new 
ClusterContextManagerCoordinator(metaDataPersistService, contextManager, 
mock(RegistryCenter.class));
@@ -250,4 +257,47 @@ public final class ClusterContextManagerCoordinatorTest {
         when(result.getFederationMetaData().getSchemas()).thenReturn(schemas);
         return result;
     }
+    
+    @Test
+    public void assertRenewPrimaryDataSourceName() {
+        Collection<ShardingSphereRule> rules = new LinkedList<>();
+        StatusContainedRule mockStatusContainedRule = 
mock(StatusContainedRule.class);
+        rules.add(mockStatusContainedRule);
+        ShardingSphereRuleMetaData mockShardingSphereRuleMetaData = new 
ShardingSphereRuleMetaData(new LinkedList<>(), rules);
+        ShardingSphereMetaData mockShardingSphereMetaData = 
mock(ShardingSphereMetaData.class);
+        
when(mockShardingSphereMetaData.getRuleMetaData()).thenReturn(mockShardingSphereRuleMetaData);
+        contextManager.getMetaDataContexts().getMetaDataMap().put("schema", 
mockShardingSphereMetaData);
+        PrimaryStateChangedEvent mockPrimaryStateChangedEvent = new 
PrimaryStateChangedEvent(new QualifiedSchema("schema.test_ds"), "test_ds");
+        coordinator.renew(mockPrimaryStateChangedEvent);
+        verify(mockStatusContainedRule).updateStatus(any());
+    }
+    
+    @Test
+    public void assertRenewInstanceStatus() {
+        Collection<String> testStates = new LinkedList<>();
+        testStates.add(StateType.OK.name());
+        testStates.add(StateType.LOCK.name());
+        StateEvent mockStateEvent = new 
StateEvent(contextManager.getInstanceContext().getInstance().getInstanceDefinition().getInstanceId().getId(),
 testStates);
+        coordinator.renew(mockStateEvent);
+        
assertThat(contextManager.getInstanceContext().getInstance().getStatus(), 
is(testStates));
+        testStates.add(StateType.CIRCUIT_BREAK.name());
+        coordinator.renew(mockStateEvent);
+        
assertThat(contextManager.getInstanceContext().getState().getCurrentState(), 
is(StateType.CIRCUIT_BREAK));
+    }
+    
+    @Test
+    public void assertRenewWorkerIdChange() {
+        WorkerIdEvent mockWorkerIdEvent = new 
WorkerIdEvent(contextManager.getInstanceContext().getInstance().getInstanceDefinition().getInstanceId().getId(),
 12223L);
+        coordinator.renew(mockWorkerIdEvent);
+        assertThat(contextManager.getInstanceContext().getWorkerId(), 
is(12223L));
+    }
+    
+    @Test
+    public void assertRenewInstanceLabels() {
+        Collection<String> labels = new LinkedList<>();
+        labels.add("test");
+        LabelsEvent mockLabelsEvent = new 
LabelsEvent(contextManager.getInstanceContext().getInstance().getInstanceDefinition().getInstanceId().getId(),
 labels);
+        coordinator.renew(mockLabelsEvent);
+        
assertThat(contextManager.getInstanceContext().getInstance().getLabels(), 
is(labels));
+    }
 } 

Reply via email to