fix : test for rpc server-side

pull/945/head
pizihao 3 years ago
parent 433df608e2
commit 5c85225133

@ -18,13 +18,13 @@
package cn.hippo4j.rpc.client;
import cn.hippo4j.rpc.discovery.ServerPort;
import cn.hippo4j.rpc.handler.NettyClientPoolHandler;
import cn.hippo4j.rpc.handler.AbstractNettyClientPoolHandler;
import cn.hippo4j.rpc.handler.NettyClientTakeHandler;
import cn.hippo4j.rpc.handler.NettyServerTakeHandler;
import cn.hippo4j.rpc.model.DefaultRequest;
import cn.hippo4j.rpc.model.Request;
import cn.hippo4j.rpc.model.Response;
import cn.hippo4j.rpc.server.NettyServerConnection;
import cn.hippo4j.rpc.server.AbstractNettyServerConnection;
import cn.hippo4j.rpc.server.RPCServer;
import cn.hippo4j.rpc.server.ServerConnection;
import cn.hippo4j.rpc.discovery.ClassRegistry;
@ -52,15 +52,15 @@ public class RPCClientTest {
// The mode connection was denied when the server was started on the specified port
Instance instance = new DefaultInstance();
NettyServerTakeHandler handler = new NettyServerTakeHandler(instance);
ServerConnection connection = new NettyServerConnection(handler);
RPCServer rpcServer = new RPCServer(port, connection);
ServerConnection connection = new AbstractNettyServerConnection(handler);
RPCServer rpcServer = new RPCServer(connection, port);
CompletableFuture.runAsync(rpcServer::bind);
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
ChannelPoolHandler channelPoolHandler = new NettyClientPoolHandler(new NettyClientTakeHandler());
ChannelPoolHandler channelPoolHandler = new AbstractNettyClientPoolHandler(new NettyClientTakeHandler());
NettyClientConnection clientConnection = new NettyClientConnection(host, port, channelPoolHandler);
RPCClient rpcClient = new RPCClient(clientConnection);
Request request = new DefaultRequest("127.0.0.18888", className, "call", null, null);
@ -85,15 +85,15 @@ public class RPCClientTest {
// The mode connection was denied when the server was started on the specified port
Instance instance = new DefaultInstance();
NettyServerTakeHandler handler = new NettyServerTakeHandler(instance);
ServerConnection connection = new NettyServerConnection(handler);
RPCServer rpcServer = new RPCServer(portTest, connection);
ServerConnection connection = new AbstractNettyServerConnection(handler);
RPCServer rpcServer = new RPCServer(connection, portTest);
CompletableFuture.runAsync(rpcServer::bind);
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
ChannelPoolHandler channelPoolHandler = new NettyClientPoolHandler(new NettyClientTakeHandler());
ChannelPoolHandler channelPoolHandler = new AbstractNettyClientPoolHandler(new NettyClientTakeHandler());
NettyClientConnection clientConnection = new NettyClientConnection(host, portTest, channelPoolHandler);
RPCClient rpcClient = new RPCClient(clientConnection);
Class<?>[] classes = new Class<?>[2];

@ -4,14 +4,13 @@ import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.boot.test.context.TestComponent;
@Setter
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class InstanceServerLoaderImpl implements InstanceServerLoader {
String name;
String name = "name";
}

@ -0,0 +1,42 @@
package cn.hippo4j.rpc.handler;
import cn.hippo4j.rpc.client.NettyClientConnection;
import cn.hippo4j.rpc.client.RPCClient;
import cn.hippo4j.rpc.discovery.*;
import cn.hippo4j.rpc.server.AbstractNettyServerConnection;
import cn.hippo4j.rpc.server.RPCServer;
import cn.hippo4j.rpc.support.NettyProxyCenter;
import io.netty.channel.pool.ChannelPoolHandler;
import org.junit.Assert;
import org.junit.Test;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
public class ConnectHandlerTest {
@Test
public void handlerTest() {
// server
Class<InstanceServerLoader> cls = InstanceServerLoader.class;
ClassRegistry.put(cls.getName(), cls);
ServerPort port = () -> 8888;
Instance instance = new DefaultInstance();
NettyServerTakeHandler serverHandler = new NettyServerTakeHandler(instance);
AbstractNettyServerConnection connection = new AbstractNettyServerConnection(serverHandler);
RPCServer rpcServer = new RPCServer(connection, port);
CompletableFuture.runAsync(rpcServer::bind);
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
ChannelPoolHandler channelPoolHandler = new AbstractNettyClientPoolHandler(new NettyClientTakeHandler());
NettyClientConnection clientConnection = new NettyClientConnection("localhost", port, channelPoolHandler);
RPCClient rpcClient = new RPCClient(clientConnection);
InstanceServerLoader loader = NettyProxyCenter.getProxy(rpcClient, cls, "localhost", port);
String name = loader.getName();
Assert.assertEquals("name", name);
}
}

@ -11,7 +11,7 @@ public class NettyClientPoolHandlerTest {
TestHandler handler = new TestHandler();
long order = 0;
String name = "Test";
NettyClientPoolHandler poolHandler = new NettyClientPoolHandler();
AbstractNettyClientPoolHandler poolHandler = new AbstractNettyClientPoolHandler();
HandlerManager.HandlerEntity<ChannelHandler> entity = poolHandler.getHandlerEntity(order, handler, name);
Assert.assertEquals(entity.getName(),name);
Assert.assertEquals(entity.getOrder(),order);
@ -26,7 +26,7 @@ public class NettyClientPoolHandlerTest {
TestHandler handler1 = new TestHandler();
long order1 = 1;
String name1 = "Test1";
NettyClientPoolHandler poolHandler = new NettyClientPoolHandler();
AbstractNettyClientPoolHandler poolHandler = new AbstractNettyClientPoolHandler();
HandlerManager.HandlerEntity<ChannelHandler> entity = poolHandler.getHandlerEntity(order, handler, name);
HandlerManager.HandlerEntity<ChannelHandler> entity1 = poolHandler.getHandlerEntity(order1, handler1, name1);
int compare = entity.compareTo(entity1);
@ -35,7 +35,7 @@ public class NettyClientPoolHandlerTest {
@Test
public void addLast() {
NettyClientPoolHandler handler = new NettyClientPoolHandler();
AbstractNettyClientPoolHandler handler = new AbstractNettyClientPoolHandler();
Assert.assertTrue(handler.isEmpty());
handler.addLast(new TestHandler());
Assert.assertFalse(handler.isEmpty());
@ -43,7 +43,7 @@ public class NettyClientPoolHandlerTest {
@Test
public void addFirst() {
NettyClientPoolHandler handler = new NettyClientPoolHandler();
AbstractNettyClientPoolHandler handler = new AbstractNettyClientPoolHandler();
Assert.assertTrue(handler.isEmpty());
handler.addFirst(new TestHandler());
Assert.assertFalse(handler.isEmpty());
@ -51,7 +51,7 @@ public class NettyClientPoolHandlerTest {
@Test
public void testAddLast() {
NettyClientPoolHandler handler = new NettyClientPoolHandler();
AbstractNettyClientPoolHandler handler = new AbstractNettyClientPoolHandler();
Assert.assertTrue(handler.isEmpty());
handler.addLast("Test", new TestHandler());
Assert.assertFalse(handler.isEmpty());
@ -59,7 +59,7 @@ public class NettyClientPoolHandlerTest {
@Test
public void testAddFirst() {
NettyClientPoolHandler handler = new NettyClientPoolHandler();
AbstractNettyClientPoolHandler handler = new AbstractNettyClientPoolHandler();
Assert.assertTrue(handler.isEmpty());
handler.addFirst("Test", new TestHandler());
Assert.assertFalse(handler.isEmpty());

@ -0,0 +1,43 @@
package cn.hippo4j.rpc.model;
import cn.hippo4j.rpc.discovery.InstanceServerLoaderImpl;
import org.junit.Assert;
import org.junit.Test;
import java.io.*;
import java.lang.reflect.Method;
public class DefaultRequestTest {
@Test
public void testReadObject() throws IOException, ClassNotFoundException, NoSuchMethodException {
String key = "name";
String clsName = InstanceServerLoaderImpl.class.getName();
Method method = InstanceServerLoaderImpl.class.getMethod("setName", String.class);
String methodName = method.getName();
Class<?>[] parameterTypes = method.getParameterTypes();
Object[] parameters = new Object[1];
parameters[0] = "hippo4j";
Request request = new DefaultRequest(key, clsName, methodName, parameterTypes, parameters);
byte[] bytes;
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream outputStream = new ObjectOutputStream(byteArrayOutputStream)) {
outputStream.writeObject(request);
outputStream.flush();
bytes = byteArrayOutputStream.toByteArray();
}
Request request1;
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream)) {
request1 = (Request) objectInputStream.readObject();
}
Assert.assertEquals(request1.hashCode(), request1.hashCode());
Assert.assertEquals(key, request1.getKey());
Assert.assertEquals(clsName, request1.getClassName());
Assert.assertEquals(methodName, request1.getMethodName());
Assert.assertArrayEquals(parameterTypes, request1.getParameterTypes());
Assert.assertArrayEquals(parameters, request1.getParameters());
Assert.assertEquals(request1, request);
}
}

@ -0,0 +1,65 @@
package cn.hippo4j.rpc.model;
import cn.hippo4j.common.web.exception.IllegalException;
import org.junit.Assert;
import org.junit.Test;
import java.io.*;
public class DefaultResponseTest {
@Test
public void testReadObject() throws IOException, ClassNotFoundException {
String key = "name";
Object o = "obj";
Class<?> cls = String.class;
Response response = new DefaultResponse(key, cls, o);
byte[] bytes;
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream outputStream = new ObjectOutputStream(byteArrayOutputStream)) {
outputStream.writeObject(response);
outputStream.flush();
bytes = byteArrayOutputStream.toByteArray();
}
Response response1;
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream)) {
response1 = (Response) objectInputStream.readObject();
}
Assert.assertEquals(response1.hashCode(), response.hashCode());
Assert.assertEquals(key, response1.getKey());
Assert.assertEquals(o, response1.getObj());
Assert.assertEquals(cls, response1.getCls());
Assert.assertEquals(response1, response);
Assert.assertFalse(response1.isErr());
}
@Test
public void testWriteObject() throws IOException, ClassNotFoundException {
String key = "name";
Throwable throwable = new IllegalException("test throwable");
String errMsg = "test throwable";
Response response = new DefaultResponse(key, throwable, errMsg);
byte[] bytes;
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream outputStream = new ObjectOutputStream(byteArrayOutputStream)) {
outputStream.writeObject(response);
outputStream.flush();
bytes = byteArrayOutputStream.toByteArray();
}
Response response1;
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream)) {
response1 = (Response) objectInputStream.readObject();
}
Assert.assertEquals(key, response1.getKey());
Assert.assertThrows(IllegalException.class, () -> {
throw response1.getThrowable();
});
Assert.assertEquals(response1.hashCode(), response.hashCode());
Assert.assertEquals(errMsg, response1.getErrMsg());
Assert.assertEquals(response1, response);
Assert.assertTrue(response1.isErr());
}
}

@ -8,7 +8,7 @@ public class NettyServerConnectionTest {
@Test
public void addLast() {
NettyServerConnection connection = new NettyServerConnection();
AbstractNettyServerConnection connection = new AbstractNettyServerConnection();
Assert.assertTrue(connection.isEmpty());
connection.addLast(new TestHandler());
Assert.assertFalse(connection.isEmpty());
@ -16,7 +16,7 @@ public class NettyServerConnectionTest {
@Test
public void addFirst() {
NettyServerConnection connection = new NettyServerConnection();
AbstractNettyServerConnection connection = new AbstractNettyServerConnection();
Assert.assertTrue(connection.isEmpty());
connection.addFirst(new TestHandler());
Assert.assertFalse(connection.isEmpty());
@ -24,7 +24,7 @@ public class NettyServerConnectionTest {
@Test
public void testAddLast() {
NettyServerConnection connection = new NettyServerConnection();
AbstractNettyServerConnection connection = new AbstractNettyServerConnection();
Assert.assertTrue(connection.isEmpty());
connection.addLast("Test", new TestHandler());
Assert.assertFalse(connection.isEmpty());
@ -32,7 +32,7 @@ public class NettyServerConnectionTest {
@Test
public void testAddFirst() {
NettyServerConnection connection = new NettyServerConnection();
AbstractNettyServerConnection connection = new AbstractNettyServerConnection();
Assert.assertTrue(connection.isEmpty());
connection.addFirst("Test", new TestHandler());
Assert.assertFalse(connection.isEmpty());

@ -38,8 +38,8 @@ public class RPCServerTest {
public void bind() throws IOException {
Instance instance = new DefaultInstance();
NettyServerTakeHandler handler = new NettyServerTakeHandler(instance);
ServerConnection connection = new NettyServerConnection(handler);
RPCServer rpcServer = new RPCServer(port, connection);
ServerConnection connection = new AbstractNettyServerConnection(handler);
RPCServer rpcServer = new RPCServer(connection, port);
CompletableFuture.runAsync(rpcServer::bind);
try {
TimeUnit.SECONDS.sleep(3);
@ -59,8 +59,8 @@ public class RPCServerTest {
EventLoopGroup leader = new NioEventLoopGroup();
EventLoopGroup worker = new NioEventLoopGroup();
NettyServerTakeHandler handler = new NettyServerTakeHandler(instance);
ServerConnection connection = new NettyServerConnection(leader, worker, handler);
RPCServer rpcServer = new RPCServer(port, connection);
ServerConnection connection = new AbstractNettyServerConnection(leader, worker, handler);
RPCServer rpcServer = new RPCServer(connection, port);
CompletableFuture.runAsync(rpcServer::bind);
try {
TimeUnit.SECONDS.sleep(3);

@ -18,7 +18,7 @@
package cn.hippo4j.rpc.support;
import cn.hippo4j.rpc.discovery.ServerPort;
import cn.hippo4j.rpc.handler.NettyClientPoolHandler;
import cn.hippo4j.rpc.handler.AbstractNettyClientPoolHandler;
import cn.hippo4j.rpc.handler.NettyClientTakeHandler;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
@ -38,7 +38,7 @@ public class NettyConnectPoolHolderTest {
@Test
public void createPool() {
NettyClientPoolHandler handler = new NettyClientPoolHandler(new NettyClientTakeHandler());
AbstractNettyClientPoolHandler handler = new AbstractNettyClientPoolHandler(new NettyClientTakeHandler());
NettyConnectPool pool = new NettyConnectPool(host, port, maxCount, timeout, group, cls, handler);
NettyConnectPool connectPool = NettyConnectPoolHolder.getPool(host, port);
Assert.assertEquals(pool, connectPool);
@ -49,7 +49,7 @@ public class NettyConnectPoolHolderTest {
@Test
public void testGetPool() {
NettyClientPoolHandler handler = new NettyClientPoolHandler(new NettyClientTakeHandler());
AbstractNettyClientPoolHandler handler = new AbstractNettyClientPoolHandler(new NettyClientTakeHandler());
NettyConnectPool connectPool = NettyConnectPoolHolder.getPool(host, port, timeout, group, handler);
NettyConnectPool connectPool1 = NettyConnectPoolHolder.getPool(host, port);
Assert.assertEquals(connectPool1, connectPool);
@ -60,7 +60,7 @@ public class NettyConnectPoolHolderTest {
@Test
public void remove() {
NettyClientPoolHandler handler = new NettyClientPoolHandler(new NettyClientTakeHandler());
AbstractNettyClientPoolHandler handler = new AbstractNettyClientPoolHandler(new NettyClientTakeHandler());
NettyConnectPool connectPool = NettyConnectPoolHolder.getPool(host, port, timeout, group, handler);
NettyConnectPool connectPool1 = NettyConnectPoolHolder.getPool(host, port);
Assert.assertEquals(connectPool1, connectPool);

@ -20,10 +20,10 @@ package cn.hippo4j.rpc.support;
import cn.hippo4j.rpc.discovery.DefaultInstance;
import cn.hippo4j.rpc.discovery.Instance;
import cn.hippo4j.rpc.discovery.ServerPort;
import cn.hippo4j.rpc.handler.NettyClientPoolHandler;
import cn.hippo4j.rpc.handler.AbstractNettyClientPoolHandler;
import cn.hippo4j.rpc.handler.NettyClientTakeHandler;
import cn.hippo4j.rpc.handler.NettyServerTakeHandler;
import cn.hippo4j.rpc.server.NettyServerConnection;
import cn.hippo4j.rpc.server.AbstractNettyServerConnection;
import cn.hippo4j.rpc.server.RPCServer;
import cn.hippo4j.rpc.server.ServerConnection;
import io.netty.channel.Channel;
@ -52,8 +52,8 @@ public class NettyConnectPoolTest {
// The mode connection was denied when the server was started on the specified port
Instance instance = new DefaultInstance();
NettyServerTakeHandler handler = new NettyServerTakeHandler(instance);
ServerConnection connection = new NettyServerConnection(handler);
RPCServer rpcServer = new RPCServer(port, connection);
ServerConnection connection = new AbstractNettyServerConnection(handler);
RPCServer rpcServer = new RPCServer(connection, port);
CompletableFuture.runAsync(rpcServer::bind);
// Given the delay in starting the server, wait here
try {
@ -61,7 +61,7 @@ public class NettyConnectPoolTest {
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
NettyClientPoolHandler poolHandler = new NettyClientPoolHandler(new NettyClientTakeHandler());
AbstractNettyClientPoolHandler poolHandler = new AbstractNettyClientPoolHandler(new NettyClientTakeHandler());
NettyConnectPool pool = new NettyConnectPool(host, port, maxCount, timeout, group, cls, poolHandler);
Channel acquire = pool.acquire(timeout);
Assert.assertNotNull(acquire);
@ -74,8 +74,8 @@ public class NettyConnectPoolTest {
// The mode connection was denied when the server was started on the specified port
Instance instance = new DefaultInstance();
NettyServerTakeHandler handler = new NettyServerTakeHandler(instance);
ServerConnection connection = new NettyServerConnection(handler);
RPCServer rpcServer = new RPCServer(port, connection);
ServerConnection connection = new AbstractNettyServerConnection(handler);
RPCServer rpcServer = new RPCServer(connection, port);
CompletableFuture.runAsync(rpcServer::bind);
// Given the delay in starting the server, wait here
try {
@ -83,7 +83,7 @@ public class NettyConnectPoolTest {
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
NettyClientPoolHandler poolHandler = new NettyClientPoolHandler(new NettyClientTakeHandler());
AbstractNettyClientPoolHandler poolHandler = new AbstractNettyClientPoolHandler(new NettyClientTakeHandler());
NettyConnectPool pool = new NettyConnectPool(host, port, maxCount, timeout, group, cls, poolHandler);
Future<Channel> acquire = pool.acquire();
Assert.assertNotNull(acquire);
@ -95,8 +95,8 @@ public class NettyConnectPoolTest {
// The mode connection was denied when the server was started on the specified port
Instance instance = new DefaultInstance();
NettyServerTakeHandler handler = new NettyServerTakeHandler(instance);
ServerConnection connection = new NettyServerConnection(handler);
RPCServer rpcServer = new RPCServer(port, connection);
ServerConnection connection = new AbstractNettyServerConnection(handler);
RPCServer rpcServer = new RPCServer(connection, port);
CompletableFuture.runAsync(rpcServer::bind);
// Given the delay in starting the server, wait here
try {
@ -105,7 +105,7 @@ public class NettyConnectPoolTest {
throw new RuntimeException(e);
}
NettyClientPoolHandler poolHandler = new NettyClientPoolHandler(new NettyClientTakeHandler());
AbstractNettyClientPoolHandler poolHandler = new AbstractNettyClientPoolHandler(new NettyClientTakeHandler());
NettyConnectPool pool = new NettyConnectPool(host, port, maxCount, timeout, group, cls, poolHandler);
Channel acquire = pool.acquire(timeout);
Assert.assertNotNull(acquire);

@ -20,7 +20,7 @@ package cn.hippo4j.rpc.support;
import cn.hippo4j.common.web.exception.IllegalException;
import cn.hippo4j.rpc.discovery.ServerPort;
import cn.hippo4j.rpc.exception.ConnectionException;
import cn.hippo4j.rpc.handler.NettyClientPoolHandler;
import cn.hippo4j.rpc.handler.AbstractNettyClientPoolHandler;
import cn.hippo4j.rpc.handler.NettyClientTakeHandler;
import org.junit.Assert;
import org.junit.Test;
@ -31,21 +31,21 @@ public class NettyProxyCenterTest {
@Test
public void getProxy() {
NettyClientPoolHandler handler = new NettyClientPoolHandler(new NettyClientTakeHandler());
AbstractNettyClientPoolHandler handler = new AbstractNettyClientPoolHandler(new NettyClientTakeHandler());
ProxyInterface localhost = NettyProxyCenter.getProxy(ProxyInterface.class, "localhost", port, handler);
Assert.assertNotNull(localhost);
}
@Test(expected = IllegalException.class)
public void getProxyTest() {
NettyClientPoolHandler handler = new NettyClientPoolHandler(new NettyClientTakeHandler());
AbstractNettyClientPoolHandler handler = new AbstractNettyClientPoolHandler(new NettyClientTakeHandler());
ProxyClass localhost = NettyProxyCenter.getProxy(ProxyClass.class, "localhost", port, handler);
Assert.assertNotNull(localhost);
}
@Test(expected = ConnectionException.class)
public void getProxyTestCall() {
NettyClientPoolHandler handler = new NettyClientPoolHandler(new NettyClientTakeHandler());
AbstractNettyClientPoolHandler handler = new AbstractNettyClientPoolHandler(new NettyClientTakeHandler());
ProxyInterface localhost = NettyProxyCenter.getProxy(ProxyInterface.class, "localhost", port, handler);
localhost.hello();
Assert.assertNotNull(localhost);

@ -0,0 +1,27 @@
package cn.hippo4j.rpc.support;
import cn.hippo4j.rpc.discovery.InstanceServerLoader;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
public class NettyServerSupportTest {
@Test
public void bind() throws IOException {
NettyServerSupport support = new NettyServerSupport(() -> 8888, InstanceServerLoader.class);
CompletableFuture.runAsync(support::bind);
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Assert.assertTrue(support.isActive());
support.close();
Assert.assertFalse(support.isActive());
}
}
Loading…
Cancel
Save