use of herddb.network.ServerSideConnection in project herddb by diennea.
the class LocalChannelTest method test.
@Test
public void test() throws Exception {
InetSocketAddress addr = new InetSocketAddress("localhost", NetworkUtils.assignFirstFreePort());
try (NettyChannelAcceptor acceptor = new NettyChannelAcceptor(addr.getHostName(), addr.getPort(), true)) {
acceptor.setEnableRealNetwork(false);
acceptor.setAcceptor((Channel channel) -> {
channel.setMessagesReceiver(new ChannelEventListener() {
@Override
public void requestReceived(Pdu message, Channel channel) {
ByteBuf msg = buildAckResponse(message);
channel.sendReplyMessage(message.messageId, msg);
message.close();
}
@Override
public void channelClosed(Channel channel) {
}
});
return (ServerSideConnection) () -> new Random().nextLong();
});
acceptor.start();
assertNotNull(LocalServerRegistry.getLocalServer(NetworkUtils.getAddress(addr), addr.getPort()));
ExecutorService executor = Executors.newCachedThreadPool();
try (Channel client = NettyConnector.connect(addr.getHostName(), addr.getPort(), true, 0, 0, new ChannelEventListener() {
@Override
public void channelClosed(Channel channel) {
System.out.println("client channelClosed");
}
}, executor, null)) {
for (int i = 0; i < 100; i++) {
ByteBuf buffer = buildAckRequest(i);
try (Pdu result = client.sendMessageWithPduReply(i, buffer, 10000)) {
assertEquals(Pdu.TYPE_ACK, result.type);
}
}
} finally {
executor.shutdown();
}
}
assertNull(LocalServerRegistry.getLocalServer(NetworkUtils.getAddress(addr), addr.getPort()));
}
use of herddb.network.ServerSideConnection in project herddb by diennea.
the class ChannelBenchTest method test.
@Test
public void test() throws Exception {
try (NettyChannelAcceptor acceptor = new NettyChannelAcceptor("localhost", NetworkUtils.assignFirstFreePort(), false)) {
acceptor.setAcceptor((Channel channel) -> {
channel.setMessagesReceiver(new ChannelEventListener() {
@Override
public void requestReceived(Pdu message, Channel channel) {
ByteBuf msg = buildAckResponse(message);
channel.sendReplyMessage(message.messageId, msg);
message.close();
}
@Override
public void channelClosed(Channel channel) {
}
});
return (ServerSideConnection) () -> new Random().nextLong();
});
acceptor.start();
ExecutorService executor = Executors.newCachedThreadPool();
try (Channel client = NettyConnector.connect(acceptor.getHost(), acceptor.getPort(), acceptor.isSsl(), 0, 0, new ChannelEventListener() {
@Override
public void channelClosed(Channel channel) {
System.out.println("client channelClosed");
}
}, executor, new NioEventLoopGroup(10, executor))) {
for (int i = 0; i < 100; i++) {
ByteBuf buffer = buildAckRequest(i);
try (Pdu result = client.sendMessageWithPduReply(i, buffer, 10000)) {
assertEquals(Pdu.TYPE_ACK, result.type);
}
}
} finally {
executor.shutdown();
}
}
}
use of herddb.network.ServerSideConnection in project herddb by diennea.
the class NetworkChannelTest method test.
@Test
public void test() throws Exception {
try (NettyChannelAcceptor acceptor = new NettyChannelAcceptor("localhost", NetworkUtils.assignFirstFreePort(), true)) {
acceptor.setEnableJVMNetwork(false);
acceptor.setAcceptor((Channel channel) -> {
channel.setMessagesReceiver(new ChannelEventListener() {
@Override
public void requestReceived(Pdu message, Channel channel) {
ByteBuf msg = buildAckResponse(message);
channel.sendReplyMessage(message.messageId, msg);
message.close();
}
@Override
public void channelClosed(Channel channel) {
}
});
return (ServerSideConnection) () -> new Random().nextLong();
});
acceptor.start();
ExecutorService executor = Executors.newCachedThreadPool();
try (Channel client = NettyConnector.connect(acceptor.getHost(), acceptor.getPort(), true, 0, 0, new ChannelEventListener() {
@Override
public void channelClosed(Channel channel) {
System.out.println("client channelClosed");
}
}, executor, new NioEventLoopGroup(10, executor))) {
for (int i = 0; i < 100; i++) {
ByteBuf buffer = buildAckRequest(i);
try (Pdu result = client.sendMessageWithPduReply(i, Unpooled.wrappedBuffer(buffer), 10000)) {
assertEquals(Pdu.TYPE_ACK, result.type);
}
}
} finally {
executor.shutdown();
}
}
if (NetworkUtils.isEnableEpoolNative()) {
try (NettyChannelAcceptor acceptor = new NettyChannelAcceptor("localhost", NetworkUtils.assignFirstFreePort(), true)) {
acceptor.setEnableJVMNetwork(false);
acceptor.setAcceptor((Channel channel) -> {
channel.setMessagesReceiver(new ChannelEventListener() {
@Override
public void requestReceived(Pdu message, Channel channel) {
ByteBuf msg = buildAckResponse(message);
channel.sendReplyMessage(message.messageId, msg);
message.close();
}
@Override
public void channelClosed(Channel channel) {
}
});
return (ServerSideConnection) () -> new Random().nextLong();
});
acceptor.start();
ExecutorService executor = Executors.newCachedThreadPool();
try (Channel client = NettyConnector.connect(acceptor.getHost(), acceptor.getPort(), true, 0, 0, new ChannelEventListener() {
@Override
public void channelClosed(Channel channel) {
System.out.println("client channelClosed");
}
}, executor, new EpollEventLoopGroup(10, executor))) {
for (int i = 0; i < 100; i++) {
ByteBuf buffer = buildAckRequest(i);
try (Pdu result = client.sendMessageWithPduReply(i, Unpooled.wrappedBuffer(buffer), 10000)) {
assertEquals(Pdu.TYPE_ACK, result.type);
}
}
} finally {
executor.shutdown();
}
}
}
}
use of herddb.network.ServerSideConnection in project herddb by diennea.
the class NetworkChannelTest method testServerPushesData.
@Test
public void testServerPushesData() throws Exception {
try (NettyChannelAcceptor acceptor = new NettyChannelAcceptor("localhost", NetworkUtils.assignFirstFreePort(), true)) {
acceptor.setEnableJVMNetwork(false);
acceptor.setEnableRealNetwork(true);
acceptor.setAcceptor((Channel channel) -> {
channel.setMessagesReceiver(new ChannelEventListener() {
@Override
public void requestReceived(Pdu message, Channel channel) {
try {
ByteBuf msg = buildAckResponse(message);
// send a message to the client
ByteBuf buffer = buildAckRequest(900);
Pdu response = channel.sendMessageWithPduReply(900, buffer, 10000);
assertEquals(Pdu.TYPE_ACK, response.type);
// send the response to the client
channel.sendReplyMessage(message.messageId, msg);
message.close();
} catch (InterruptedException ex) {
ex.printStackTrace();
} catch (TimeoutException ex) {
ex.printStackTrace();
}
}
@Override
public void channelClosed(Channel channel) {
}
});
return (ServerSideConnection) () -> new Random().nextLong();
});
acceptor.start();
ExecutorService executor = Executors.newCachedThreadPool();
CopyOnWriteArrayList<Long> pushedMessagesFromServer = new CopyOnWriteArrayList<>();
try (Channel client = NettyConnector.connect(acceptor.getHost(), acceptor.getPort(), true, 0, 0, new ChannelEventListener() {
@Override
public void requestReceived(Pdu pdu, Channel channel) {
pushedMessagesFromServer.add(pdu.messageId);
assertTrue(pdu.isRequest());
ByteBuf msg = buildAckResponse(pdu);
// send the response to the server
channel.sendReplyMessage(pdu.messageId, msg);
pdu.close();
}
@Override
public void channelClosed(Channel channel) {
System.out.println("client channelClosed");
}
}, executor, new NioEventLoopGroup(10, executor))) {
ByteBuf buffer = buildAckRequest(134);
try (Pdu result = client.sendMessageWithPduReply(134, buffer, 10000)) {
assertEquals(Pdu.TYPE_ACK, result.type);
}
assertEquals(1, pushedMessagesFromServer.size());
} finally {
executor.shutdown();
}
}
}
use of herddb.network.ServerSideConnection in project herddb by diennea.
the class NetworkChannelTest method testCloseServer.
@Test
public void testCloseServer() throws Exception {
try (NettyChannelAcceptor server = new NettyChannelAcceptor("localhost", NetworkUtils.assignFirstFreePort(), true)) {
server.setEnableJVMNetwork(false);
server.setEnableRealNetwork(true);
server.setAcceptor((Channel channel) -> {
channel.setMessagesReceiver(new ChannelEventListener() {
});
return (ServerSideConnection) () -> new Random().nextLong();
});
server.start();
ExecutorService executor = Executors.newCachedThreadPool();
AtomicBoolean closeNotificationReceived = new AtomicBoolean();
try (Channel client = NettyConnector.connect(server.getHost(), server.getPort(), true, 0, 0, new ChannelEventListener() {
@Override
public void channelClosed(Channel channel) {
System.out.println("client channelClosed");
closeNotificationReceived.set(true);
}
}, executor, new NioEventLoopGroup(10, executor))) {
// closing the server should eventually close the client
server.close();
TestUtils.waitForCondition(() -> closeNotificationReceived.get(), NOOP, 100);
assertTrue(closeNotificationReceived.get());
} finally {
executor.shutdown();
}
}
}
Aggregations