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

wuweijie 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 520335b  add more unit test for traffic (#14682)
520335b is described below

commit 520335bde560767f37f8929e4b67634a6e49ded6
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Tue Jan 11 16:59:33 2022 +0800

    add more unit test for traffic (#14682)
---
 .../shardingsphere/traffic/rule/TrafficRule.java   |  10 +-
 .../TrafficContextTest.java}                       |  33 +++---
 .../algorithm/engine/TrafficEngineTest.java        | 113 +++++++++++++++++++++
 .../RandomTrafficLoadBalanceAlgorithmTest.java     |  13 +++
 .../RoundRobinTrafficLoadBalanceAlgorithmTest.java |  11 ++
 .../{ => hint}/SQLHintTrafficAlgorithmTest.java    |  10 +-
 .../SQLMatchTrafficAlgorithmTest.java              |  10 +-
 7 files changed, 172 insertions(+), 28 deletions(-)

diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficRule.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficRule.java
index 2c6e3ad..ddc3c1e 100644
--- 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficRule.java
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficRule.java
@@ -61,11 +61,6 @@ public final class TrafficRule implements GlobalRule {
         config.getLoadBalancers().forEach((key, value) -> 
loadBalancers.put(key, ShardingSphereAlgorithmFactory.createAlgorithm(value, 
TrafficLoadBalanceAlgorithm.class)));
     }
     
-    @Override
-    public String getType() {
-        return TrafficRule.class.getSimpleName();
-    }
-    
     /**
      * Find matched strategy rule.
      * 
@@ -136,4 +131,9 @@ public final class TrafficRule implements GlobalRule {
         }
         return result;
     }
+    
+    @Override
+    public String getType() {
+        return TrafficRule.class.getSimpleName();
+    }
 }
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/SQLHintTrafficAlgorithmTest.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/context/TrafficContextTest.java
similarity index 50%
copy from 
shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/SQLHintTrafficAlgorithmTest.java
copy to 
shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/context/TrafficContextTest.java
index 089b108..b28def7 100644
--- 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/SQLHintTrafficAlgorithmTest.java
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/context/TrafficContextTest.java
@@ -15,35 +15,30 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.traffic.algorithm.traffic;
+package org.apache.shardingsphere.traffic.algorithm.context;
 
-import 
org.apache.shardingsphere.traffic.algorithm.traffic.hint.SQLHintTrafficAlgorithm;
-import org.apache.shardingsphere.traffic.api.traffic.hint.HintTrafficValue;
-import org.junit.Before;
+import org.apache.shardingsphere.infra.executor.sql.context.ExecutionUnit;
+import org.apache.shardingsphere.infra.executor.sql.context.SQLUnit;
+import org.apache.shardingsphere.traffic.context.TrafficContext;
 import org.junit.Test;
 
+import java.util.Collections;
+
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
-public final class SQLHintTrafficAlgorithmTest {
-    
-    private SQLHintTrafficAlgorithm sqlHintAlgorithm;
-    
-    @Before
-    public void setUp() {
-        sqlHintAlgorithm = new SQLHintTrafficAlgorithm();
-        sqlHintAlgorithm.getProps().put("foo", "bar");
-        sqlHintAlgorithm.getProps().put("test", "234");
-        sqlHintAlgorithm.init();
-    }
+public final class TrafficContextTest {
     
     @Test
-    public void assertMatchWhenSQLHintAllMatch() {
-        assertTrue(sqlHintAlgorithm.match(new HintTrafficValue<>("/* 
ShardingSphere hint: foo=bar , test=234 */")));
+    public void assertIsMatchTrafficWhenExistExecutionUnit() {
+        TrafficContext trafficContext = new TrafficContext();
+        trafficContext.getExecutionUnits().add(new 
ExecutionUnit("127.0.0.1@3307", new SQLUnit("SELECT * FROM t_order", 
Collections.emptyList())));
+        assertTrue(trafficContext.isMatchTraffic());
     }
     
     @Test
-    public void assertMatchWhenSQLHintOneMatch() {
-        assertFalse(sqlHintAlgorithm.match(new HintTrafficValue<>("/* 
ShardingSphere hint: foo=bar */")));
+    public void assertIsMatchTrafficWhenNotExistExecutionUnit() {
+        TrafficContext trafficContext = new TrafficContext();
+        assertFalse(trafficContext.isMatchTraffic());
     }
 }
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/engine/TrafficEngineTest.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/engine/TrafficEngineTest.java
new file mode 100644
index 0000000..8309aa3
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/engine/TrafficEngineTest.java
@@ -0,0 +1,113 @@
+/*
+ * 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.traffic.algorithm.engine;
+
+import org.apache.shardingsphere.infra.binder.LogicSQL;
+import org.apache.shardingsphere.infra.instance.ComputeNodeInstance;
+import org.apache.shardingsphere.infra.instance.InstanceDefinition;
+import org.apache.shardingsphere.infra.instance.InstanceType;
+import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
+import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
+import org.apache.shardingsphere.traffic.context.TrafficContext;
+import org.apache.shardingsphere.traffic.engine.TrafficEngine;
+import org.apache.shardingsphere.traffic.rule.TrafficRule;
+import org.apache.shardingsphere.traffic.rule.TrafficStrategyRule;
+import org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class TrafficEngineTest {
+    
+    @Mock
+    private TrafficRule trafficRule;
+    
+    @Mock
+    private MetaDataContexts metaDataContexts;
+    
+    @Mock
+    private TrafficStrategyRule strategyRule;
+    
+    @Mock
+    private LogicSQL logicSQL;
+    
+    @Mock
+    private MetaDataPersistService metaDataPersistService;
+    
+    @Test
+    public void assertDispatchWhenNotExistTrafficStrategyRule() {
+        TrafficEngine trafficEngine = new TrafficEngine(trafficRule, 
metaDataContexts);
+        
when(trafficRule.findMatchedStrategyRule(logicSQL)).thenReturn(Optional.empty());
+        TrafficContext actual = trafficEngine.dispatch(logicSQL);
+        assertThat(actual.getExecutionUnits().size(), is(0));
+    }
+    
+    @Test
+    public void 
assertDispatchWhenExistTrafficStrategyRuleNotExistComputeNodeInstances() {
+        TrafficEngine trafficEngine = new TrafficEngine(trafficRule, 
metaDataContexts);
+        
when(trafficRule.findMatchedStrategyRule(logicSQL)).thenReturn(Optional.of(strategyRule));
+        when(strategyRule.getLabels()).thenReturn(Arrays.asList("OLTP", 
"OLAP"));
+        
when(metaDataContexts.getMetaDataPersistService()).thenReturn(Optional.of(metaDataPersistService));
+        
when(metaDataPersistService.loadComputeNodeInstances(InstanceType.PROXY, 
Arrays.asList("OLTP", "OLAP"))).thenReturn(Collections.emptyList());
+        TrafficContext actual = trafficEngine.dispatch(logicSQL);
+        assertThat(actual.getExecutionUnits().size(), is(0));
+    }
+    
+    @Test
+    public void 
assertDispatchWhenExistTrafficStrategyRuleExistComputeNodeInstances() {
+        TrafficEngine trafficEngine = new TrafficEngine(trafficRule, 
metaDataContexts);
+        
when(trafficRule.findMatchedStrategyRule(logicSQL)).thenReturn(Optional.of(strategyRule));
+        when(strategyRule.getLabels()).thenReturn(Arrays.asList("OLTP", 
"OLAP"));
+        when(strategyRule.getLoadBalancerName()).thenReturn("RANDOM");
+        when(strategyRule.getName()).thenReturn("traffic");
+        
when(metaDataContexts.getMetaDataPersistService()).thenReturn(Optional.of(metaDataPersistService));
+        
when(metaDataPersistService.loadComputeNodeInstances(InstanceType.PROXY, 
Arrays.asList("OLTP", "OLAP"))).thenReturn(mockComputeNodeInstances());
+        TrafficLoadBalanceAlgorithm algorithm = 
mock(TrafficLoadBalanceAlgorithm.class);
+        when(algorithm.getInstanceId("traffic", 
Arrays.asList("127.0.0.1@3307", 
"127.0.0.1@3308"))).thenReturn("127.0.0.1@3307");
+        when(trafficRule.findLoadBalancer("RANDOM")).thenReturn(algorithm);
+        TrafficContext actual = trafficEngine.dispatch(logicSQL);
+        assertThat(actual.getExecutionUnits().size(), is(1));
+        
assertThat(actual.getExecutionUnits().iterator().next().getDataSourceName(), 
is("127.0.0.1@3307"));
+    }
+    
+    private Collection<ComputeNodeInstance> mockComputeNodeInstances() {
+        Collection<ComputeNodeInstance> result = new LinkedList<>();
+        ComputeNodeInstance instanceOlap = new ComputeNodeInstance();
+        instanceOlap.setLabels(Collections.singletonList("OLAP"));
+        instanceOlap.setInstanceDefinition(new 
InstanceDefinition(InstanceType.PROXY, "127.0.0.1@3307"));
+        result.add(instanceOlap);
+        ComputeNodeInstance instanceOltp = new ComputeNodeInstance();
+        instanceOltp.setLabels(Collections.singletonList("OLTP"));
+        instanceOltp.setInstanceDefinition(new 
InstanceDefinition(InstanceType.PROXY, "127.0.0.1@3308"));
+        result.add(instanceOltp);
+        return result;
+    }
+}
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RandomTrafficLoadBalanceAlgorithmTest.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RandomTrafficLoadBalanceAlgorithmTest.java
index fcf37f3..b554177 100644
--- 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RandomTrafficLoadBalanceAlgorithmTest.java
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RandomTrafficLoadBalanceAlgorithmTest.java
@@ -22,6 +22,9 @@ import org.junit.Test;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 
 public final class RandomTrafficLoadBalanceAlgorithmTest {
@@ -35,4 +38,14 @@ public final class RandomTrafficLoadBalanceAlgorithmTest {
         
assertTrue(instanceIds.contains(randomAlgorithm.getInstanceId("simple_traffic", 
instanceIds)));
         
assertTrue(instanceIds.contains(randomAlgorithm.getInstanceId("simple_traffic", 
instanceIds)));
     }
+    
+    @Test
+    public void assertGetType() {
+        assertThat(randomAlgorithm.getType(), is("RANDOM"));
+    }
+    
+    @Test
+    public void assertIsDefault() {
+        assertFalse(randomAlgorithm.isDefault());
+    }
 }
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RoundRobinTrafficLoadBalanceAlgorithmTest.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RoundRobinTrafficLoadBalanceAlgorithmTest.java
index b075e69..eba8e99 100644
--- 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RoundRobinTrafficLoadBalanceAlgorithmTest.java
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RoundRobinTrafficLoadBalanceAlgorithmTest.java
@@ -28,6 +28,7 @@ import java.util.concurrent.ConcurrentHashMap;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
 
 public final class RoundRobinTrafficLoadBalanceAlgorithmTest {
     
@@ -50,4 +51,14 @@ public final class RoundRobinTrafficLoadBalanceAlgorithmTest 
{
         assertThat(roundRobinAlgorithm.getInstanceId("simple_traffic", 
instanceIds), is(instanceId2));
         assertThat(roundRobinAlgorithm.getInstanceId("simple_traffic", 
instanceIds), is(instanceId1));
     }
+    
+    @Test
+    public void assertGetType() {
+        assertThat(roundRobinAlgorithm.getType(), is("ROUND_ROBIN"));
+    }
+    
+    @Test
+    public void assertIsDefault() {
+        assertTrue(roundRobinAlgorithm.isDefault());
+    }
 }
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/SQLHintTrafficAlgorithmTest.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/hint/SQLHintTrafficAlgorithmTest.java
similarity index 86%
rename from 
shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/SQLHintTrafficAlgorithmTest.java
rename to 
shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/hint/SQLHintTrafficAlgorithmTest.java
index 089b108..26a6f57 100644
--- 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/SQLHintTrafficAlgorithmTest.java
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/hint/SQLHintTrafficAlgorithmTest.java
@@ -15,14 +15,15 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.traffic.algorithm.traffic;
+package org.apache.shardingsphere.traffic.algorithm.traffic.hint;
 
-import 
org.apache.shardingsphere.traffic.algorithm.traffic.hint.SQLHintTrafficAlgorithm;
 import org.apache.shardingsphere.traffic.api.traffic.hint.HintTrafficValue;
 import org.junit.Before;
 import org.junit.Test;
 
+import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 
 public final class SQLHintTrafficAlgorithmTest {
@@ -46,4 +47,9 @@ public final class SQLHintTrafficAlgorithmTest {
     public void assertMatchWhenSQLHintOneMatch() {
         assertFalse(sqlHintAlgorithm.match(new HintTrafficValue<>("/* 
ShardingSphere hint: foo=bar */")));
     }
+    
+    @Test
+    public void assertGetType() {
+        assertThat(sqlHintAlgorithm.getType(), is("SQL_HINT"));
+    }
 }
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/SQLMatchTrafficAlgorithmTest.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLMatchTrafficAlgorithmTest.java
similarity index 91%
rename from 
shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/SQLMatchTrafficAlgorithmTest.java
rename to 
shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLMatchTrafficAlgorithmTest.java
index 6d48f51..8fa1042 100644
--- 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/SQLMatchTrafficAlgorithmTest.java
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLMatchTrafficAlgorithmTest.java
@@ -15,16 +15,17 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.traffic.algorithm.traffic;
+package org.apache.shardingsphere.traffic.algorithm.traffic.segment;
 
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 import 
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
-import 
org.apache.shardingsphere.traffic.algorithm.traffic.segment.SQLMatchTrafficAlgorithm;
 import 
org.apache.shardingsphere.traffic.api.traffic.segment.SegmentTrafficValue;
 import org.junit.Before;
 import org.junit.Test;
 
+import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
 
@@ -57,4 +58,9 @@ public final class SQLMatchTrafficAlgorithmTest {
         assertFalse(sqlMatchAlgorithm.match(new 
SegmentTrafficValue(sqlStatement, "UPDATE `t_order` SET `order_id` = ?;")));
         assertFalse(sqlMatchAlgorithm.match(new 
SegmentTrafficValue(sqlStatement, "UPDATE `t_order_item` SET `order_id` = ? 
WHERE user_id = ?;")));
     }
+    
+    @Test
+    public void assertGetType() {
+        assertThat(sqlMatchAlgorithm.getType(), is("SQL_MATCH"));
+    }
 }

Reply via email to