kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 723a422766066bdc1edd2ddcf14890d4a8245a1884ff3241e0dcf816eb58c41a |
|---|---|
| Author | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-01-15 21:41 -06:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-02-03 14:11 +01:00 |
| Parents | 59d6d54613ba |
2 files changed
modified driver-core/src/test/java/com/datastax/driver/core/HeartbeatTest.java +81 −5
| @@ -28,6 +28,15 @@ | ||
| 28 | 28 | connectionLogger.removeAppender(logs); |
| 29 | 29 | } |
| 30 | 30 | |
| 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 | + */ | |
| 31 | 40 | @Test(groups = "long") |
| 32 | 41 | public void should_send_heartbeat_when_connection_is_inactive() throws InterruptedException { |
| 33 | 42 | CCMBridge ccm = null; |
| @@ -43,14 +52,34 @@ | ||
| 43 | 52 | cluster.init(); |
| 44 | 53 | |
| 45 | 54 | for (int i = 0; i < 5; i++) { |
| 46 | - // Simulate activity | |
| 47 | - cluster.manager.controlConnection.refreshNodeInfo(TestUtils.findHost(cluster, 1)); | |
| 55 | + triggerRequestOnControlConnection(cluster); | |
| 48 | 56 | SECONDS.sleep(1); |
| 49 | 57 | } |
| 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"); | |
| 51 | 79 | |
| 52 | - SECONDS.sleep(5); | |
| 53 | - assertThat(logs.get()) | |
| 80 | + // Finally, ensure heartbeat is sent after inactivity. | |
| 81 | + SECONDS.sleep(4); | |
| 82 | + assertThat(logs.getNext()) | |
| 54 | 83 | .contains("sending heartbeat") |
| 55 | 84 | .contains("heartbeat query succeeded"); |
| 56 | 85 | } finally { |
| @@ -60,4 +89,51 @@ | ||
| 60 | 89 | ccm.remove(); |
| 61 | 90 | } |
| 62 | 91 | } |
| 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 | + } | |
| 63 | 139 | } |
modified driver-core/src/test/java/com/datastax/driver/core/MemoryAppender.java +11 −0
| @@ -14,6 +14,8 @@ | ||
| 14 | 14 | public class MemoryAppender extends WriterAppender { |
| 15 | 15 | public final StringWriter writer = new StringWriter(); |
| 16 | 16 | |
| 17 | + private int nextLogIdx = 0; | |
| 18 | + | |
| 17 | 19 | public MemoryAppender() { |
| 18 | 20 | setWriter(writer); |
| 19 | 21 | setLayout(new PatternLayout("%m%n")); |
| @@ -22,4 +24,13 @@ | ||
| 22 | 24 | public String get() { |
| 23 | 25 | return writer.toString(); |
| 24 | 26 | } |
| 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 | + } | |
| 25 | 36 | } |