mirror of https://github.com/longtai-cn/hippo4j
Adjust the directory structure and add the supporting classes on the server side (#946)
* fix : Adjust the directory structure and add the supporting classes on the server side * fix : test for rpc handler and server side * fix : update test for rpcpull/950/head
parent
344f629c74
commit
31bbd1efa4
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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 cn.hippo4j.rpc.discovery;
|
||||
|
||||
/**
|
||||
* Gets the top-level interface of the instance port
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface ServerPort {
|
||||
|
||||
/**
|
||||
* Gets the listening or exposed port
|
||||
*
|
||||
* @return port
|
||||
*/
|
||||
int getPort();
|
||||
|
||||
}
|
@ -0,0 +1,113 @@
|
||||
/*
|
||||
* 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 cn.hippo4j.rpc.support;
|
||||
|
||||
import cn.hippo4j.rpc.discovery.ClassRegistry;
|
||||
import cn.hippo4j.rpc.discovery.DefaultInstance;
|
||||
import cn.hippo4j.rpc.discovery.ServerPort;
|
||||
import cn.hippo4j.rpc.handler.HandlerManager;
|
||||
import cn.hippo4j.rpc.handler.NettyServerTakeHandler;
|
||||
import cn.hippo4j.rpc.server.AbstractNettyServerConnection;
|
||||
import cn.hippo4j.rpc.server.RPCServer;
|
||||
import cn.hippo4j.rpc.server.Server;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This is a server-side build class that allows you to quickly prepare data on the server side and start the server side.<br>
|
||||
* <p>
|
||||
* The composite pattern is adopted, which means that it is itself a server-side implementation, so it is stateless.
|
||||
*/
|
||||
public class NettyServerSupport implements Server {
|
||||
|
||||
/**
|
||||
* The interface that the server side can call,
|
||||
* All the methods in the interface are brokered during initialization
|
||||
*/
|
||||
List<Class<?>> classes;
|
||||
|
||||
/**
|
||||
* Extract the port number of the web container,
|
||||
* which is the port information exposed by the server
|
||||
*/
|
||||
ServerPort serverPort;
|
||||
|
||||
/**
|
||||
* ChannelHandler
|
||||
*/
|
||||
HandlerManager<ChannelHandler> handlerManager;
|
||||
|
||||
Server server;
|
||||
|
||||
public NettyServerSupport(ServerPort serverPort, Class<?>... classes) {
|
||||
this(serverPort, new AbstractNettyServerConnection(), classes);
|
||||
}
|
||||
|
||||
public NettyServerSupport(ServerPort serverPort, List<Class<?>> classes) {
|
||||
this(serverPort, new AbstractNettyServerConnection(), classes);
|
||||
}
|
||||
|
||||
public NettyServerSupport(ServerPort serverPort, HandlerManager<ChannelHandler> handlerManager, Class<?>... classes) {
|
||||
this(serverPort, handlerManager, classes != null ? Arrays.asList(classes) : Collections.emptyList());
|
||||
}
|
||||
|
||||
public NettyServerSupport(ServerPort serverPort, HandlerManager<ChannelHandler> handlerManager, List<Class<?>> classes) {
|
||||
this.classes = classes;
|
||||
this.serverPort = serverPort;
|
||||
this.handlerManager = handlerManager;
|
||||
initServer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the entire server side, which includes interface registration, processors, and ports.<br>
|
||||
* Only interfaces are registered during registration. Classes and abstract classes are not registered.
|
||||
* If no processor is available, a default processor is provided
|
||||
*/
|
||||
private void initServer() {
|
||||
// Register the interface that can be invoked
|
||||
classes.stream().filter(Class::isInterface)
|
||||
.forEach(cls -> ClassRegistry.put(cls.getName(), cls));
|
||||
AbstractNettyServerConnection connection = (handlerManager instanceof AbstractNettyServerConnection)
|
||||
? (AbstractNettyServerConnection) handlerManager
|
||||
: new AbstractNettyServerConnection();
|
||||
// Assign a default handler if no handler exists
|
||||
if (connection.isEmpty()) {
|
||||
connection.addLast(new NettyServerTakeHandler(new DefaultInstance()));
|
||||
}
|
||||
server = new RPCServer(connection, serverPort);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bind() {
|
||||
server.bind();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActive() {
|
||||
return server.isActive();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
server.close();
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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 cn.hippo4j.rpc.discovery;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.test.context.TestComponent;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@TestComponent
|
||||
public class InstanceModel {
|
||||
|
||||
String name;
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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 cn.hippo4j.rpc.discovery;
|
||||
|
||||
public interface InstanceServerLoader {
|
||||
|
||||
String getName();
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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 cn.hippo4j.rpc.discovery;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class InstanceServerLoaderImpl implements InstanceServerLoader {
|
||||
|
||||
String name = "name";
|
||||
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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 cn.hippo4j.rpc.discovery;
|
||||
|
||||
import cn.hippo4j.common.config.ApplicationContextHolder;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = {InstanceModel.class, ApplicationContextHolder.class})
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class SpringContextInstanceTest {
|
||||
|
||||
Instance instance = new SpringContextInstance();
|
||||
|
||||
@Test
|
||||
public void getInstance() {
|
||||
Object obj = instance.getInstance(InstanceModel.class);
|
||||
Assert.assertNotNull(obj);
|
||||
Assert.assertEquals(obj.getClass(), InstanceModel.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetInstance() {
|
||||
Object obj = instance.getInstance("instanceModel");
|
||||
Assert.assertNotNull(obj);
|
||||
Assert.assertEquals(obj.getClass(), InstanceModel.class);
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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 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.io.IOException;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class ConnectHandlerTest {
|
||||
|
||||
@Test
|
||||
public void handlerTest() throws IOException {
|
||||
// server
|
||||
Class<InstanceServerLoader> cls = InstanceServerLoader.class;
|
||||
ClassRegistry.put(cls.getName(), cls);
|
||||
ServerPort port = () -> 8891;
|
||||
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);
|
||||
rpcClient.close();
|
||||
rpcServer.close();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* 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 cn.hippo4j.rpc.handler;
|
||||
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class NettyClientPoolHandlerTest {
|
||||
|
||||
@Test
|
||||
public void testGetHandlerEntity() {
|
||||
TestHandler handler = new TestHandler();
|
||||
long order = 0;
|
||||
String name = "Test";
|
||||
AbstractNettyClientPoolHandler poolHandler = new AbstractNettyClientPoolHandler();
|
||||
HandlerManager.HandlerEntity<ChannelHandler> entity = poolHandler.getHandlerEntity(order, handler, name);
|
||||
Assert.assertEquals(entity.getName(), name);
|
||||
Assert.assertEquals(entity.getOrder(), order);
|
||||
Assert.assertEquals(entity.getHandler(), handler);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompareTo() {
|
||||
TestHandler handler = new TestHandler();
|
||||
long order = 0;
|
||||
String name = "Test";
|
||||
TestHandler handler1 = new TestHandler();
|
||||
long order1 = 1;
|
||||
String name1 = "Test1";
|
||||
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);
|
||||
Assert.assertTrue(compare < 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addLast() {
|
||||
AbstractNettyClientPoolHandler handler = new AbstractNettyClientPoolHandler();
|
||||
Assert.assertTrue(handler.isEmpty());
|
||||
handler.addLast(new TestHandler());
|
||||
Assert.assertFalse(handler.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addFirst() {
|
||||
AbstractNettyClientPoolHandler handler = new AbstractNettyClientPoolHandler();
|
||||
Assert.assertTrue(handler.isEmpty());
|
||||
handler.addFirst(new TestHandler());
|
||||
Assert.assertFalse(handler.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddLast() {
|
||||
AbstractNettyClientPoolHandler handler = new AbstractNettyClientPoolHandler();
|
||||
Assert.assertTrue(handler.isEmpty());
|
||||
handler.addLast("Test", new TestHandler());
|
||||
Assert.assertFalse(handler.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddFirst() {
|
||||
AbstractNettyClientPoolHandler handler = new AbstractNettyClientPoolHandler();
|
||||
Assert.assertTrue(handler.isEmpty());
|
||||
handler.addFirst("Test", new TestHandler());
|
||||
Assert.assertFalse(handler.isEmpty());
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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 cn.hippo4j.rpc.handler;
|
||||
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
|
||||
public class TestHandler implements ChannelHandler {
|
||||
|
||||
@Override
|
||||
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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 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,86 @@
|
||||
/*
|
||||
* 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 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,57 @@
|
||||
/*
|
||||
* 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 cn.hippo4j.rpc.server;
|
||||
|
||||
import cn.hippo4j.rpc.handler.TestHandler;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class NettyServerConnectionTest {
|
||||
|
||||
@Test
|
||||
public void addLast() {
|
||||
AbstractNettyServerConnection connection = new AbstractNettyServerConnection();
|
||||
Assert.assertTrue(connection.isEmpty());
|
||||
connection.addLast(new TestHandler());
|
||||
Assert.assertFalse(connection.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addFirst() {
|
||||
AbstractNettyServerConnection connection = new AbstractNettyServerConnection();
|
||||
Assert.assertTrue(connection.isEmpty());
|
||||
connection.addFirst(new TestHandler());
|
||||
Assert.assertFalse(connection.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddLast() {
|
||||
AbstractNettyServerConnection connection = new AbstractNettyServerConnection();
|
||||
Assert.assertTrue(connection.isEmpty());
|
||||
connection.addLast("Test", new TestHandler());
|
||||
Assert.assertFalse(connection.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddFirst() {
|
||||
AbstractNettyServerConnection connection = new AbstractNettyServerConnection();
|
||||
Assert.assertTrue(connection.isEmpty());
|
||||
connection.addFirst("Test", new TestHandler());
|
||||
Assert.assertFalse(connection.isEmpty());
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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 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(() -> 8890, 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());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1 @@
|
||||
cn.hippo4j.rpc.discovery.InstanceServerLoaderImpl
|
Loading…
Reference in new issue