|
| 1 | +/* |
| 2 | + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Modifications copyright (C) 2017 Uber Technologies, Inc. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not |
| 7 | + * use this file except in compliance with the License. A copy of the License is |
| 8 | + * located at |
| 9 | + * |
| 10 | + * http://aws.amazon.com/apache2.0 |
| 11 | + * |
| 12 | + * or in the "license" file accompanying this file. This file is distributed on |
| 13 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 14 | + * express or implied. See the License for the specific language governing |
| 15 | + * permissions and limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.uber.cadence.reporter; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | + |
| 22 | +import com.uber.m3.tally.CapableOf; |
| 23 | +import com.uber.m3.util.Duration; |
| 24 | +import com.uber.m3.util.ImmutableMap; |
| 25 | +import io.micrometer.core.instrument.Metrics; |
| 26 | +import io.micrometer.core.instrument.Tag; |
| 27 | +import io.micrometer.core.instrument.simple.SimpleMeterRegistry; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | +import org.junit.After; |
| 32 | +import org.junit.Before; |
| 33 | +import org.junit.Test; |
| 34 | + |
| 35 | +public class CadenceClientStatsReporterTest { |
| 36 | + |
| 37 | + private static final String DEFAULT_REPORT_NAME = "cadence_workflow_start"; |
| 38 | + private static final Map<String, String> DEFAULT_REPORT_TAGS = |
| 39 | + ImmutableMap.of("Domain", "domain_name", "TaskList", "task_list"); |
| 40 | + private static final long DEFAULT_COUNT = 10; |
| 41 | + private static final Duration DEFAULT_DURATION = Duration.ofSeconds(10); |
| 42 | + |
| 43 | + private CadenceClientStatsReporter cadenceClientStatsReporter = new CadenceClientStatsReporter(); |
| 44 | + |
| 45 | + @Before |
| 46 | + public void init() { |
| 47 | + Metrics.addRegistry(new SimpleMeterRegistry()); |
| 48 | + } |
| 49 | + |
| 50 | + @After |
| 51 | + public void cleanup() { |
| 52 | + Metrics.globalRegistry.getMeters().forEach(Metrics.globalRegistry::remove); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testReporterCapabilitiesShouldReturnReporting() { |
| 57 | + assertEquals(CapableOf.REPORTING, cadenceClientStatsReporter.capabilities()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testCounterShouldCallMetricRegistryForMonitoredCounterCadenceAction() { |
| 62 | + callDefaultCounter(); |
| 63 | + |
| 64 | + assertEquals( |
| 65 | + Arrays.asList(Tag.of("Domain", "domain_name"), Tag.of("TaskList", "task_list")), |
| 66 | + Metrics.globalRegistry.get(DEFAULT_REPORT_NAME).counter().getId().getTags()); |
| 67 | + assertEquals(10, Metrics.globalRegistry.get(DEFAULT_REPORT_NAME).counter().count(), 0); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testTimerShouldCallMetricRegistryForMonitoredCounterCadenceAction() { |
| 72 | + callDefaultTimer(); |
| 73 | + |
| 74 | + assertEquals( |
| 75 | + Arrays.asList(Tag.of("Domain", "domain_name"), Tag.of("TaskList", "task_list")), |
| 76 | + Metrics.globalRegistry.get(DEFAULT_REPORT_NAME).timer().getId().getTags()); |
| 77 | + assertEquals( |
| 78 | + 10, Metrics.globalRegistry.get(DEFAULT_REPORT_NAME).timer().totalTime(TimeUnit.SECONDS), 0); |
| 79 | + } |
| 80 | + |
| 81 | + private void callDefaultCounter() { |
| 82 | + cadenceClientStatsReporter.reportCounter( |
| 83 | + DEFAULT_REPORT_NAME, DEFAULT_REPORT_TAGS, DEFAULT_COUNT); |
| 84 | + } |
| 85 | + |
| 86 | + private void callDefaultTimer() { |
| 87 | + cadenceClientStatsReporter.reportTimer( |
| 88 | + DEFAULT_REPORT_NAME, DEFAULT_REPORT_TAGS, DEFAULT_DURATION); |
| 89 | + } |
| 90 | +} |
0 commit comments