Elffuss BraidHub

kikocis/java-driver public

Explicitly close clusters between tests

Motivation:

Some tests require that other Cluster instances not be running while
executing.

Usually one could use register(Cluster) to indicate that the Cluster
should be closed when the Test class completes.

In HeartbeatTest, we need to close cluster instances after each test
completes as these tests check log entries for heartbeat messages, which
may be sent on previous clusters during the test since they aren't doing
anything.

LoadBalancingPolicyTest also needs to close its instances as well.

Modification:

Close cluster instances explicitly in finally block of each test.

Result:

Cluster instances closed after each test causing tests to run more
reliably.
Changeset738a73e7064e00e0717fcc5168a59cf26505700b76df268c6c371a3fc241bef1
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2016-12-06 17:10 -06:00
CommitterAndrew Tolbert <andrew.tolbert@datastax.com> · 2016-12-14 17:44 -06:00
Parentsb5a08ee3ce3d

Browse the files at this changeset

2 files changed

modified driver-core/src/test/java/com/datastax/driver/core/HeartbeatTest.java +52 −44
@@ -63,40 +63,44 @@
6363 .setHeartbeatIntervalSeconds(3))
6464 .build());
6565
66- // Don't create any session, only the control connection will be established
67- cluster.init();
68-
69- for (int i = 0; i < 5; i++) {
70- triggerRequestOnControlConnection(cluster);
71- SECONDS.sleep(1);
66+ try {
67+ // Don't create any session, only the control connection will be established
68+ cluster.init();
69+
70+ for (int i = 0; i < 5; i++) {
71+ triggerRequestOnControlConnection(cluster);
72+ SECONDS.sleep(1);
73+ }
74+ assertThat(logs.getNext()).doesNotContain("sending heartbeat");
75+
76+ // Ensure heartbeat is sent after no activity.
77+ SECONDS.sleep(4);
78+ assertThat(logs.getNext())
79+ .contains("sending heartbeat")
80+ .contains("heartbeat query succeeded");
81+
82+ // Ensure heartbeat is sent after continued inactivity.
83+ SECONDS.sleep(4);
84+ assertThat(logs.getNext())
85+ .contains("sending heartbeat")
86+ .contains("heartbeat query succeeded");
87+
88+ // Ensure heartbeat is not sent after activity.
89+ logs.getNext();
90+ for (int i = 0; i < 5; i++) {
91+ triggerRequestOnControlConnection(cluster);
92+ SECONDS.sleep(1);
93+ }
94+ assertThat(logs.getNext()).doesNotContain("sending heartbeat");
95+
96+ // Finally, ensure heartbeat is sent after inactivity.
97+ SECONDS.sleep(4);
98+ assertThat(logs.getNext())
99+ .contains("sending heartbeat")
100+ .contains("heartbeat query succeeded");
101+ } finally {
102+ cluster.close();
72103 }
73- assertThat(logs.getNext()).doesNotContain("sending heartbeat");
74-
75- // Ensure heartbeat is sent after no activity.
76- SECONDS.sleep(4);
77- assertThat(logs.getNext())
78- .contains("sending heartbeat")
79- .contains("heartbeat query succeeded");
80-
81- // Ensure heartbeat is sent after continued inactivity.
82- SECONDS.sleep(4);
83- assertThat(logs.getNext())
84- .contains("sending heartbeat")
85- .contains("heartbeat query succeeded");
86-
87- // Ensure heartbeat is not sent after activity.
88- logs.getNext();
89- for (int i = 0; i < 5; i++) {
90- triggerRequestOnControlConnection(cluster);
91- SECONDS.sleep(1);
92- }
93- assertThat(logs.getNext()).doesNotContain("sending heartbeat");
94-
95- // Finally, ensure heartbeat is sent after inactivity.
96- SECONDS.sleep(4);
97- assertThat(logs.getNext())
98- .contains("sending heartbeat")
99- .contains("heartbeat query succeeded");
100104 }
101105
102106 /**
@@ -119,18 +123,22 @@
119123 .setHeartbeatIntervalSeconds(0))
120124 .build());
121125
122- // Don't create any session, only the control connection will be established
123- cluster.init();
124-
125- for (int i = 0; i < 5; i++) {
126- triggerRequestOnControlConnection(cluster);
127- SECONDS.sleep(1);
126+ try {
127+ // Don't create any session, only the control connection will be established
128+ cluster.init();
129+
130+ for (int i = 0; i < 5; i++) {
131+ triggerRequestOnControlConnection(cluster);
132+ SECONDS.sleep(1);
133+ }
134+ assertThat(logs.get()).doesNotContain("sending heartbeat");
135+
136+ // Sleep for a while and ensure no heartbeat is sent.
137+ SECONDS.sleep(32);
138+ assertThat(logs.get()).doesNotContain("sending heartbeat");
139+ } finally {
140+ cluster.close();
128141 }
129- assertThat(logs.get()).doesNotContain("sending heartbeat");
130-
131- // Sleep for a while and ensure no heartbeat is sent.
132- SECONDS.sleep(32);
133- assertThat(logs.get()).doesNotContain("sending heartbeat");
134142 }
135143
136144 // Simulates activity on the control connection via the internal API
modified driver-core/src/test/java/com/datastax/driver/core/LoadBalancingPolicyBootstrapTest.java +34 −26
@@ -53,12 +53,16 @@
5353 .withLoadBalancingPolicy(policy)
5454 .build());
5555
56- cluster.init();
57-
58- assertThat(policy.history).containsOnly(
59- entry(INIT, TestUtils.findHost(cluster, 1)),
60- entry(INIT, TestUtils.findHost(cluster, 2))
61- );
56+ try {
57+ cluster.init();
58+
59+ assertThat(policy.history).containsOnly(
60+ entry(INIT, TestUtils.findHost(cluster, 1)),
61+ entry(INIT, TestUtils.findHost(cluster, 2))
62+ );
63+ } finally {
64+ cluster.close();
65+ }
6266 }
6367
6468 /**
@@ -95,27 +99,31 @@
9599 .withLoadBalancingPolicy(policy)
96100 .build());
97101
98- cluster.init();
99-
100- if (policy.history.contains(entry(DOWN, TestUtils.findHost(cluster, nodeToStop)))) {
101- // This is the situation we're testing, the control connection tried the stopped node first.
102- assertThat(policy.history).containsExactly(
103- entry(INIT, TestUtils.findHost(cluster, activeNode)),
104- entry(DOWN, TestUtils.findHost(cluster, nodeToStop))
105- );
106- break;
107- } else {
108- assertThat(policy.history).containsOnly(
109- entry(INIT, TestUtils.findHost(cluster, 1)),
110- entry(INIT, TestUtils.findHost(cluster, 2))
111- );
112-
113- logger.info("Could not get first contact point to fail, retrying");
114-
102+ try {
103+ cluster.init();
104+
105+ if (policy.history.contains(entry(DOWN, TestUtils.findHost(cluster, nodeToStop)))) {
106+ // This is the situation we're testing, the control connection tried the stopped node first.
107+ assertThat(policy.history).containsExactly(
108+ entry(INIT, TestUtils.findHost(cluster, activeNode)),
109+ entry(DOWN, TestUtils.findHost(cluster, nodeToStop))
110+ );
111+ break;
112+ } else {
113+ assertThat(policy.history).containsOnly(
114+ entry(INIT, TestUtils.findHost(cluster, 1)),
115+ entry(INIT, TestUtils.findHost(cluster, 2))
116+ );
117+
118+ logger.info("Could not get first contact point to fail, retrying");
119+
120+ cluster.close();
121+
122+ ccm().start(nodeToStop);
123+ ccm().waitForUp(nodeToStop);
124+ }
125+ } finally {
115126 cluster.close();
116-
117- ccm().start(nodeToStop);
118- ccm().waitForUp(nodeToStop);
119127 }
120128 }
121129