use of com.mysql.cj.xdevapi.Client in project aws-mysql-jdbc by awslabs.
the class SessionTest method pooledSessionFailoverRandomSortAndPooling.
@Test
public void pooledSessionFailoverRandomSortAndPooling() {
UnreliableSocketFactory.flushAllStaticData();
final String testUriPattern = "mysqlx://%s:%s@[%s]/%s?" + PropertyKey.xdevapiConnectTimeout.getKeyName() + "=100&" + PropertyKey.socketFactory.getKeyName() + "=" + UnreliableSocketFactory.class.getName();
String testHosts = IntStream.range(1, 6).mapToObj(i -> "host" + i).peek(h -> UnreliableSocketFactory.mapHost(h, getTestHost())).map(h -> h + ":" + getTestPort()).collect(Collectors.joining(","));
String testUri = String.format(testUriPattern, getTestUser() == null ? "" : getTestUser(), getTestPassword() == null ? "" : getTestPassword(), testHosts, getTestDatabase());
final ClientFactory cf = new ClientFactory();
Client client = cf.getClient(testUri, "{\"pooling\" : {\"enabled\" : true, \"maxSize\" : 10} }");
Set<String> downHosts = new HashSet<>();
for (int i = 1; i <= 5; i++) {
Session testSession = client.getSession();
assertTrue(UnreliableSocketFactory.isConnected());
// Pool this connection.
testSession.close();
String lastHost = UnreliableSocketFactory.getHostFromLastConnection();
for (String h : downHosts) {
assertNotEquals(h, lastHost);
}
downHosts.add(lastHost);
UnreliableSocketFactory.downHost(lastHost.substring(1));
UnreliableSocketFactory.flushConnectionAttempts();
}
// None of the hosts is available by now.
assertThrows(CJCommunicationsException.class, "Unable to connect to any of the target hosts\\.", () -> {
client.getSession();
return null;
});
UnreliableSocketFactory.dontDownHost("host3");
UnreliableSocketFactory.flushConnectionAttempts();
Session testSession = client.getSession();
assertTrue(UnreliableSocketFactory.isConnected());
testSession.close();
assertEquals(UnreliableSocketFactory.getHostConnectedStatus("host3"), UnreliableSocketFactory.getHostFromLastConnection());
UnreliableSocketFactory.flushConnectionAttempts();
// Pick previous connection from the pool. Doesn't count as new connections.
Session testSession1 = client.getSession();
assertEquals(0, UnreliableSocketFactory.getHostsFromAllConnections().size());
// Create a new connection.
Session testSession2 = client.getSession();
assertTrue(UnreliableSocketFactory.isConnected());
testSession1.close();
testSession2.close();
assertEquals(UnreliableSocketFactory.getHostConnectedStatus("host3"), UnreliableSocketFactory.getHostFromLastConnection());
client.close();
}
Aggregations