You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by gu...@apache.org on 2021/10/13 03:03:51 UTC
[dubbo] branch 3.0 updated: Add unit test for Http2ProtocolDetector
and NettyEventLoopFactory (#9023)
This is an automated email from the ASF dual-hosted git repository.
guohao pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.0 by this push:
new 87f869d Add unit test for Http2ProtocolDetector and NettyEventLoopFactory (#9023)
87f869d is described below
commit 87f869d1dd9a925bf712767d377e50f8ce045946
Author: 灼华 <43...@users.noreply.github.com>
AuthorDate: Wed Oct 13 11:03:31 2021 +0800
Add unit test for Http2ProtocolDetector and NettyEventLoopFactory (#9023)
1.add ut
2.extract constants
3.remove duplicate class NettyEventLoopFactory
---
.../dubbo/common/constants/CommonConstants.java | 6 ++
.../threadpool/support/AbortPolicyWithReport.java | 6 +-
.../dubbo/common/timer/HashedWheelTimer.java | 5 +-
.../test/java/org/apache/dubbo/common/URLTest.java | 6 +-
.../support/AbortPolicyWithReportTest.java | 6 +-
.../java/org/apache/dubbo/remoting/Constants.java | 6 ++
.../org/apache/dubbo/remoting/api/Connection.java | 13 ++--
.../api/MultiplexProtocolConnectionManager.java | 4 +-
.../dubbo/remoting/api/NettyEventLoopFactory.java | 10 ++-
.../dubbo/remoting/api/PortUnificationServer.java | 6 +-
.../api/SingleProtocolConnectionManager.java | 2 +
.../apache/dubbo/remoting/api/ConnectionTest.java | 2 +-
.../remoting/api/Http2ProtocolDetectorTest.java | 52 ++++++++++++++
.../MultiplexProtocolConnectionManagerTest.java | 8 +--
.../remoting/api/NettyEventLoopFactoryTest.java | 82 ++++++++++++++++++++++
.../remoting/api/PortUnificationServerTest.java | 2 +-
.../api/SingleProtocolConnectionManagerTest.java | 20 ++++--
.../remoting/transport/netty/NettyServer.java | 6 +-
.../remoting/transport/netty4/NettyClient.java | 4 +-
.../transport/netty4/NettyEventLoopFactory.java | 56 ---------------
.../remoting/transport/netty4/NettyServer.java | 7 +-
21 files changed, 213 insertions(+), 96 deletions(-)
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java
index 06c222d..a45c12e 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java
@@ -476,4 +476,10 @@ public interface CommonConstants {
*/
String DUBBO_NETWORK_IGNORED_INTERFACE = "dubbo.network.interface.ignored";
+ String OS_NAME_KEY = "os.name";
+
+ String OS_LINUX_PREFIX = "linux";
+
+ String OS_WIN_PREFIX = "win";
+
}
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReport.java b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReport.java
index d00e6f1..77dff05 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReport.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReport.java
@@ -38,6 +38,8 @@ import java.util.concurrent.ThreadPoolExecutor;
import static java.lang.String.format;
import static org.apache.dubbo.common.constants.CommonConstants.DUMP_DIRECTORY;
+import static org.apache.dubbo.common.constants.CommonConstants.OS_NAME_KEY;
+import static org.apache.dubbo.common.constants.CommonConstants.OS_WIN_PREFIX;
/**
* Abort Policy.
@@ -55,10 +57,6 @@ public class AbortPolicyWithReport extends ThreadPoolExecutor.AbortPolicy {
private static final long TEN_MINUTES_MILLS = 10 * 60 * 1000;
- private static final String OS_WIN_PREFIX = "win";
-
- private static final String OS_NAME_KEY = "os.name";
-
private static final String WIN_DATETIME_FORMAT = "yyyy-MM-dd_HH-mm-ss";
private static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd_HH:mm:ss";
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/timer/HashedWheelTimer.java b/dubbo-common/src/main/java/org/apache/dubbo/common/timer/HashedWheelTimer.java
index 9f95e14..7ae561f 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/timer/HashedWheelTimer.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/timer/HashedWheelTimer.java
@@ -36,6 +36,9 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicLong;
+import static org.apache.dubbo.common.constants.CommonConstants.OS_NAME_KEY;
+import static org.apache.dubbo.common.constants.CommonConstants.OS_WIN_PREFIX;
+
/**
* A {@link Timer} optimized for approximated I/O timeout scheduling.
*
@@ -806,7 +809,7 @@ public class HashedWheelTimer implements Timer {
}
}
- private static final boolean IS_OS_WINDOWS = System.getProperty("os.name", "").toLowerCase(Locale.US).contains("win");
+ private static final boolean IS_OS_WINDOWS = System.getProperty(OS_NAME_KEY, "").toLowerCase(Locale.US).contains(OS_WIN_PREFIX);
private boolean isWindows() {
return IS_OS_WINDOWS;
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/URLTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/URLTest.java
index 089bf1b..9dcf568 100644
--- a/dubbo-common/src/test/java/org/apache/dubbo/common/URLTest.java
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/URLTest.java
@@ -28,6 +28,8 @@ import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;
+import static org.apache.dubbo.common.constants.CommonConstants.OS_NAME_KEY;
+import static org.apache.dubbo.common.constants.CommonConstants.OS_WIN_PREFIX;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -694,8 +696,8 @@ public class URLTest {
@Test
public void test_windowAbsolutePathBeginWithSlashIsValid() throws Exception {
- final String osProperty = System.getProperties().getProperty("os.name");
- if (!osProperty.toLowerCase().contains("windows")) return;
+ final String osProperty = System.getProperties().getProperty(OS_NAME_KEY);
+ if (!osProperty.toLowerCase().contains(OS_WIN_PREFIX)) return;
System.out.println("Test Windows valid path string.");
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReportTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReportTest.java
index 1319d92..909c841 100644
--- a/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReportTest.java
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReportTest.java
@@ -27,6 +27,8 @@ import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
+import static org.apache.dubbo.common.constants.CommonConstants.OS_NAME_KEY;
+import static org.apache.dubbo.common.constants.CommonConstants.OS_WIN_PREFIX;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class AbortPolicyWithReportTest {
@@ -69,8 +71,8 @@ public class AbortPolicyWithReportTest {
}
private String dumpDirectoryCannotBeCreated() {
- final String os = System.getProperty("os.name").toLowerCase();
- if (os.contains("win")) {
+ final String os = System.getProperty(OS_NAME_KEY).toLowerCase();
+ if (os.contains(OS_WIN_PREFIX)) {
// "con" is one of Windows reserved names, https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file
return "con";
} else {
diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/Constants.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/Constants.java
index f44b80b..5511abe 100644
--- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/Constants.java
+++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/Constants.java
@@ -103,6 +103,12 @@ public interface Constants {
int DEFAULT_IO_THREADS = Math.min(Runtime.getRuntime().availableProcessors() + 1, 32);
+ String EVENT_LOOP_BOSS_POOL_NAME = "NettyServerBoss";
+
+ String EVENT_LOOP_WORKER_POOL_NAME = "NettyServerWorker";
+
+ String NETTY_EPOLL_ENABLE_KEY = "netty.epoll.enable";
+
String BIND_IP_KEY = "bind.ip";
String BIND_PORT_KEY = "bind.port";
diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/Connection.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/Connection.java
index 18c9e6d..a675ef4 100644
--- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/Connection.java
+++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/Connection.java
@@ -53,7 +53,7 @@ import static org.apache.dubbo.common.constants.CommonConstants.SSL_ENABLED_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.THREADPOOL_KEY;
import static org.apache.dubbo.remoting.api.NettyEventLoopFactory.socketChannelClass;
-public class Connection extends AbstractReferenceCounted implements ReferenceCounted {
+public class Connection extends AbstractReferenceCounted {
public static final AttributeKey<Connection> CONNECTION = AttributeKey.valueOf("connection");
private static final Logger logger = LoggerFactory.getLogger(Connection.class);
@@ -94,7 +94,7 @@ public class Connection extends AbstractReferenceCounted implements ReferenceCou
.option(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
- .remoteAddress(getConnectAddress())
+ .remoteAddress(remote)
.channel(socketChannelClass());
final ConnectionHandler connectionHandler = new ConnectionHandler(this);
@@ -103,16 +103,17 @@ public class Connection extends AbstractReferenceCounted implements ReferenceCou
@Override
protected void initChannel(SocketChannel ch) {
+ final ChannelPipeline pipeline = ch.pipeline();
SslContext sslContext = null;
if (getUrl().getParameter(SSL_ENABLED_KEY, false)) {
- ch.pipeline().addLast("negotiation", new SslClientTlsHandler(url));
+ pipeline.addLast("negotiation", new SslClientTlsHandler(url));
}
- final ChannelPipeline p = ch.pipeline();//.addLast("logging",new LoggingHandler(LogLevel.INFO))//for debug
+ //.addLast("logging",new LoggingHandler(LogLevel.INFO))//for debug
// TODO support IDLE
// int heartbeatInterval = UrlUtils.getHeartbeat(getUrl());
- p.addLast(connectionHandler);
- protocol.configClientPipeline(url, p, sslContext);
+ pipeline.addLast(connectionHandler);
+ protocol.configClientPipeline(url, pipeline, sslContext);
// TODO support Socks5
}
});
diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/MultiplexProtocolConnectionManager.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/MultiplexProtocolConnectionManager.java
index f347538..7724976 100644
--- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/MultiplexProtocolConnectionManager.java
+++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/MultiplexProtocolConnectionManager.java
@@ -25,6 +25,8 @@ import java.util.concurrent.ConcurrentMap;
import java.util.function.Consumer;
public class MultiplexProtocolConnectionManager implements ConnectionManager {
+ public static final String NAME = "multiple";
+
private final ConcurrentMap<String, ConnectionManager> protocols = new ConcurrentHashMap<>();
private FrameworkModel frameworkModel;
@@ -45,6 +47,6 @@ public class MultiplexProtocolConnectionManager implements ConnectionManager {
}
private ConnectionManager createSingleProtocolConnectionManager(String protocol) {
- return frameworkModel.getExtensionLoader(ConnectionManager.class).getExtension("single");
+ return frameworkModel.getExtensionLoader(ConnectionManager.class).getExtension(SingleProtocolConnectionManager.NAME);
}
}
diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/NettyEventLoopFactory.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/NettyEventLoopFactory.java
index 5fb5015..c766c07 100644
--- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/NettyEventLoopFactory.java
+++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/NettyEventLoopFactory.java
@@ -32,6 +32,10 @@ import io.netty.util.concurrent.DefaultThreadFactory;
import java.util.concurrent.ThreadFactory;
+import static org.apache.dubbo.common.constants.CommonConstants.OS_LINUX_PREFIX;
+import static org.apache.dubbo.common.constants.CommonConstants.OS_NAME_KEY;
+import static org.apache.dubbo.remoting.Constants.NETTY_EPOLL_ENABLE_KEY;
+
public class NettyEventLoopFactory {
/**
* netty client bootstrap
@@ -53,9 +57,9 @@ public class NettyEventLoopFactory {
}
private static boolean shouldEpoll() {
- if (Boolean.parseBoolean(System.getProperty("netty.epoll.enable", "false"))) {
- String osName = System.getProperty("os.name");
- return osName.toLowerCase().contains("linux") && Epoll.isAvailable();
+ if (Boolean.parseBoolean(System.getProperty(NETTY_EPOLL_ENABLE_KEY, "false"))) {
+ String osName = System.getProperty(OS_NAME_KEY);
+ return osName.toLowerCase().contains(OS_LINUX_PREFIX) && Epoll.isAvailable();
}
return false;
diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/PortUnificationServer.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/PortUnificationServer.java
index d5f1ed1..15c0d5a 100644
--- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/PortUnificationServer.java
+++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/PortUnificationServer.java
@@ -47,6 +47,8 @@ import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE;
import static org.apache.dubbo.common.constants.CommonConstants.IO_THREADS_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.SSL_ENABLED_KEY;
+import static org.apache.dubbo.remoting.Constants.EVENT_LOOP_BOSS_POOL_NAME;
+import static org.apache.dubbo.remoting.Constants.EVENT_LOOP_WORKER_POOL_NAME;
/**
* PortUnificationServer.
@@ -100,10 +102,10 @@ public class PortUnificationServer {
protected void doOpen() {
bootstrap = new ServerBootstrap();
- bossGroup = NettyEventLoopFactory.eventLoopGroup(1, "NettyServerBoss");
+ bossGroup = NettyEventLoopFactory.eventLoopGroup(1, EVENT_LOOP_BOSS_POOL_NAME);
workerGroup = NettyEventLoopFactory.eventLoopGroup(
getUrl().getPositiveParameter(IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS),
- "NettyServerWorker");
+ EVENT_LOOP_WORKER_POOL_NAME);
bootstrap.group(bossGroup, workerGroup)
.channel(NettyEventLoopFactory.serverSocketChannelClass())
diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/SingleProtocolConnectionManager.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/SingleProtocolConnectionManager.java
index 8b57eed..efac3cb 100644
--- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/SingleProtocolConnectionManager.java
+++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/SingleProtocolConnectionManager.java
@@ -25,6 +25,8 @@ import java.util.concurrent.ConcurrentMap;
import java.util.function.Consumer;
public class SingleProtocolConnectionManager implements ConnectionManager {
+ public static final String NAME = "single";
+
private final ConcurrentMap<String, Connection> connections = PlatformDependent.newConcurrentHashMap();
@Override
diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/api/ConnectionTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/api/ConnectionTest.java
index 613c01b..1ae90d9 100644
--- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/api/ConnectionTest.java
+++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/api/ConnectionTest.java
@@ -37,7 +37,7 @@ class ConnectionTest {
}
@Test
- public void testRefCnt1() throws InterruptedException {
+ public void testRefCnt1() {
Connection connection = new Connection(URL.valueOf("empty://127.0.0.1:8080?foo=bar"));
CountDownLatch latch = new CountDownLatch(1);
connection.retain();
diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/api/Http2ProtocolDetectorTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/api/Http2ProtocolDetectorTest.java
new file mode 100644
index 0000000..702bf10
--- /dev/null
+++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/api/Http2ProtocolDetectorTest.java
@@ -0,0 +1,52 @@
+/*
+ * 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.remoting.api;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufAllocator;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.http2.Http2CodecUtil;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+/**
+ * {@link Http2ProtocolDetector}
+ */
+public class Http2ProtocolDetectorTest {
+
+ @Test
+ public void testDetect() {
+ ProtocolDetector detector = new Http2ProtocolDetector();
+ ChannelHandlerContext ctx = Mockito.mock(ChannelHandlerContext.class);
+
+ ByteBuf connectionPrefaceBuf = Http2CodecUtil.connectionPrefaceBuf();
+ ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer();
+ ProtocolDetector.Result result = detector.detect(ctx, byteBuf);
+ Assertions.assertEquals(result, ProtocolDetector.Result.UNRECOGNIZED);
+
+ byteBuf.writeBytes(connectionPrefaceBuf);
+ result = detector.detect(ctx, byteBuf);
+ Assertions.assertEquals(result, ProtocolDetector.Result.RECOGNIZED);
+
+ byteBuf.clear();
+ byteBuf.writeBytes(connectionPrefaceBuf, 0, 1);
+ result = detector.detect(ctx, byteBuf);
+ Assertions.assertEquals(result, ProtocolDetector.Result.NEED_MORE_DATA);
+
+ }
+}
diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/api/MultiplexProtocolConnectionManagerTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/api/MultiplexProtocolConnectionManagerTest.java
index b9d38fb..ba921b4 100644
--- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/api/MultiplexProtocolConnectionManagerTest.java
+++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/api/MultiplexProtocolConnectionManagerTest.java
@@ -29,18 +29,18 @@ import java.util.Map;
import java.util.function.Consumer;
public class MultiplexProtocolConnectionManagerTest {
- private ConnectionManager connectionManager = ExtensionLoader.getExtensionLoader(ConnectionManager.class).getExtension("multiple");
+ private ConnectionManager connectionManager = ExtensionLoader.getExtensionLoader(ConnectionManager.class).getExtension(MultiplexProtocolConnectionManager.NAME);
@Test
public void testConnect() throws Exception {
URL url = URL.valueOf("empty://127.0.0.1:8080?foo=bar");
- Connection connect = connectionManager.connect(url);
- Assertions.assertNotNull(connect);
+ Connection connection = connectionManager.connect(url);
+ Assertions.assertNotNull(connection);
Field protocolsField = connectionManager.getClass().getDeclaredField("protocols");
protocolsField.setAccessible(true);
Map protocolMap = (Map) protocolsField.get(connectionManager);
Assertions.assertNotNull(protocolMap.get(url.getProtocol()));
- connect.close();
+ connection.close();
}
@Test
diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/api/NettyEventLoopFactoryTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/api/NettyEventLoopFactoryTest.java
new file mode 100644
index 0000000..9a59609
--- /dev/null
+++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/api/NettyEventLoopFactoryTest.java
@@ -0,0 +1,82 @@
+/*
+ * 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.remoting.api;
+
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.epoll.Epoll;
+import io.netty.channel.epoll.EpollEventLoopGroup;
+import io.netty.channel.epoll.EpollServerSocketChannel;
+import io.netty.channel.epoll.EpollSocketChannel;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.ServerSocketChannel;
+import io.netty.channel.socket.SocketChannel;
+import io.netty.channel.socket.nio.NioServerSocketChannel;
+import io.netty.channel.socket.nio.NioSocketChannel;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.apache.dubbo.common.constants.CommonConstants.OS_LINUX_PREFIX;
+import static org.apache.dubbo.common.constants.CommonConstants.OS_NAME_KEY;
+import static org.apache.dubbo.remoting.Constants.NETTY_EPOLL_ENABLE_KEY;
+
+/**
+ * {@link NettyEventLoopFactory}
+ */
+public class NettyEventLoopFactoryTest {
+
+ @BeforeEach
+ public void setUp() {
+ System.setProperty(NETTY_EPOLL_ENABLE_KEY, "true");
+ }
+
+ @AfterEach
+ public void reset() {
+ System.clearProperty(NETTY_EPOLL_ENABLE_KEY);
+ }
+
+ @Test
+ void eventLoopGroup() {
+ if (isEpoll()) {
+ EventLoopGroup eventLoopGroup = NettyEventLoopFactory.eventLoopGroup(1, "test");
+ Assertions.assertTrue(eventLoopGroup instanceof EpollEventLoopGroup);
+
+ Class<? extends SocketChannel> socketChannelClass = NettyEventLoopFactory.socketChannelClass();
+ Assertions.assertEquals(socketChannelClass, EpollSocketChannel.class);
+
+ Class<? extends ServerSocketChannel> serverSocketChannelClass = NettyEventLoopFactory.serverSocketChannelClass();
+ Assertions.assertEquals(serverSocketChannelClass, EpollServerSocketChannel.class);
+
+ } else {
+ EventLoopGroup eventLoopGroup = NettyEventLoopFactory.eventLoopGroup(1, "test");
+ Assertions.assertTrue(eventLoopGroup instanceof NioEventLoopGroup);
+
+ Class<? extends SocketChannel> socketChannelClass = NettyEventLoopFactory.socketChannelClass();
+ Assertions.assertEquals(socketChannelClass, NioSocketChannel.class);
+
+ Class<? extends ServerSocketChannel> serverSocketChannelClass = NettyEventLoopFactory.serverSocketChannelClass();
+ Assertions.assertEquals(serverSocketChannelClass, NioServerSocketChannel.class);
+ }
+ }
+
+ private boolean isEpoll() {
+ String osName = System.getProperty(OS_NAME_KEY);
+ return osName.toLowerCase().contains(OS_LINUX_PREFIX) && Epoll.isAvailable();
+ }
+
+}
diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/api/PortUnificationServerTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/api/PortUnificationServerTest.java
index 98ee317..338bd81 100644
--- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/api/PortUnificationServerTest.java
+++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/api/PortUnificationServerTest.java
@@ -27,7 +27,7 @@ import org.junit.jupiter.api.Test;
public class PortUnificationServerTest {
@Test
- public void testBind() throws RemotingException {
+ public void testBind() {
URL url = new ServiceConfigURL(CommonConstants.TRIPLE, "localhost", 8898,
new String[]{Constants.BIND_PORT_KEY, String.valueOf(8898)});
diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/api/SingleProtocolConnectionManagerTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/api/SingleProtocolConnectionManagerTest.java
index 077b3ec..9275565 100644
--- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/api/SingleProtocolConnectionManagerTest.java
+++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/api/SingleProtocolConnectionManagerTest.java
@@ -29,30 +29,36 @@ import java.util.function.Consumer;
public class SingleProtocolConnectionManagerTest {
- private ConnectionManager connectionManager = ExtensionLoader.getExtensionLoader(ConnectionManager.class).getExtension("single");
+ private ConnectionManager connectionManager = ExtensionLoader.getExtensionLoader(ConnectionManager.class).getExtension(SingleProtocolConnectionManager.NAME);
@Test
public void testConnect() throws Exception {
URL url = URL.valueOf("empty://127.0.0.1:8080?foo=bar");
- Connection connect = connectionManager.connect(url);
- Assertions.assertNotNull(connect);
+ Connection connection = connectionManager.connect(url);
+ Assertions.assertNotNull(connection);
Field protocolsField = connectionManager.getClass().getDeclaredField("connections");
protocolsField.setAccessible(true);
Map protocolMap = (Map) protocolsField.get(connectionManager);
Assertions.assertNotNull(protocolMap.get(url.getAddress()));
- connect.close();
+ connection.close();
+
+ // Test whether closePromise's listener removes entry
+ connection.getClosePromise().await();
+ while (protocolMap.containsKey(url.getAddress())) {
+ }
+ Assertions.assertNull(protocolMap.get(url.getAddress()));
}
@Test
public void testForEachConnection() throws RemotingException {
URL url = URL.valueOf("empty://127.0.0.1:8080?foo=bar");
- Connection connect = connectionManager.connect(url);
+ Connection connection = connectionManager.connect(url);
{
Consumer<Connection> consumer1 = new Consumer<Connection>() {
@Override
public void accept(Connection connection) {
- Assertions.assertEquals( "empty", connection.getUrl().getProtocol());
+ Assertions.assertEquals("empty", connection.getUrl().getProtocol());
}
};
@@ -63,7 +69,7 @@ public class SingleProtocolConnectionManagerTest {
Consumer<Connection> consumer2 = new Consumer<Connection>() {
@Override
public void accept(Connection connection) {
- Assertions.assertNotEquals( "not-empty", connection.getUrl().getProtocol());
+ Assertions.assertNotEquals("not-empty", connection.getUrl().getProtocol());
}
};
diff --git a/dubbo-remoting/dubbo-remoting-netty/src/main/java/org/apache/dubbo/remoting/transport/netty/NettyServer.java b/dubbo-remoting/dubbo-remoting-netty/src/main/java/org/apache/dubbo/remoting/transport/netty/NettyServer.java
index a74412d..4afdb00 100644
--- a/dubbo-remoting/dubbo-remoting-netty/src/main/java/org/apache/dubbo/remoting/transport/netty/NettyServer.java
+++ b/dubbo-remoting/dubbo-remoting-netty/src/main/java/org/apache/dubbo/remoting/transport/netty/NettyServer.java
@@ -47,6 +47,8 @@ import java.util.concurrent.Executors;
import static org.apache.dubbo.common.constants.CommonConstants.BACKLOG_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.IO_THREADS_KEY;
+import static org.apache.dubbo.remoting.Constants.EVENT_LOOP_BOSS_POOL_NAME;
+import static org.apache.dubbo.remoting.Constants.EVENT_LOOP_WORKER_POOL_NAME;
/**
* NettyServer
@@ -68,8 +70,8 @@ public class NettyServer extends AbstractServer implements RemotingServer {
@Override
protected void doOpen() throws Throwable {
NettyHelper.setNettyLoggerFactory();
- ExecutorService boss = Executors.newCachedThreadPool(new NamedThreadFactory("NettyServerBoss", true));
- ExecutorService worker = Executors.newCachedThreadPool(new NamedThreadFactory("NettyServerWorker", true));
+ ExecutorService boss = Executors.newCachedThreadPool(new NamedThreadFactory(EVENT_LOOP_BOSS_POOL_NAME, true));
+ ExecutorService worker = Executors.newCachedThreadPool(new NamedThreadFactory(EVENT_LOOP_WORKER_POOL_NAME, true));
ChannelFactory channelFactory = new NioServerSocketChannelFactory(boss, worker, getUrl().getPositiveParameter(IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS));
bootstrap = new ServerBootstrap(channelFactory);
diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClient.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClient.java
index 5d7fb3e..f18893b 100644
--- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClient.java
+++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClient.java
@@ -45,8 +45,8 @@ import java.net.InetSocketAddress;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.apache.dubbo.common.constants.CommonConstants.SSL_ENABLED_KEY;
import static org.apache.dubbo.remoting.Constants.DEFAULT_CONNECT_TIMEOUT;
-import static org.apache.dubbo.remoting.transport.netty4.NettyEventLoopFactory.eventLoopGroup;
-import static org.apache.dubbo.remoting.transport.netty4.NettyEventLoopFactory.socketChannelClass;
+import static org.apache.dubbo.remoting.api.NettyEventLoopFactory.eventLoopGroup;
+import static org.apache.dubbo.remoting.api.NettyEventLoopFactory.socketChannelClass;
/**
* NettyClient.
diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyEventLoopFactory.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyEventLoopFactory.java
deleted file mode 100644
index eb97078..0000000
--- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyEventLoopFactory.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * 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.remoting.transport.netty4;
-
-import io.netty.channel.EventLoopGroup;
-import io.netty.channel.epoll.Epoll;
-import io.netty.channel.epoll.EpollEventLoopGroup;
-import io.netty.channel.epoll.EpollServerSocketChannel;
-import io.netty.channel.epoll.EpollSocketChannel;
-import io.netty.channel.nio.NioEventLoopGroup;
-import io.netty.channel.socket.ServerSocketChannel;
-import io.netty.channel.socket.SocketChannel;
-import io.netty.channel.socket.nio.NioServerSocketChannel;
-import io.netty.channel.socket.nio.NioSocketChannel;
-import io.netty.util.concurrent.DefaultThreadFactory;
-
-import java.util.concurrent.ThreadFactory;
-
-public class NettyEventLoopFactory {
- public static EventLoopGroup eventLoopGroup(int threads, String threadFactoryName) {
- ThreadFactory threadFactory = new DefaultThreadFactory(threadFactoryName, true);
- return shouldEpoll() ? new EpollEventLoopGroup(threads, threadFactory) :
- new NioEventLoopGroup(threads, threadFactory);
- }
-
- public static Class<? extends SocketChannel> socketChannelClass() {
- return shouldEpoll() ? EpollSocketChannel.class : NioSocketChannel.class;
- }
-
- public static Class<? extends ServerSocketChannel> serverSocketChannelClass() {
- return shouldEpoll() ? EpollServerSocketChannel.class : NioServerSocketChannel.class;
- }
-
- private static boolean shouldEpoll() {
- if (Boolean.parseBoolean(System.getProperty("netty.epoll.enable", "false"))) {
- String osName = System.getProperty("os.name");
- return osName.toLowerCase().contains("linux") && Epoll.isAvailable();
- }
-
- return false;
- }
-}
diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServer.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServer.java
index 262e71c..3d66046 100644
--- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServer.java
+++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServer.java
@@ -36,6 +36,7 @@ import org.apache.dubbo.remoting.ChannelHandler;
import org.apache.dubbo.remoting.Constants;
import org.apache.dubbo.remoting.RemotingException;
import org.apache.dubbo.remoting.RemotingServer;
+import org.apache.dubbo.remoting.api.NettyEventLoopFactory;
import org.apache.dubbo.remoting.api.SslServerTlsHandler;
import org.apache.dubbo.remoting.transport.AbstractServer;
import org.apache.dubbo.remoting.transport.dispatcher.ChannelHandlers;
@@ -50,6 +51,8 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.apache.dubbo.common.constants.CommonConstants.IO_THREADS_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.KEEP_ALIVE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.SSL_ENABLED_KEY;
+import static org.apache.dubbo.remoting.Constants.EVENT_LOOP_BOSS_POOL_NAME;
+import static org.apache.dubbo.remoting.Constants.EVENT_LOOP_WORKER_POOL_NAME;
/**
@@ -94,10 +97,10 @@ public class NettyServer extends AbstractServer implements RemotingServer {
protected void doOpen() throws Throwable {
bootstrap = new ServerBootstrap();
- bossGroup = NettyEventLoopFactory.eventLoopGroup(1, "NettyServerBoss");
+ bossGroup = NettyEventLoopFactory.eventLoopGroup(1, EVENT_LOOP_BOSS_POOL_NAME);
workerGroup = NettyEventLoopFactory.eventLoopGroup(
getUrl().getPositiveParameter(IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS),
- "NettyServerWorker");
+ EVENT_LOOP_WORKER_POOL_NAME);
final NettyServerHandler nettyServerHandler = new NettyServerHandler(getUrl(), this);
channels = nettyServerHandler.getChannels();