This is an automated email from the ASF dual-hosted git repository.
Aias00 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 7c94ab8878 test(loadbalancer): avoid external network in health check
(#7409)
7c94ab8878 is described below
commit 7c94ab88783d34aebfb7c23751c702807750f01d
Author: JerryčAI <[email protected]>
AuthorDate: Fri Oct 2 12:30:33 2026 +0800
test(loadbalancer): avoid external network in health check (#7409)
---
.../loadbalancer/cache/UpstreamCheckTaskTest.java | 17 ++++++++++-------
1 file changed, 10 insertions(+), 7 deletions(-)
diff --git
a/shenyu-loadbalancer/src/test/java/org/apache/shenyu/loadbalancer/cache/UpstreamCheckTaskTest.java
b/shenyu-loadbalancer/src/test/java/org/apache/shenyu/loadbalancer/cache/UpstreamCheckTaskTest.java
index d103e8844f..8532f4e433 100644
---
a/shenyu-loadbalancer/src/test/java/org/apache/shenyu/loadbalancer/cache/UpstreamCheckTaskTest.java
+++
b/shenyu-loadbalancer/src/test/java/org/apache/shenyu/loadbalancer/cache/UpstreamCheckTaskTest.java
@@ -24,6 +24,8 @@ import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
+import java.io.IOException;
+import java.net.ServerSocket;
import java.util.concurrent.TimeUnit;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -48,7 +50,7 @@ public class UpstreamCheckTaskTest {
*/
@Test
@Timeout(30000)
- public void testRun() {
+ public void testRun() throws IOException {
// Mock selectorId1~selectorId4 to let it coverage 4 branch of
`HealthCheckTask#check` method.
final String selectorId1 = "s1";
SelectorData selectorData1 = mock(SelectorData.class);
@@ -83,12 +85,13 @@ public class UpstreamCheckTaskTest {
assertTrue(CollectionUtils.isNotEmpty(healthCheckTask.getUnhealthyUpstream().get(selectorId1)));
// Let it coverage line 151~163.
when(upstream.isHealthy()).thenReturn(false).thenReturn(true);
- // Even if the address could not connect, it will return false, that
mean it will not coverage 151~163.
- when(upstream.getUrl()).thenReturn("https://www.baidu.com");
- // Manually run one time
- healthCheckTask.run();
- Awaitility.await().pollDelay(1, TimeUnit.SECONDS).untilAsserted(() ->
assertFalse(healthCheckTask.getCheckStarted().get()));
-
assertFalse(healthCheckTask.getHealthyUpstream().get(selectorId1).isEmpty());
+ try (ServerSocket serverSocket = new ServerSocket(0)) {
+ when(upstream.getUrl()).thenReturn("http://127.0.0.1:" +
serverSocket.getLocalPort());
+ // Manually run one time against a local listening socket.
+ healthCheckTask.run();
+ Awaitility.await().pollDelay(1, TimeUnit.SECONDS).untilAsserted(()
-> assertFalse(healthCheckTask.getCheckStarted().get()));
+
assertFalse(healthCheckTask.getHealthyUpstream().get(selectorId1).isEmpty());
+ }
healthCheckTask.print();
}