kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 88804a2cfe18695508b472bfe2f7f14d2739ac8bd8b6ec3a4ce5cabe7369c2b7 |
|---|---|
| Author | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-10-12 12:18 -05:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-10-13 11:29 +02:00 |
| Parents | 75c486da29cc |
1 file changed
modified driver-core/src/test/java/com/datastax/driver/core/MetricsTest.java +77 −0
| @@ -15,9 +15,15 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | +import java.lang.management.ManagementFactory; | |
| 18 | 19 | import java.util.Collection; |
| 20 | +import java.util.Collections; | |
| 19 | 21 | |
| 20 | 22 | 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; | |
| 21 | 27 | import org.testng.annotations.Test; |
| 22 | 28 | |
| 23 | 29 | import static org.testng.Assert.assertEquals; |
| @@ -26,9 +32,13 @@ | ||
| 26 | 32 | import com.datastax.driver.core.policies.RetryPolicy; |
| 27 | 33 | import com.datastax.driver.core.policies.RetryPolicy.RetryDecision; |
| 28 | 34 | |
| 35 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 36 | + | |
| 29 | 37 | public class MetricsTest extends CCMBridge.PerClassSingleNodeCluster { |
| 30 | 38 | private volatile RetryDecision retryDecision; |
| 31 | 39 | |
| 40 | + MBeanServer server = ManagementFactory.getPlatformMBeanServer(); | |
| 41 | + | |
| 32 | 42 | @Override |
| 33 | 43 | protected Cluster.Builder configure(Cluster.Builder builder) { |
| 34 | 44 | return builder.withRetryPolicy(new RetryPolicy() { |
| @@ -81,4 +91,71 @@ | ||
| 81 | 91 | assertEquals(errors.getIgnores().getCount(), 1); |
| 82 | 92 | assertEquals(errors.getIgnoresOnUnavailable().getCount(), 1); |
| 83 | 93 | } |
| 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 | + } | |
| 84 | 161 | } |