|
| 1 | +// Copyright (c) 2018 Pivotal Software, Inc. All rights reserved. |
| 2 | +// |
| 3 | +// This software, the RabbitMQ Java client library, is triple-licensed under the |
| 4 | +// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2 |
| 5 | +// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see |
| 6 | +// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL, |
| 7 | +// please see LICENSE-APACHE2. |
| 8 | +// |
| 9 | +// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, |
| 10 | +// either express or implied. See the LICENSE file for specific language governing |
| 11 | +// rights and limitations of this software. |
| 12 | +// |
| 13 | +// If you have any questions regarding licensing, please contact us at |
| 14 | + |
| 15 | + |
| 16 | +package com.rabbitmq.perf; |
| 17 | + |
| 18 | +import io.micrometer.core.instrument.composite.CompositeMeterRegistry; |
| 19 | +import org.apache.commons.cli.CommandLine; |
| 20 | +import org.apache.commons.cli.CommandLineParser; |
| 21 | +import org.apache.commons.cli.GnuParser; |
| 22 | +import org.apache.commons.cli.Options; |
| 23 | +import org.junit.jupiter.api.AfterEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import javax.management.MBeanInfo; |
| 27 | +import javax.management.MBeanServer; |
| 28 | +import javax.management.ObjectName; |
| 29 | +import java.lang.management.ManagementFactory; |
| 30 | +import java.util.Set; |
| 31 | +import java.util.concurrent.atomic.AtomicInteger; |
| 32 | + |
| 33 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 34 | + |
| 35 | +/** |
| 36 | + * |
| 37 | + */ |
| 38 | +public class JmxMetricsTest { |
| 39 | + |
| 40 | + Metrics metrics = new JmxMetrics(); |
| 41 | + |
| 42 | + @AfterEach |
| 43 | + public void tearDown() throws Exception { |
| 44 | + metrics.close(); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void metricsShouldBeExposedAsMbeans() throws Exception { |
| 49 | + Options options = metrics.options(); |
| 50 | + |
| 51 | + CommandLineParser parser = new GnuParser(); |
| 52 | + CommandLine rawCmd = parser.parse( |
| 53 | + options, |
| 54 | + ("--metrics-jmx").split(" ") |
| 55 | + ); |
| 56 | + CommandLineProxy cmd = new CommandLineProxy(options, rawCmd, name -> null); |
| 57 | + CompositeMeterRegistry registry = new CompositeMeterRegistry(); |
| 58 | + AtomicInteger metric = registry.gauge("dummy", new AtomicInteger(0)); |
| 59 | + metric.set(42); |
| 60 | + metrics.configure(cmd, registry, null); |
| 61 | + |
| 62 | + MBeanServer server = ManagementFactory.getPlatformMBeanServer(); |
| 63 | + Set<ObjectName> objectNames = server.queryNames(new ObjectName("*:name=dummy"), null); |
| 64 | + assertEquals(1, objectNames.size()); |
| 65 | + ObjectName objectName = objectNames.iterator().next(); |
| 66 | + MBeanInfo info = server.getMBeanInfo(objectName); |
| 67 | + Object attribute = server.getAttribute(objectName, info.getAttributes()[0].getName()); |
| 68 | + assertEquals(metric.get(), Double.valueOf(attribute.toString()).intValue()); |
| 69 | + } |
| 70 | +} |
0 commit comments