Elffuss BraidHub

kikocis/java-driver public

Don't rely on time in test for cluster init optimization (JAVA-522).
Changeset4bcb1e4dc5856648ecca2657c99dd95cb42b80b112c4c16500972599336b8e9f
Authorolim7t <omichallat+github@gmail.com> · 2014-11-03 20:22 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2014-11-03 20:22 +01:00
Parentsc25aa88069e8

Browse the files at this changeset

1 file changed

modified driver-core/src/test/java/com/datastax/driver/core/ClusterInitTest.java +23 −9
@@ -5,13 +5,19 @@
55 import java.util.concurrent.TimeUnit;
66
77 import com.google.common.collect.Lists;
8+import org.mockito.Mockito;
89 import org.slf4j.Logger;
910 import org.slf4j.LoggerFactory;
1011 import org.testng.annotations.Test;
1112
1213 import static org.assertj.core.api.Assertions.assertThat;
1314 import static org.assertj.core.api.Assertions.fail;
14-
15+import static org.mockito.Mockito.atMost;
16+import static org.mockito.Mockito.spy;
17+import static org.mockito.Mockito.times;
18+import static org.mockito.Mockito.verify;
19+
20+import com.datastax.driver.core.policies.ConstantReconnectionPolicy;
1521 import com.datastax.driver.core.utils.UUIDs;
1622
1723 import static com.datastax.driver.core.FakeHost.Behavior.THROWING_CONNECT_TIMEOUTS;
@@ -50,20 +56,28 @@
5056 logger.info("Environment is set up, starting test");
5157 long start = System.nanoTime();
5258
59+ // We want to count how many connections were attempted. For that, we rely on the fact that SocketOptions.getKeepAlive is called in Connection.Factory.newBoostrap()
60+ // each time we prepare to open a new connection. This is a bit of a hack, but this is what we have.
61+ SocketOptions socketOptions = spy(new SocketOptions());
62+
63+ // Set an enormous delay so that reconnection attempts don't pollute our observations
64+ ConstantReconnectionPolicy reconnectionPolicy = new ConstantReconnectionPolicy(3600 * 1000);
65+
5366 cluster = Cluster.builder().addContactPoints(
5467 CCMBridge.ipOfNode(1), CCMBridge.ipOfNode(2), CCMBridge.ipOfNode(3),
55- CCMBridge.ipOfNode(4), CCMBridge.ipOfNode(5), CCMBridge.ipOfNode(6)
56- ).build();
68+ CCMBridge.ipOfNode(4), CCMBridge.ipOfNode(5), CCMBridge.ipOfNode(6))
69+ .withSocketOptions(socketOptions)
70+ .withReconnectionPolicy(reconnectionPolicy)
71+ .build();
5772 cluster.connect();
5873
74+ // For information only:
5975 long initTimeMs = TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS);
60-
61- // It's hard to estimate how much time init should take. But at the very least, we should wait at most
62- // once for each of the 5 failing hosts, so 6 times the connect timeout is a reasonable upper bound.
63- int expected = cluster.getConfiguration().getSocketOptions().getConnectTimeoutMillis() * 6;
64-
6576 logger.info("Cluster and session initialized in {} ms", initTimeMs);
66- assertThat(initTimeMs).isLessThan(expected);
77+
78+ // We have one live host so 3 successful connections (1 control connection and 2 core connections in the pool).
79+ // The other 5 hosts are unreachable, we should attempt to connect to each of them only once.
80+ verify(socketOptions, times(3 + 5)).getKeepAlive();
6781 } finally {
6882 if (cluster != null)
6983 cluster.close();