This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 0bc30b66714 Add more test cases on WeightLoadBalanceAlgorithm (#33189)
0bc30b66714 is described below
commit 0bc30b6671467a7bc969671dcef7204548b08b69
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Oct 9 19:02:57 2024 +0800
Add more test cases on WeightLoadBalanceAlgorithm (#33189)
---
.../loadbalancer/weight/WeightLoadBalanceAlgorithm.java | 3 +--
.../loadbalancer/weight/WeightLoadBalanceAlgorithmTest.java | 13 +++++++++++++
2 files changed, 14 insertions(+), 2 deletions(-)
diff --git
a/infra/algorithm/type/load-balancer/type/weight/src/main/java/org/apache/shardingsphere/infra/algorithm/loadbalancer/weight/WeightLoadBalanceAlgorithm.java
b/infra/algorithm/type/load-balancer/type/weight/src/main/java/org/apache/shardingsphere/infra/algorithm/loadbalancer/weight/WeightLoadBalanceAlgorithm.java
index 6b26f00e8b7..47f1e3701ce 100644
---
a/infra/algorithm/type/load-balancer/type/weight/src/main/java/org/apache/shardingsphere/infra/algorithm/loadbalancer/weight/WeightLoadBalanceAlgorithm.java
+++
b/infra/algorithm/type/load-balancer/type/weight/src/main/java/org/apache/shardingsphere/infra/algorithm/loadbalancer/weight/WeightLoadBalanceAlgorithm.java
@@ -48,8 +48,7 @@ public final class WeightLoadBalanceAlgorithm implements
LoadBalanceAlgorithm {
ShardingSpherePreconditions.checkNotEmpty(availableTargetNames, () ->
new AlgorithmInitializationException(this, "Available target is required."));
for (String each : availableTargetNames) {
String weight = props.getProperty(each);
- ShardingSpherePreconditions.checkNotNull(weight,
- () -> new AlgorithmInitializationException(this, "Weight
of available target `%s` is required.", each));
+ ShardingSpherePreconditions.checkNotNull(weight, () -> new
AlgorithmInitializationException(this, "Weight of available target `%s` is
required.", each));
try {
weightConfigMap.put(each, Double.parseDouble(weight));
} catch (final NumberFormatException ex) {
diff --git
a/infra/algorithm/type/load-balancer/type/weight/src/test/java/org/apache/shardingsphere/infra/algorithm/loadbalancer/weight/WeightLoadBalanceAlgorithmTest.java
b/infra/algorithm/type/load-balancer/type/weight/src/test/java/org/apache/shardingsphere/infra/algorithm/loadbalancer/weight/WeightLoadBalanceAlgorithmTest.java
index fbe73712030..c4fa3e3db9b 100644
---
a/infra/algorithm/type/load-balancer/type/weight/src/test/java/org/apache/shardingsphere/infra/algorithm/loadbalancer/weight/WeightLoadBalanceAlgorithmTest.java
+++
b/infra/algorithm/type/load-balancer/type/weight/src/test/java/org/apache/shardingsphere/infra/algorithm/loadbalancer/weight/WeightLoadBalanceAlgorithmTest.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.infra.algorithm.loadbalancer.weight;
+import
org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;
import
org.apache.shardingsphere.infra.algorithm.loadbalancer.core.LoadBalanceAlgorithm;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
@@ -30,9 +31,21 @@ import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
class WeightLoadBalanceAlgorithmTest {
+ @Test
+ void assertInitFailed() {
+ assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(LoadBalanceAlgorithm.class, "WEIGHT",
PropertiesBuilder.build(new Property("test_read_ds_1", "a"))));
+ }
+
+ @Test
+ void assertCheck() {
+ LoadBalanceAlgorithm loadBalanceAlgorithm =
TypedSPILoader.getService(LoadBalanceAlgorithm.class, "WEIGHT",
PropertiesBuilder.build(new Property("test_read_ds_1", "5")));
+ assertThrows(AlgorithmInitializationException.class, () ->
loadBalanceAlgorithm.check("foo_db",
Collections.singletonList("test_read_ds_0")));
+ }
+
@Test
void assertGetSingleAvailableTarget() {
LoadBalanceAlgorithm loadBalanceAlgorithm =
TypedSPILoader.getService(LoadBalanceAlgorithm.class, "WEIGHT",
PropertiesBuilder.build(new Property("test_read_ds_1", "5")));