use of herddb.network.ServerSideConnection in project herddb by diennea.
the class LocalChannelTest method testCloseServer.
@Test
public void testCloseServer() throws Exception {
InetSocketAddress addr = new InetSocketAddress("localhost", NetworkUtils.assignFirstFreePort());
try (NettyChannelAcceptor server = new NettyChannelAcceptor(addr.getHostName(), addr.getPort(), true)) {
server.setEnableRealNetwork(false);
server.setAcceptor((Channel channel) -> {
channel.setMessagesReceiver(new ChannelEventListener() {
});
return (ServerSideConnection) () -> new Random().nextLong();
});
server.start();
assertNotNull(LocalServerRegistry.getLocalServer(NetworkUtils.getAddress(addr), addr.getPort()));
ExecutorService executor = Executors.newCachedThreadPool();
AtomicBoolean closeNotificationReceived = new AtomicBoolean();
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");
closeNotificationReceived.set(true);
}
}, executor, null)) {
// closing the server should close the client
server.close();
assertTrue(client.isClosed());
TestUtils.waitForCondition(() -> closeNotificationReceived.get(), NOOP, 100);
} finally {
executor.shutdown();
}
}
assertNull(LocalServerRegistry.getLocalServer(NetworkUtils.getAddress(addr), addr.getPort()));
}
use of herddb.network.ServerSideConnection in project herddb by diennea.
the class LocalChannelTest method testServerPushesData.
@Test
public void testServerPushesData() 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) {
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();
assertNotNull(LocalServerRegistry.getLocalServer(NetworkUtils.getAddress(addr), addr.getPort()));
ExecutorService executor = Executors.newCachedThreadPool();
CopyOnWriteArrayList<Long> pushedMessagesFromServer = new CopyOnWriteArrayList<>();
try (Channel client = NettyConnector.connect(addr.getHostName(), addr.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, null)) {
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();
}
}
assertNull(LocalServerRegistry.getLocalServer(NetworkUtils.getAddress(addr), addr.getPort()));
}
Aggregations