Search in sources :

Example 6 with ServerSideConnection

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()));
}
Also used : ChannelEventListener(herddb.network.ChannelEventListener) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) InetSocketAddress(java.net.InetSocketAddress) Channel(herddb.network.Channel) ExecutorService(java.util.concurrent.ExecutorService) ServerSideConnection(herddb.network.ServerSideConnection) Test(org.junit.Test)

Example 7 with ServerSideConnection

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()));
}
Also used : Pdu(herddb.proto.Pdu) InetSocketAddress(java.net.InetSocketAddress) Channel(herddb.network.Channel) ServerSideConnection(herddb.network.ServerSideConnection) ByteBuf(io.netty.buffer.ByteBuf) ChannelEventListener(herddb.network.ChannelEventListener) Random(java.util.Random) ExecutorService(java.util.concurrent.ExecutorService) TimeoutException(java.util.concurrent.TimeoutException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Aggregations

Channel (herddb.network.Channel)7 ChannelEventListener (herddb.network.ChannelEventListener)7 ServerSideConnection (herddb.network.ServerSideConnection)7 Random (java.util.Random)7 ExecutorService (java.util.concurrent.ExecutorService)7 Test (org.junit.Test)7 Pdu (herddb.proto.Pdu)5 ByteBuf (io.netty.buffer.ByteBuf)5 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)4 InetSocketAddress (java.net.InetSocketAddress)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 TimeoutException (java.util.concurrent.TimeoutException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)1