kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 4bcb1e4dc5856648ecca2657c99dd95cb42b80b112c4c16500972599336b8e9f |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-11-03 20:22 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-11-03 20:22 +01:00 |
| Parents | c25aa88069e8 |
1 file changed
modified driver-core/src/test/java/com/datastax/driver/core/ClusterInitTest.java +23 −9
| @@ -5,13 +5,19 @@ | ||
| 5 | 5 | import java.util.concurrent.TimeUnit; |
| 6 | 6 | |
| 7 | 7 | import com.google.common.collect.Lists; |
| 8 | +import org.mockito.Mockito; | |
| 8 | 9 | import org.slf4j.Logger; |
| 9 | 10 | import org.slf4j.LoggerFactory; |
| 10 | 11 | import org.testng.annotations.Test; |
| 11 | 12 | |
| 12 | 13 | import static org.assertj.core.api.Assertions.assertThat; |
| 13 | 14 | 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; | |
| 15 | 21 | import com.datastax.driver.core.utils.UUIDs; |
| 16 | 22 | |
| 17 | 23 | import static com.datastax.driver.core.FakeHost.Behavior.THROWING_CONNECT_TIMEOUTS; |
| @@ -50,20 +56,28 @@ | ||
| 50 | 56 | logger.info("Environment is set up, starting test"); |
| 51 | 57 | long start = System.nanoTime(); |
| 52 | 58 | |
| 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 | + | |
| 53 | 66 | cluster = Cluster.builder().addContactPoints( |
| 54 | 67 | 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(); | |
| 57 | 72 | cluster.connect(); |
| 58 | 73 | |
| 74 | + // For information only: | |
| 59 | 75 | 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 | - | |
| 65 | 76 | 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(); | |
| 67 | 81 | } finally { |
| 68 | 82 | if (cluster != null) |
| 69 | 83 | cluster.close(); |