Search in sources :

Example 1 with NettyChannel

use of herddb.network.netty.NettyChannel in project herddb by diennea.

the class SimpleClientServerTest method testEnsureOpen.

@Test
public void testEnsureOpen() throws Exception {
    Path baseDir = folder.newFolder().toPath();
    AtomicReference<ClientSideConnectionPeer[]> connections = new AtomicReference<>();
    ServerConfiguration serverConfiguration = newServerConfigurationWithAutoPort(baseDir);
    try (Server server = new Server(newServerConfigurationWithAutoPort(baseDir))) {
        server.getNetworkServer().setEnableJVMNetwork(false);
        server.getNetworkServer().setEnableRealNetwork(true);
        server.start();
        server.waitForStandaloneBoot();
        ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
        clientConfiguration.set(ClientConfiguration.PROPERTY_MAX_CONNECTIONS_PER_SERVER, 1);
        clientConfiguration.set(ClientConfiguration.PROPERTY_TIMEOUT, 2000);
        try (HDBClient client = new HDBClient(clientConfiguration) {

            @Override
            public HDBConnection openConnection() {
                HDBConnection con = new HDBConnection(this) {

                    @Override
                    protected ClientSideConnectionPeer chooseConnection(ClientSideConnectionPeer[] all) {
                        connections.set(all);
                        return all[0];
                    }
                };
                registerConnection(con);
                return con;
            }
        };
            HDBConnection connection = client.openConnection()) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            assertTrue(connection.waitForTableSpace(TableSpace.DEFAULT, Integer.MAX_VALUE));
            long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id int primary key, s1 string)", 0, false, true, Collections.emptyList()).updateCount;
            Assert.assertEquals(1, resultCreateTable);
            assertEquals(1, connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", 0, false, true, Arrays.asList(1, "test1")).updateCount);
            // assert we are using real network
            assertNotEquals(NettyChannel.ADDRESS_JVM_LOCAL, connections.get()[0].getChannel().getRemoteAddress());
            io.netty.channel.Channel socket = ((NettyChannel) connections.get()[0].getChannel()).getSocket();
            assertThat(socket, instanceOf(SocketChannel.class));
            // invalidate socket connection (simulate network error)
            socket.close().await();
            // ensure reconnection is performed (using prepared statement)
            connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable ", true, Collections.emptyList(), 0, 100, 1000, true).close();
            // assert we are using real network
            assertNotEquals(NettyChannel.ADDRESS_JVM_LOCAL, connections.get()[0].getChannel().getRemoteAddress());
            io.netty.channel.Channel socket2 = ((NettyChannel) connections.get()[0].getChannel()).getSocket();
            assertThat(socket2, instanceOf(SocketChannel.class));
            assertNotSame(socket2, socket);
            // invalidate socket connection (simulate network error)
            socket2.close().await();
            // ensure reconnection is performed (not using prepared statement)
            connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable ", false, Collections.emptyList(), 0, 100, 1000, true).close();
        }
    }
}
Also used : Path(java.nio.file.Path) SocketChannel(io.netty.channel.socket.SocketChannel) Server(herddb.server.Server) ServerConfiguration(herddb.server.ServerConfiguration) AtomicReference(java.util.concurrent.atomic.AtomicReference) StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) NettyChannel(herddb.network.netty.NettyChannel) Test(org.junit.Test)

Aggregations

NettyChannel (herddb.network.netty.NettyChannel)1 Server (herddb.server.Server)1 ServerConfiguration (herddb.server.ServerConfiguration)1 StaticClientSideMetadataProvider (herddb.server.StaticClientSideMetadataProvider)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 Path (java.nio.file.Path)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1