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();
}
}
}
Aggregations