Elffuss BraidHub

kikocis/java-driver public

Add tests for Metrics enablement/disablement.
Changeset88804a2cfe18695508b472bfe2f7f14d2739ac8bd8b6ec3a4ce5cabe7369c2b7
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-10-12 12:18 -05:00
Committerolim7t <omichallat+github@gmail.com> · 2015-10-13 11:29 +02:00
Parents75c486da29cc

Browse the files at this changeset

1 file changed

modified driver-core/src/test/java/com/datastax/driver/core/MetricsTest.java +77 −0
@@ -15,9 +15,15 @@
1515 */
1616 package com.datastax.driver.core;
1717
18+import java.lang.management.ManagementFactory;
1819 import java.util.Collection;
20+import java.util.Collections;
1921
2022 import com.google.common.collect.Lists;
23+import javax.management.InstanceNotFoundException;
24+import javax.management.MBeanInfo;
25+import javax.management.MBeanServer;
26+import javax.management.ObjectName;
2127 import org.testng.annotations.Test;
2228
2329 import static org.testng.Assert.assertEquals;
@@ -26,9 +32,13 @@
2632 import com.datastax.driver.core.policies.RetryPolicy;
2733 import com.datastax.driver.core.policies.RetryPolicy.RetryDecision;
2834
35+import static com.datastax.driver.core.Assertions.assertThat;
36+
2937 public class MetricsTest extends CCMBridge.PerClassSingleNodeCluster {
3038 private volatile RetryDecision retryDecision;
3139
40+ MBeanServer server = ManagementFactory.getPlatformMBeanServer();
41+
3242 @Override
3343 protected Cluster.Builder configure(Cluster.Builder builder) {
3444 return builder.withRetryPolicy(new RetryPolicy() {
@@ -81,4 +91,71 @@
8191 assertEquals(errors.getIgnores().getCount(), 1);
8292 assertEquals(errors.getIgnoresOnUnavailable().getCount(), 1);
8393 }
94+
95+ /**
96+ * Validates that metrics are enabled and exposed by JMX by default by checking that
97+ * {@link Cluster#getMetrics()} is not null and 'clusterName-metrics:name=connected-to'
98+ * MBean is present.
99+ *
100+ * @test_category metrics
101+ */
102+ @Test(groups="short")
103+ public void should_enable_metrics_and_jmx_by_default() throws Exception {
104+ assertThat(cluster.getMetrics()).isNotNull();
105+ ObjectName clusterMetricsON = ObjectName.getInstance(cluster.getClusterName() + "-metrics:name=connected-to");
106+ MBeanInfo mBean = server.getMBeanInfo(clusterMetricsON);
107+ assertThat(mBean).isNotNull();
108+
109+ assertThat(cluster.getConfiguration().getMetricsOptions().isEnabled()).isTrue();
110+ assertThat(cluster.getConfiguration().getMetricsOptions().isJMXReportingEnabled()).isTrue();
111+ }
112+
113+ /**
114+ * Validates that when metrics are disabled using {@link Cluster.Builder#withoutMetrics()}
115+ * that {@link Cluster#getMetrics()} returns null and 'clusterName-metrics:name=connected-to'
116+ * MBean is not present.
117+ *
118+ * @test_category metrics
119+ */
120+ @Test(groups="short", expectedExceptions=InstanceNotFoundException.class)
121+ public void metrics_should_be_null_when_metrics_disabled() throws Exception {
122+ Cluster cluster = super.configure(Cluster.builder())
123+ .addContactPointsWithPorts(Collections.singletonList(hostAddress))
124+ .withoutMetrics()
125+ .build();
126+ try {
127+ cluster.init();
128+ assertThat(cluster.getMetrics()).isNull();
129+ assertThat(cluster.getConfiguration().getMetricsOptions().isEnabled()).isFalse();
130+ ObjectName clusterMetricsON = ObjectName.getInstance(cluster.getClusterName() + "-metrics:name=connected-to");
131+ server.getMBeanInfo(clusterMetricsON);
132+ } finally {
133+ cluster.close();
134+ }
135+ }
136+
137+ /**
138+ * Validates that when metrics are enabled but JMX reporting is disabled via
139+ * {@link Cluster.Builder#withoutJMXReporting()} that {@link Cluster#getMetrics()}
140+ * is not null and 'clusterName-metrics:name=connected-to' MBean is present.
141+ *
142+ * @test_category metrics
143+ */
144+ @Test(groups="short", expectedExceptions=InstanceNotFoundException.class)
145+ public void should_be_no_jmx_mbean_when_jmx_is_disabled() throws Exception {
146+ Cluster cluster = super.configure(Cluster.builder())
147+ .addContactPointsWithPorts(Collections.singletonList(hostAddress))
148+ .withoutJMXReporting()
149+ .build();
150+ try {
151+ cluster.init();
152+ assertThat(cluster.getMetrics()).isNotNull();
153+ assertThat(cluster.getConfiguration().getMetricsOptions().isEnabled()).isTrue();
154+ assertThat(cluster.getConfiguration().getMetricsOptions().isJMXReportingEnabled()).isFalse();
155+ ObjectName clusterMetricsON = ObjectName.getInstance(cluster.getClusterName() + "-metrics:name=connected-to");
156+ server.getMBeanInfo(clusterMetricsON);
157+ } finally {
158+ cluster.close();
159+ }
160+ }
84161 }