Fix assertion error and class cast exception in unit tests. (#10825)
This commit is contained in:
parent
4d289883e7
commit
393ea1d7e2
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<ExecuteLimitFilter> invoker = new BlockMyInvoker<ExecuteLimitFilter>(url, 1000);
|
||||
final Invoker<ExecuteLimitFilter> 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());
|
||||
|
|
|
|||
Loading…
Reference in New Issue