This is an automated email from the ASF dual-hosted git repository. zhangzicheng pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push: new 717cbe9f73 [test] add RoundRobinLoadBalancer tests for selection logic and distribution (#6093) 717cbe9f73 is described below commit 717cbe9f732a82a283c3da9e8c27b8985738f738 Author: Jast <745925...@qq.com> AuthorDate: Wed Aug 13 11:27:26 2025 +0800 [test] add RoundRobinLoadBalancer tests for selection logic and distribution (#6093) * test: enhance RoundRobinLoadBalancer tests for selection logic and distribution * test(loadbalancer): add constant for round robin selection iterations - Introduce SELECTION_ITERATIONS constant to replace magic number 30 - Update test to use the new constant for better readability and maintainability --------- Co-authored-by: aias00 <liuhon...@apache.org> --- .../spi/RoundRobinLoadBalanceTest.java | 31 +++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/shenyu-loadbalancer/src/test/java/org/apache/shenyu/loadbalancer/spi/RoundRobinLoadBalanceTest.java b/shenyu-loadbalancer/src/test/java/org/apache/shenyu/loadbalancer/spi/RoundRobinLoadBalanceTest.java index 01af4dd617..975b9397ac 100644 --- a/shenyu-loadbalancer/src/test/java/org/apache/shenyu/loadbalancer/spi/RoundRobinLoadBalanceTest.java +++ b/shenyu-loadbalancer/src/test/java/org/apache/shenyu/loadbalancer/spi/RoundRobinLoadBalanceTest.java @@ -28,12 +28,16 @@ import java.util.stream.IntStream; import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * The type Load balance test. */ public final class RoundRobinLoadBalanceTest { + private static final int SELECTION_ITERATIONS = 30; + /** * Round robin load balance test. */ @@ -115,7 +119,28 @@ public final class RoundRobinLoadBalanceTest { .collect(Collectors.toList()); RoundRobinLoadBalancer roundRobinLoadBalancer = new RoundRobinLoadBalancer(); - roundRobinLoadBalancer.select(upstreamList, ""); - roundRobinLoadBalancer.select(upstreamList2, ""); + + // Test with weighted upstream list + Upstream result1 = roundRobinLoadBalancer.select(upstreamList, ""); + assertNotNull(result1, "Selected upstream should not be null"); + assertTrue(upstreamList.contains(result1), "Selected upstream should be from the provided list"); + + // Test with equal weight upstream list + Upstream result2 = roundRobinLoadBalancer.select(upstreamList2, ""); + assertNotNull(result2, "Selected upstream should not be null"); + assertTrue(upstreamList2.contains(result2), "Selected upstream should be from the provided list"); + + // Test multiple selections to verify round-robin behavior + Map<String, Integer> countMap = new HashMap<>(); + IntStream.range(0, SELECTION_ITERATIONS).forEach(i -> { + Upstream result = roundRobinLoadBalancer.select(upstreamList2, ""); + int count = countMap.getOrDefault(result.getUrl(), 0); + countMap.put(result.getUrl(), ++count); + }); + + // With equal weights, distribution should be roughly equal + assertEquals(3, countMap.size(), "All three upstreams should be selected"); + countMap.values().forEach(count -> + assertTrue(count >= 8 && count <= 12, "Distribution should be roughly equal for equal weights")); } -} +} \ No newline at end of file