mirror of https://github.com/longtai-cn/hippo4j
parent
433df608e2
commit
5c85225133
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
@ -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…
Reference in new issue