Elffuss BraidHub

kikocis/java-driver public

Add additional tests for validating heartbeat behavior (JAVA-533).

Update should_send_heartbeat_when_connection_is_inactive to additionally
validate that consecutive heartbeats are sent, heartbeat not sent after
activity after heartbeat.

Add should_not_found_heartbeat_when_disabled to ensure heartbeat is not
sent when disabled.
Changeset723a422766066bdc1edd2ddcf14890d4a8245a1884ff3241e0dcf816eb58c41a
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-01-15 21:41 -06:00
Committerolim7t <omichallat+github@gmail.com> · 2015-02-03 14:11 +01:00
Parents59d6d54613ba

Browse the files at this changeset

2 files changed

modified driver-core/src/test/java/com/datastax/driver/core/HeartbeatTest.java +81 −5
@@ -28,6 +28,15 @@
2828 connectionLogger.removeAppender(logs);
2929 }
3030
31+ /**
32+ * Ensures that a heartbeat message is sent after the configured heartbeat interval of idle time and succeeds and
33+ * continues to be sent as long as the connection remains idle.
34+ *
35+ * @test_category connection:heartbeat
36+ * @expected_result heartbeat is sent after heartbeat interval (3) seconds of idle time.
37+ * @jira_ticket JAVA-533
38+ * @since 2.0.10, 2.1.5
39+ */
3140 @Test(groups = "long")
3241 public void should_send_heartbeat_when_connection_is_inactive() throws InterruptedException {
3342 CCMBridge ccm = null;
@@ -43,14 +52,34 @@
4352 cluster.init();
4453
4554 for (int i = 0; i < 5; i++) {
46- // Simulate activity
47- cluster.manager.controlConnection.refreshNodeInfo(TestUtils.findHost(cluster, 1));
55+ triggerRequestOnControlConnection(cluster);
4856 SECONDS.sleep(1);
4957 }
50- assertThat(logs.get()).doesNotContain("sending heartbeat");
58+ assertThat(logs.getNext()).doesNotContain("sending heartbeat");
59+
60+ // Ensure heartbeat is sent after no activity.
61+ SECONDS.sleep(4);
62+ assertThat(logs.getNext())
63+ .contains("sending heartbeat")
64+ .contains("heartbeat query succeeded");
65+
66+ // Ensure heartbeat is sent after continued inactivity.
67+ SECONDS.sleep(4);
68+ assertThat(logs.getNext())
69+ .contains("sending heartbeat")
70+ .contains("heartbeat query succeeded");
71+
72+ // Ensure heartbeat is not sent after activity.
73+ logs.getNext();
74+ for (int i = 0; i < 5; i++) {
75+ triggerRequestOnControlConnection(cluster);
76+ SECONDS.sleep(1);
77+ }
78+ assertThat(logs.getNext()).doesNotContain("sending heartbeat");
5179
52- SECONDS.sleep(5);
53- assertThat(logs.get())
80+ // Finally, ensure heartbeat is sent after inactivity.
81+ SECONDS.sleep(4);
82+ assertThat(logs.getNext())
5483 .contains("sending heartbeat")
5584 .contains("heartbeat query succeeded");
5685 } finally {
@@ -60,4 +89,51 @@
6089 ccm.remove();
6190 }
6291 }
92+
93+ /**
94+ * Ensures that a heartbeat message is not sent if the configured heartbeat interval is 0.
95+ *
96+ * While difficult to prove the absence of evidence, the test will wait up to the default heartbeat interval
97+ * (30 seconds + 1) and check to see if the heartbeat was sent.
98+ *
99+ * @test_category connection:heartbeat
100+ * @expected_result heartbeat is not sent after default heartbeat interval (60) seconds of idle time.
101+ * @jira_ticket JAVA-533
102+ * @since 2.0.10, 2.1.5
103+ */
104+ @Test(groups = "long")
105+ public void should_not_send_heartbeat_when_disabled() throws InterruptedException {
106+ CCMBridge ccm = null;
107+ Cluster cluster = null;
108+ try {
109+ ccm = CCMBridge.create("test", 1);
110+ cluster = Cluster.builder().addContactPoint(CCMBridge.ipOfNode(1))
111+ .withPoolingOptions(new PoolingOptions()
112+ .setHeartbeatIntervalSeconds(0))
113+ .build();
114+
115+ // Don't create any session, only the control connection will be established
116+ cluster.init();
117+
118+ for (int i = 0; i < 5; i++) {
119+ triggerRequestOnControlConnection(cluster);
120+ SECONDS.sleep(1);
121+ }
122+ assertThat(logs.get()).doesNotContain("sending heartbeat");
123+
124+ // Sleep for a while and ensure no heartbeat is sent.
125+ SECONDS.sleep(32);
126+ assertThat(logs.get()).doesNotContain("sending heartbeat");
127+ } finally {
128+ if (cluster != null)
129+ cluster.close();
130+ if (ccm != null)
131+ ccm.remove();
132+ }
133+ }
134+
135+ // Simulates activity on the control connection via the internal API
136+ private void triggerRequestOnControlConnection(Cluster cluster) {
137+ cluster.manager.controlConnection.refreshNodeInfo(TestUtils.findHost(cluster, 1));
138+ }
63139 }
modified driver-core/src/test/java/com/datastax/driver/core/MemoryAppender.java +11 −0
@@ -14,6 +14,8 @@
1414 public class MemoryAppender extends WriterAppender {
1515 public final StringWriter writer = new StringWriter();
1616
17+ private int nextLogIdx = 0;
18+
1719 public MemoryAppender() {
1820 setWriter(writer);
1921 setLayout(new PatternLayout("%m%n"));
@@ -22,4 +24,13 @@
2224 public String get() {
2325 return writer.toString();
2426 }
27+
28+ /**
29+ * @return The next set of logs after getNext was last called. Not thread safe.
30+ */
31+ public String getNext() {
32+ String next = get().substring(nextLogIdx);
33+ nextLogIdx += next.length();
34+ return next;
35+ }
2536 }