This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/master by this push:
new 4af7d63 Add test case for ServerStatusChecker (#7889)
4af7d63 is described below
commit 4af7d632efcbd9f351a5ecc7fa7506d33dab9452
Author: xiaoheng1 <[email protected]>
AuthorDate: Mon May 31 10:17:24 2021 +0800
Add test case for ServerStatusChecker (#7889)
* fix #7888 add test case for ServerStatusChecker
* use available port
* modify getAvailablePort
* fix testGetAvailablePort test case.
* modify min_port=1
* make getAvailablePort Sync
* use random port as init value
* bugfix
* Use bitset to optimize code
* remote unused import
* remove unused import
* bugfix
* bugfix
* add log
* bugfix
* bugfix
---
.../org/apache/dubbo/common/utils/NetUtils.java | 39 ++++++++-----
.../apache/dubbo/common/utils/NetUtilsTest.java | 2 +-
.../dubbo/status/ServerStatusCheckerTest.java | 65 ++++++++++++++++++++++
3 files changed, 90 insertions(+), 16 deletions(-)
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java
b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java
index 6161e07..e99d59e 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java
@@ -33,6 +33,7 @@ import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.SocketException;
import java.net.UnknownHostException;
+import java.util.BitSet;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
@@ -70,7 +71,7 @@ public class NetUtils {
private static final int RND_PORT_RANGE = 10000;
// valid port range is (0, 65535]
- private static final int MIN_PORT = 0;
+ private static final int MIN_PORT = 1;
private static final int MAX_PORT = 65535;
private static final Pattern ADDRESS_PATTERN =
Pattern.compile("^\\d{1,3}(\\.\\d{1,3}){3}\\:\\d{1,5}$");
@@ -83,25 +84,31 @@ public class NetUtils {
private static final String SPLIT_IPV4_CHARACTER = "\\.";
private static final String SPLIT_IPV6_CHARACTER = ":";
+ /**
+ * store the used port.
+ * the set used only on the synchronized method.
+ */
+ private static BitSet USED_PORT = new BitSet(65536);
+
public static int getRandomPort() {
return RND_PORT_START +
ThreadLocalRandom.current().nextInt(RND_PORT_RANGE);
}
- public static int getAvailablePort() {
- try (ServerSocket ss = new ServerSocket()) {
- ss.bind(null);
- return ss.getLocalPort();
- } catch (IOException e) {
- return getRandomPort();
- }
+ public synchronized static int getAvailablePort() {
+ int randomPort = getRandomPort();
+ return getAvailablePort(randomPort);
}
- public static int getAvailablePort(int port) {
- if (port <= 0) {
- return getAvailablePort();
+ public synchronized static int getAvailablePort(int port) {
+ if (port < MIN_PORT) {
+ port = MIN_PORT;
}
for (int i = port; i < MAX_PORT; i++) {
+ if (USED_PORT.get(i)) {
+ continue;
+ }
try (ServerSocket ignored = new ServerSocket(i)) {
+ USED_PORT.set(i);
return i;
} catch (IOException e) {
// continue
@@ -111,7 +118,7 @@ public class NetUtils {
}
public static boolean isInvalidPort(int port) {
- return port <= MIN_PORT || port > MAX_PORT;
+ return port < MIN_PORT || port > MAX_PORT;
}
public static boolean isValidAddress(String address) {
@@ -572,7 +579,7 @@ public class NetUtils {
if (!ipPatternContainExpression(pattern)) {
InetAddress patternAddress = InetAddress.getByName(pattern);
return patternAddress.getHostAddress().equals(host);
- }
+ }
String[] ipAddress = host.split(splitCharacter);
for (int i = 0; i < mask.length; i++) {
@@ -589,7 +596,8 @@ public class NetUtils {
if (ip < min || ip > max) {
return false;
}
- } else if ("0".equals(ipAddress[i]) && ("0".equals(mask[i]) ||
"00".equals(mask[i]) || "000".equals(mask[i]) || "0000".equals(mask[i]))) {
+ } else if ("0".equals(ipAddress[i]) &&
+ ("0".equals(mask[i]) || "00".equals(mask[i]) ||
"000".equals(mask[i]) || "0000".equals(mask[i]))) {
continue;
} else if (!mask[i].equals(ipAddress[i])) {
return false;
@@ -623,7 +631,8 @@ public class NetUtils {
private static void checkHostPattern(String pattern, String[] mask,
boolean isIpv4) {
if (!isIpv4) {
if (mask.length != 8 && ipPatternContainExpression(pattern)) {
- throw new IllegalArgumentException("If you config ip
expression that contains '*' or '-', please fill qualified ip pattern like
234e:0:4567:0:0:0:3d:*. ");
+ throw new IllegalArgumentException(
+ "If you config ip expression that contains '*' or '-',
please fill qualified ip pattern like 234e:0:4567:0:0:0:3d:*. ");
}
if (mask.length != 8 && !pattern.contains("::")) {
throw new IllegalArgumentException("The host is ipv6, but the
pattern is not ipv6 pattern : " + pattern);
diff --git
a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java
b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java
index 4374b84..562902d 100644
--- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java
@@ -52,7 +52,7 @@ public class NetUtilsTest {
@Test
public void testGetAvailablePort() throws Exception {
- assertThat(NetUtils.getAvailablePort(), greaterThan(0));
+ assertThat(NetUtils.getAvailablePort(), greaterThanOrEqualTo(0));
assertThat(NetUtils.getAvailablePort(12345),
greaterThanOrEqualTo(12345));
assertThat(NetUtils.getAvailablePort(-1), greaterThanOrEqualTo(0));
}
diff --git
a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/status/ServerStatusCheckerTest.java
b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/status/ServerStatusCheckerTest.java
new file mode 100644
index 0000000..548e844
--- /dev/null
+++
b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/status/ServerStatusCheckerTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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.dubbo.rpc.protocol.dubbo.status;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.extension.ExtensionLoader;
+import org.apache.dubbo.common.status.Status;
+import org.apache.dubbo.common.status.StatusChecker;
+import org.apache.dubbo.common.utils.NetUtils;
+import org.apache.dubbo.rpc.ProxyFactory;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+import org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol;
+import org.apache.dubbo.rpc.protocol.dubbo.support.DemoService;
+import org.apache.dubbo.rpc.protocol.dubbo.support.DemoServiceImpl;
+import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+public class ServerStatusCheckerTest {
+ private ProxyFactory proxy =
ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
+
+ @AfterAll
+ public static void after() {
+
ApplicationModel.getServiceRepository().unregisterService(DemoService.class);
+ }
+
+ @BeforeAll
+ public static void setup() {
+
ApplicationModel.getServiceRepository().registerService(DemoService.class);
+ }
+
+ @Test
+ public void testServerStatusChecker() throws Exception {
+ int port = NetUtils.getAvailablePort(7000);
+ URL url = URL.valueOf("dubbo://127.0.0.1:" + port + "/" +
DemoService.class.getName());
+ DemoService service = new DemoServiceImpl();
+
+ DubboProtocol.getDubboProtocol().export(proxy.getInvoker(service,
DemoService.class, url));
+
+ StatusChecker server =
ExtensionLoader.getExtensionLoader(StatusChecker.class).getExtension("server");
+ Assertions.assertEquals(ServerStatusChecker.class, server.getClass());
+
+ Status status = server.check();
+ Assertions.assertEquals(Status.Level.OK, status.getLevel());
+
+ ProtocolUtils.closeAll();
+ }
+}