From 393ea1d7e21dfbb54b8aadacaba57a2aa0928069 Mon Sep 17 00:00:00 2001 From: Andy Cheung Date: Wed, 26 Oct 2022 10:20:48 +0800 Subject: [PATCH] Fix assertion error and class cast exception in unit tests. (#10825) --- .../transport/netty4/ConnectionTest.java | 21 +++++--- .../rpc/filter/ExecuteLimitFilterTest.java | 50 ++++++++++--------- 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ConnectionTest.java b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ConnectionTest.java index 55aa550639..cb1037ef78 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ConnectionTest.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/ConnectionTest.java @@ -28,9 +28,9 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -public class ConnectionTest { +class ConnectionTest { @Test - public void connectSyncTest() throws Throwable { + void connectSyncTest() throws Throwable { int port = NetUtils.getAvailablePort(); URL url = URL.valueOf("empty://127.0.0.1:" + port + "?foo=bar"); NettyPortUnificationServer server = null; @@ -57,39 +57,44 @@ public class ConnectionTest { // ignored } } - - } @Test - public void testMultiConnect() throws Throwable { + void testMultiConnect() throws Throwable { int port = NetUtils.getAvailablePort(); URL url = URL.valueOf("empty://127.0.0.1:" + port + "?foo=bar"); NettyPortUnificationServer server = null; + ExecutorService service = Executors.newFixedThreadPool(10); + try { server = new NettyPortUnificationServer(url, new DefaultPuHandler()); - server.close(); Connection connection = new Connection(url); - ExecutorService service = Executors.newFixedThreadPool(10); + final CountDownLatch latch = new CountDownLatch(10); for (int i = 0; i < 10; i++) { Runnable runnable = () -> { try { Assertions.assertTrue(connection.isAvailable()); - latch.countDown(); } catch (Exception e) { // ignore + } finally { + latch.countDown(); } }; + service.execute(runnable); } + + latch.await(); } finally { try { server.close(); } catch (Throwable e) { // ignored } + + service.shutdown(); } } } diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ExecuteLimitFilterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ExecuteLimitFilterTest.java index 4a8ba537eb..2f0f3b5faf 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ExecuteLimitFilterTest.java +++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ExecuteLimitFilterTest.java @@ -22,6 +22,7 @@ import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.Result; import org.apache.dubbo.rpc.RpcException; +import org.apache.dubbo.rpc.RpcInvocation; import org.apache.dubbo.rpc.RpcStatus; import org.apache.dubbo.rpc.support.BlockMyInvoker; @@ -36,12 +37,12 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.when; -public class ExecuteLimitFilterTest { +class ExecuteLimitFilterTest { private ExecuteLimitFilter executeLimitFilter = new ExecuteLimitFilter(); @Test - public void testNoExecuteLimitInvoke() throws Exception { + void testNoExecuteLimitInvoke() { Invoker invoker = Mockito.mock(Invoker.class); when(invoker.invoke(any(Invocation.class))).thenReturn(new AppResponse("result")); when(invoker.getUrl()).thenReturn(URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1")); @@ -54,7 +55,7 @@ public class ExecuteLimitFilterTest { } @Test - public void testExecuteLimitInvoke() throws Exception { + void testExecuteLimitInvoke() { Invoker invoker = Mockito.mock(Invoker.class); when(invoker.invoke(any(Invocation.class))).thenReturn(new AppResponse("result")); when(invoker.getUrl()).thenReturn(URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&executes=10")); @@ -67,7 +68,7 @@ public class ExecuteLimitFilterTest { } @Test - public void testExecuteLimitInvokeWitException() throws Exception { + void testExecuteLimitInvokeWithException() { Invoker invoker = Mockito.mock(Invoker.class); doThrow(new RpcException()) .when(invoker).invoke(any(Invocation.class)); @@ -89,43 +90,44 @@ public class ExecuteLimitFilterTest { } @Test - public void testMoreThanExecuteLimitInvoke() throws Exception { + void testMoreThanExecuteLimitInvoke() { int maxExecute = 10; int totalExecute = 20; final AtomicInteger failed = new AtomicInteger(0); - final Invocation invocation = Mockito.mock(Invocation.class); + final Invocation invocation = Mockito.mock(RpcInvocation.class); when(invocation.getMethodName()).thenReturn("testMoreThanExecuteLimitInvoke"); URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&executes=" + maxExecute); - final Invoker invoker = new BlockMyInvoker(url, 1000); + final Invoker invoker = new BlockMyInvoker<>(url, 1000); + + final CountDownLatch latchThatWaitsAllThreadStarted = new CountDownLatch(1); + final CountDownLatch latchThatWaitsAllThreadFinished = new CountDownLatch(totalExecute); - final CountDownLatch latch = new CountDownLatch(1); for (int i = 0; i < totalExecute; i++) { - Thread thread = new Thread(new Runnable() { - - public void run() { - try { - latch.await(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - try { - executeLimitFilter.invoke(invoker, invocation); - } catch (RpcException expected) { - failed.incrementAndGet(); - } + Thread thread = new Thread(() -> { + try { + latchThatWaitsAllThreadStarted.await(); + executeLimitFilter.invoke(invoker, invocation); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (RpcException expected) { + failed.incrementAndGet(); + } finally { + latchThatWaitsAllThreadFinished.countDown(); } }); + thread.start(); } - latch.countDown(); + + latchThatWaitsAllThreadStarted.countDown(); try { - Thread.sleep(1000); + latchThatWaitsAllThreadFinished.await(); } catch (InterruptedException e) { - e.printStackTrace(); + throw new RuntimeException(e); } Assertions.assertEquals(totalExecute - maxExecute, failed.get());