From b0275031257aecd9671d0079218fc52ff533f30b Mon Sep 17 00:00:00 2001 From: Tyler Ouyang Date: Mon, 4 Mar 2024 09:56:07 -0800 Subject: [PATCH] Modernize universal tests commit-id:a1e3bceb --- .../events/GenericEventPublisherTest.java | 12 +- .../events/MetricsAsEventsListenerTest.java | 22 +- .../events/ResourceChangedEventTest.java | 33 ++- .../universal/metrics/UtilsTest.java | 100 +++++--- .../micrometer/PinStatsConfigTest.java | 167 ++++++------ .../micrometer/PinStatsLongTaskTimerTest.java | 49 ++-- .../PinStatsNamingConventionTest.java | 92 ++++--- .../micrometer/PinStatsPublisherTest.java | 241 ++++++++++-------- 8 files changed, 421 insertions(+), 295 deletions(-) diff --git a/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/events/GenericEventPublisherTest.java b/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/events/GenericEventPublisherTest.java index 66c9a628a3..160fdf5e20 100644 --- a/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/events/GenericEventPublisherTest.java +++ b/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/events/GenericEventPublisherTest.java @@ -27,7 +27,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -public class GenericEventPublisherTest { +class GenericEventPublisherTest { private AppEventListener mockResourceChangedEventListener; private AppEventListener mockSlowListener; private AppEventListener mockChildTestEventListener; @@ -66,7 +66,7 @@ private AppEventListener mockEventListener(Class even } @Test - public void testSubscribe() { + void testSubscribe() { sut.subscribe(mockResourceChangedEventListener); sut.publishEvent(resourceChangedEvent); sut.terminate(); @@ -75,7 +75,7 @@ public void testSubscribe() { } @Test - public void testPublishEvent_publishThenSubscribe() { + void testPublishEvent_publishThenSubscribe() { for (int i = 0; i < GenericEventPublisher.BUFFER_SIZE + 10; i++) { sut.publishEvent(resourceChangedEvent); } @@ -88,7 +88,7 @@ public void testPublishEvent_publishThenSubscribe() { } @Test - public void testPublishEvent_subscribeThenPublish() { + void testPublishEvent_subscribeThenPublish() { int numEvents = GenericEventPublisher.BUFFER_SIZE * 2; sut.subscribe(mockResourceChangedEventListener); for (int i = 0; i < numEvents; i++) { @@ -102,7 +102,7 @@ public void testPublishEvent_subscribeThenPublish() { } @Test - public void testMultipleListeners() { + void testMultipleListeners() { sut.subscribe(mockResourceChangedEventListener); sut.subscribe(mockSlowListener); @@ -120,7 +120,7 @@ public void testMultipleListeners() { } @Test - public void testMultipleListeners_differentEventTypes() { + void testMultipleListeners_differentEventTypes() { sut.subscribe(mockResourceChangedEventListener); sut.subscribe(mockSlowListener); sut.subscribe(mockChildTestEventListener); diff --git a/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/events/MetricsAsEventsListenerTest.java b/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/events/MetricsAsEventsListenerTest.java index ae5c1899de..b2564c7cbb 100644 --- a/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/events/MetricsAsEventsListenerTest.java +++ b/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/events/MetricsAsEventsListenerTest.java @@ -1,15 +1,29 @@ +/** + * Copyright (c) 2024 Pinterest, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.pinterest.teletraan.universal.events; -import static org.junit.Assert.assertNotNull; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import io.micrometer.core.instrument.Metrics; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import io.micrometer.core.instrument.Metrics; - -public class MetricsAsEventsListenerTest { +class MetricsAsEventsListenerTest { private static final String CHILD_RESOURCE = "child_resource"; private static final String RESOURCE = "resource"; private MetricsAsEventsListener sut; diff --git a/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/events/ResourceChangedEventTest.java b/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/events/ResourceChangedEventTest.java index bdba84000c..71c5e93ef6 100644 --- a/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/events/ResourceChangedEventTest.java +++ b/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/events/ResourceChangedEventTest.java @@ -1,42 +1,57 @@ +/** + * Copyright (c) 2024 Pinterest, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.pinterest.teletraan.universal.events; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; +import com.google.common.collect.ImmutableMap; import java.util.Map; import org.junit.jupiter.api.Test; -import com.google.common.collect.ImmutableMap; - -public class ResourceChangedEventTest { +class ResourceChangedEventTest { private static final String OPERATOR = "operator"; private static final String RESOURCE = "resource"; @Test - public void testConstructor_oddTags() { + void testConstructor_oddTags() { assertThrows( IllegalArgumentException.class, () -> new ResourceChangedEvent(RESOURCE, OPERATOR, this, 0L, "")); } @Test - public void testConstructor_evenTags() { - assertDoesNotThrow(() -> new ResourceChangedEvent(RESOURCE, OPERATOR, this, 0L, "t1", "v1")); + void testConstructor_evenTags() { + assertDoesNotThrow( + () -> new ResourceChangedEvent(RESOURCE, OPERATOR, this, 0L, "t1", "v1")); } @Test - public void testConstructor_noTag() { + void testConstructor_noTag() { assertDoesNotThrow(() -> new ResourceChangedEvent(RESOURCE, OPERATOR, this, 0L)); } @Test - public void testConstructor_tagsAsMap() { + void testConstructor_tagsAsMap() { Map tags = ImmutableMap.of("t1", "v1"); assertDoesNotThrow(() -> new ResourceChangedEvent(RESOURCE, OPERATOR, this, 0L, tags)); } @Test - public void testConstructor_nullSource() { + void testConstructor_nullSource() { assertThrows( IllegalArgumentException.class, () -> new ResourceChangedEvent(RESOURCE, OPERATOR, null, 0L)); diff --git a/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/metrics/UtilsTest.java b/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/metrics/UtilsTest.java index 4ffc3d4ebe..8444f7d773 100644 --- a/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/metrics/UtilsTest.java +++ b/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/metrics/UtilsTest.java @@ -1,9 +1,24 @@ +/** + * Copyright (c) 2024 Pinterest, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.pinterest.teletraan.universal.metrics; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertTrue; import io.micrometer.core.instrument.Measurement; import io.micrometer.core.instrument.Meter; @@ -15,50 +30,51 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -public class UtilsTest { - @BeforeEach - public void setUp() { - Metrics.globalRegistry.clear(); - } +class UtilsTest { + @BeforeEach + public void setUp() { + Metrics.globalRegistry.clear(); + } - @Test - void testRegisterOrReplaceMeter() { - String meterName = "meter"; - Tags tags = Tags.of("tagKey", "tagValue"); - double value1 = 1.0; - Meter meter1 = - Meter.builder( - meterName, - Type.GAUGE, - Arrays.asList(new Measurement(() -> value1, Statistic.VALUE))) - .tags(tags) - .register(Metrics.globalRegistry); + @Test + void testRegisterOrReplaceMeter() { + String meterName = "meter"; + Tags tags = Tags.of("tagKey", "tagValue"); + double value1 = 1.0; + Meter meter1 = + Meter.builder( + meterName, + Type.GAUGE, + Arrays.asList(new Measurement(() -> value1, Statistic.VALUE))) + .tags(tags) + .register(Metrics.globalRegistry); - // When registering a meter directly with the same name and tags, the old meter - // should be returned. - double value2 = 2.0; - Meter meter2 = - Meter.builder( - meterName, - Type.GAUGE, - Arrays.asList(new Measurement(() -> value2, Statistic.VALUE))) - .tags(tags) - .register(Metrics.globalRegistry); - assertSame(meter1, meter2); + // When registering a meter directly with the same name and tags, the old meter + // should be returned. + double value2 = 2.0; + Meter meter2 = + Meter.builder( + meterName, + Type.GAUGE, + Arrays.asList(new Measurement(() -> value2, Statistic.VALUE))) + .tags(tags) + .register(Metrics.globalRegistry); + assertSame(meter1, meter2); - // The value remains the same. - Meter found = Metrics.globalRegistry.find(meterName).tags(tags).meter(); - assertTrue(found != null); - assertEquals(value1, found.measure().iterator().next().getValue()); + // The value remains the same. + Meter found = Metrics.globalRegistry.find(meterName).tags(tags).meter(); + assertNotNull(found); + assertEquals(value1, found.measure().iterator().next().getValue()); - // When registering the meter with Utils.registerOrReplaceMeter, a new meter - // should be returned. - Meter meter3 = - Utils.registerOrReplaceMeter(Metrics.globalRegistry, meterName, tags, value2, Type.GAUGE); - assertNotSame(meter1, meter3); + // When registering the meter with Utils.registerOrReplaceMeter, a new meter + // should be returned. + Meter meter3 = + Utils.registerOrReplaceMeter( + Metrics.globalRegistry, meterName, tags, value2, Type.GAUGE); + assertNotSame(meter1, meter3); - found = Metrics.globalRegistry.find(meterName).tags(tags).meter(); - assertTrue(found != null); - assertEquals(value2, found.measure().iterator().next().getValue()); - } + found = Metrics.globalRegistry.find(meterName).tags(tags).meter(); + assertNotNull(found); + assertEquals(value2, found.measure().iterator().next().getValue()); + } } diff --git a/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/metrics/micrometer/PinStatsConfigTest.java b/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/metrics/micrometer/PinStatsConfigTest.java index c6999095f4..28a9ec9099 100644 --- a/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/metrics/micrometer/PinStatsConfigTest.java +++ b/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/metrics/micrometer/PinStatsConfigTest.java @@ -1,3 +1,18 @@ +/** + * Copyright (c) 2024 Pinterest, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.pinterest.teletraan.universal.metrics.micrometer; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -11,80 +26,80 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -public class PinStatsConfigTest { - private final Map props = new HashMap<>(); - private static final String URI_KEY = "mm.uri"; - private static final String IS_REQUIRED = "is required"; - - private final PinStatsConfig config = props::get; - - @BeforeEach - void setUp() { - props.clear(); - } - - @Test - void invalid_uri() { - props.put(URI_KEY, ":not-a-uri"); - - assertLinesMatch( - Stream.of("must be a valid URI", "must be a valid URI"), - config.validate().failures().stream().map(Validated.Invalid::getMessage)); - } - - @Test - void invalid_emptyUri() { - props.put(URI_KEY, ""); - - assertLinesMatch( - Stream.of(IS_REQUIRED, IS_REQUIRED), - config.validate().failures().stream().map(Validated.Invalid::getMessage)); - } - - @Test - void invalid_noPort() { - props.put(URI_KEY, "tcp://localhost"); - - assertLinesMatch( - Stream.of(IS_REQUIRED), - config.validate().failures().stream().map(Validated.Invalid::getMessage)); - } - - @Test - void valid() { - String host = "example.com"; - int port = 223; - props.put(URI_KEY, String.format("tcp://%s:%d", host, port)); - - assertTrue(config.validate().isValid()); - assertEquals(host, config.host()); - assertEquals(port, config.port()); - } - - @Test - void namePrefix() { - String namePrefix = "rodimus.prod"; - props.put("mm.namePrefix", namePrefix); - - assertTrue(config.validate().isValid()); - assertEquals(namePrefix, config.namePrefix()); - } - - @Test - void defaultConfig() { - PinStatsConfig defaultConfig = PinStatsConfig.DEFAULT; - assertTrue(defaultConfig.validate().isValid()); - assertEquals("localhost", defaultConfig.host()); - assertEquals(18126, defaultConfig.port()); - assertEquals("mm.", defaultConfig.namePrefix()); - } - - @Test - void noConfig_defaultValuesUsed() { - PinStatsConfig defaultConfig = PinStatsConfig.DEFAULT; - assertTrue(config.validate().isValid()); - assertEquals(defaultConfig.host(), config.host()); - assertEquals(defaultConfig.port(), config.port()); - assertEquals(defaultConfig.namePrefix(), config.namePrefix()); - } +class PinStatsConfigTest { + private final Map props = new HashMap<>(); + private static final String URI_KEY = "mm.uri"; + private static final String IS_REQUIRED = "is required"; + + private final PinStatsConfig config = props::get; + + @BeforeEach + void setUp() { + props.clear(); + } + + @Test + void invalid_uri() { + props.put(URI_KEY, ":not-a-uri"); + + assertLinesMatch( + Stream.of("must be a valid URI", "must be a valid URI"), + config.validate().failures().stream().map(Validated.Invalid::getMessage)); + } + + @Test + void invalid_emptyUri() { + props.put(URI_KEY, ""); + + assertLinesMatch( + Stream.of(IS_REQUIRED, IS_REQUIRED), + config.validate().failures().stream().map(Validated.Invalid::getMessage)); + } + + @Test + void invalid_noPort() { + props.put(URI_KEY, "tcp://localhost"); + + assertLinesMatch( + Stream.of(IS_REQUIRED), + config.validate().failures().stream().map(Validated.Invalid::getMessage)); + } + + @Test + void valid() { + String host = "example.com"; + int port = 223; + props.put(URI_KEY, String.format("tcp://%s:%d", host, port)); + + assertTrue(config.validate().isValid()); + assertEquals(host, config.host()); + assertEquals(port, config.port()); + } + + @Test + void namePrefix() { + String namePrefix = "rodimus.prod"; + props.put("mm.namePrefix", namePrefix); + + assertTrue(config.validate().isValid()); + assertEquals(namePrefix, config.namePrefix()); + } + + @Test + void defaultConfig() { + PinStatsConfig defaultConfig = PinStatsConfig.DEFAULT; + assertTrue(defaultConfig.validate().isValid()); + assertEquals("localhost", defaultConfig.host()); + assertEquals(18126, defaultConfig.port()); + assertEquals("mm.", defaultConfig.namePrefix()); + } + + @Test + void noConfig_defaultValuesUsed() { + PinStatsConfig defaultConfig = PinStatsConfig.DEFAULT; + assertTrue(config.validate().isValid()); + assertEquals(defaultConfig.host(), config.host()); + assertEquals(defaultConfig.port(), config.port()); + assertEquals(defaultConfig.namePrefix(), config.namePrefix()); + } } diff --git a/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/metrics/micrometer/PinStatsLongTaskTimerTest.java b/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/metrics/micrometer/PinStatsLongTaskTimerTest.java index 067501e74f..4949bf38ba 100644 --- a/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/metrics/micrometer/PinStatsLongTaskTimerTest.java +++ b/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/metrics/micrometer/PinStatsLongTaskTimerTest.java @@ -1,12 +1,21 @@ +/** + * Copyright (c) 2024 Pinterest, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.pinterest.teletraan.universal.metrics.micrometer; -import static org.junit.Assert.assertEquals; - -import java.time.Instant; -import java.util.concurrent.TimeUnit; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; import io.micrometer.core.instrument.LongTaskTimer; import io.micrometer.core.instrument.LongTaskTimer.Sample; @@ -16,8 +25,12 @@ import io.micrometer.core.instrument.distribution.DistributionStatisticConfig; import io.micrometer.core.instrument.simple.SimpleConfig; import io.micrometer.core.instrument.simple.SimpleMeterRegistry; +import java.time.Instant; +import java.util.concurrent.TimeUnit; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -public class PinStatsLongTaskTimerTest { +class PinStatsLongTaskTimerTest { private PinStatsLongTaskTimer sut; private MockClock clock; private MeterRegistry registry; @@ -25,13 +38,15 @@ public class PinStatsLongTaskTimerTest { @BeforeEach void setUp() { clock = new MockClock(); - registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, clock) { - @Override - protected LongTaskTimer newLongTaskTimer(Meter.Id id, - DistributionStatisticConfig distributionStatisticConfig) { - return new PinStatsLongTaskTimer(id, clock, getBaseTimeUnit(), distributionStatisticConfig); - } - }; + registry = + new SimpleMeterRegistry(SimpleConfig.DEFAULT, clock) { + @Override + protected LongTaskTimer newLongTaskTimer( + Meter.Id id, DistributionStatisticConfig distributionStatisticConfig) { + return new PinStatsLongTaskTimer( + id, clock, getBaseTimeUnit(), distributionStatisticConfig); + } + }; sut = (PinStatsLongTaskTimer) LongTaskTimer.builder("my.ltt").register(registry); } @@ -48,7 +63,9 @@ void withoutParameter_start_nowAsStartTime() { void withInput_start_inputAsStartTime() { Instant startTime = Instant.now(); // Set the clock to 5 seconds after startTime - clock.add(startTime.plusSeconds(5).minusMillis(clock.wallTime()).toEpochMilli(), TimeUnit.MILLISECONDS); + clock.add( + startTime.plusSeconds(5).minusMillis(clock.wallTime()).toEpochMilli(), + TimeUnit.MILLISECONDS); Sample sample = sut.start(startTime); // Add another 5 seconds to the clock clock.addSeconds(5); diff --git a/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/metrics/micrometer/PinStatsNamingConventionTest.java b/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/metrics/micrometer/PinStatsNamingConventionTest.java index d07f3f89cb..7dd71bc8a6 100644 --- a/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/metrics/micrometer/PinStatsNamingConventionTest.java +++ b/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/metrics/micrometer/PinStatsNamingConventionTest.java @@ -1,3 +1,18 @@ +/** + * Copyright (c) 2024 Pinterest, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.pinterest.teletraan.universal.metrics.micrometer; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -5,43 +20,50 @@ import io.micrometer.core.instrument.Meter; import org.junit.jupiter.api.Test; -public class PinStatsNamingConventionTest { - private PinStatsNamingConvention convention = new PinStatsNamingConvention(); - private static final String METER_NAME = "test.meter"; +class PinStatsNamingConventionTest { + private PinStatsNamingConvention convention = new PinStatsNamingConvention(); + private static final String METER_NAME = "test.meter"; - @Test - void testName() { - assertEquals("mm.counters.test_meter_total", convention.name(METER_NAME, Meter.Type.COUNTER)); - assertEquals("mm.gauges.test_meter", convention.name(METER_NAME, Meter.Type.GAUGE)); - assertEquals(METER_NAME, convention.name(METER_NAME, Meter.Type.OTHER)); - assertEquals( - "mm.histograms.test_meter", convention.name(METER_NAME, Meter.Type.DISTRIBUTION_SUMMARY)); - assertEquals( - "mm.test_meter_duration_seconds", convention.name(METER_NAME, Meter.Type.LONG_TASK_TIMER)); - assertEquals( - "mm.timers.test_meter_duration_seconds", convention.name(METER_NAME, Meter.Type.TIMER)); - } + @Test + void testName() { + assertEquals( + "mm.counters.test_meter_total", convention.name(METER_NAME, Meter.Type.COUNTER)); + assertEquals("mm.gauges.test_meter", convention.name(METER_NAME, Meter.Type.GAUGE)); + assertEquals(METER_NAME, convention.name(METER_NAME, Meter.Type.OTHER)); + assertEquals( + "mm.histograms.test_meter", + convention.name(METER_NAME, Meter.Type.DISTRIBUTION_SUMMARY)); + assertEquals( + "mm.test_meter_duration_seconds", + convention.name(METER_NAME, Meter.Type.LONG_TASK_TIMER)); + assertEquals( + "mm.timers.test_meter_duration_seconds", + convention.name(METER_NAME, Meter.Type.TIMER)); + } - @Test - void testNamePrefix() { - String prefix = "a.name.prefix."; - PinStatsNamingConvention conventionWithPrefix = new PinStatsNamingConvention(prefix); - assertEquals( - prefix + "counters.test_meter_total", - conventionWithPrefix.name(METER_NAME, Meter.Type.COUNTER)); - } + @Test + void testNamePrefix() { + String prefix = "a.name.prefix."; + PinStatsNamingConvention conventionWithPrefix = new PinStatsNamingConvention(prefix); + assertEquals( + prefix + "counters.test_meter_total", + conventionWithPrefix.name(METER_NAME, Meter.Type.COUNTER)); + } - @Test - void testTagKey() { - assertEquals("tag_key", convention.tagKey("tag.key")); - assertEquals("m_0tag_key", convention.tagKey("0tag.key")); - assertEquals("m__tag_key", convention.tagKey("*tag.key")); - } + @Test + void testTagKey() { + assertEquals("tag_key", convention.tagKey("tag.key")); + assertEquals("m_0tag_key", convention.tagKey("0tag.key")); + assertEquals("m__tag_key", convention.tagKey("*tag.key")); + } - @Test - void testCustomName() { - String customName = "abcDEF.123-xyZ"; - assertEquals(customName, - convention.name(PinStatsNamingConvention.CUSTOM_NAME_PREFIX + customName, Meter.Type.COUNTER)); - } + @Test + void testCustomName() { + String customName = "abcDEF.123-xyZ"; + assertEquals( + customName, + convention.name( + PinStatsNamingConvention.CUSTOM_NAME_PREFIX + customName, + Meter.Type.COUNTER)); + } } diff --git a/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/metrics/micrometer/PinStatsPublisherTest.java b/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/metrics/micrometer/PinStatsPublisherTest.java index b8ca8b0f84..192b0a0665 100644 --- a/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/metrics/micrometer/PinStatsPublisherTest.java +++ b/deploy-service/universal/src/test/java/com/pinterest/teletraan/universal/metrics/micrometer/PinStatsPublisherTest.java @@ -1,3 +1,18 @@ +/** + * Copyright (c) 2024 Pinterest, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.pinterest.teletraan.universal.metrics.micrometer; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -22,111 +37,123 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class PinStatsPublisherTest { - private static final Logger LOG = LoggerFactory.getLogger(PinStatsPublisherTest.class); - - private static final PinStatsConfig config = PinStatsConfig.DEFAULT; - private final MockClock clock = new MockClock(); - private final PinStatsMeterRegistry meterRegistry = new PinStatsMeterRegistry(config, clock); - private final PinStatsPublisher publisher = - new PinStatsPublisher(config, clock, TimeUnit.SECONDS, new PinStatsNamingConvention()); - private final Tags tags = Tags.of("tag", "value"); - - @Test - void writeCounter() { - String counterName = "my.counter"; - meterRegistry.counter(counterName, "t1", "v1", "t2", "v2"); - Counter counter = meterRegistry.get(counterName).counter(); - counter.increment(); - assertEquals(1, publisher.writeCounter(counter).count()); - assertEquals( - "put mm.counters.my_counter_total 1 " + clock.wallTime() + " t1=v1 t2=v2\n", - publisher.writeCounter(counter).collect(Collectors.joining())); - } - - @Test - void writeGauge() { - meterRegistry.gauge("my.gauge", 1d); - Gauge gauge = meterRegistry.get("my.gauge").gauge(); - assertEquals(1, publisher.writeGauge(gauge).count()); - } - - @Test - void writeTimeGauge() { - AtomicReference obj = new AtomicReference<>(1d); - meterRegistry - .more() - .timeGauge("my.time.gauge", Tags.empty(), obj, TimeUnit.SECONDS, AtomicReference::get); - TimeGauge timeGauge = meterRegistry.get("my.time.gauge").timeGauge(); - assertEquals(1, publisher.writeTimeGauge(timeGauge).count()); - } - - @Test - void writeFunctionCounter() { - FunctionCounter counter = - FunctionCounter.builder("my.function_counter", 1d, Number::doubleValue) - .register(meterRegistry); - clock.add(config.step()); - assertEquals(1, publisher.writeFunctionCounter(counter).count()); - } - - @Test - void histogramBucketsHaveCorrectBaseUnit() { - Timer timer = - Timer.builder("my.timer") - .publishPercentileHistogram() - .serviceLevelObjectives(Duration.ofMillis(900), Duration.ofSeconds(1)) - .register(meterRegistry); - - timer.record(1, TimeUnit.SECONDS); - clock.add(config.step()); - - publisher.writeTimer(timer).forEach(LOG::debug); - - assertTrue( - publisher - .writeTimer(timer) - .anyMatch( - t -> t.equals("put mm.timers.my_timer_duration_seconds.bucket 60001 1 le=1.0\n"))); - assertTrue( - publisher - .writeTimer(timer) - .anyMatch( - t -> t.equals("put mm.timers.my_timer_duration_seconds.bucket 60001 0 le=0.9\n"))); - } - - @Test - void longTaskTimer() { - LongTaskTimer timer = LongTaskTimer.builder("my.timer").tags(tags).register(meterRegistry); - publisher.writeLongTaskTimer(timer).forEach(LOG::debug); - - assertTrue( - publisher - .writeLongTaskTimer(timer) - .anyMatch( - t -> t.equals("put mm.my_timer_duration_seconds.active_count 1 0 tag=value\n"))); - assertTrue( - publisher - .writeLongTaskTimer(timer) - .anyMatch( - t -> t.equals("put mm.my_timer_duration_seconds.duration_sum 1 0 tag=value\n"))); - assertTrue( - publisher - .writeLongTaskTimer(timer) - .anyMatch(t -> t.equals("put mm.my_timer_duration_seconds.max 1 0 tag=value\n"))); - } - - @Test - void writeCustomMetric() { - String name = "my.meter"; - double value = 1.0; - - Meter m = Utils.registerOrReplaceMeter(meterRegistry, name, tags, value); - String putString = publisher.writeCustomMetric(m).collect(Collectors.joining()); - assertEquals("put my.meter 1 1 statistics=VALUE tag=value\n", putString); - - m = Utils.registerOrReplaceMeter(meterRegistry, name, tags, value, Type.GAUGE); - putString = publisher.writeCustomMetric(m).collect(Collectors.joining()); - assertEquals("put mm.gauges.my_meter 1 1 statistics=VALUE tag=value\n", putString); - } +class PinStatsPublisherTest { + private static final Logger LOG = LoggerFactory.getLogger(PinStatsPublisherTest.class); + + private static final PinStatsConfig config = PinStatsConfig.DEFAULT; + private final MockClock clock = new MockClock(); + private final PinStatsMeterRegistry meterRegistry = new PinStatsMeterRegistry(config, clock); + private final PinStatsPublisher publisher = + new PinStatsPublisher(config, clock, TimeUnit.SECONDS, new PinStatsNamingConvention()); + private final Tags tags = Tags.of("tag", "value"); + + @Test + void writeCounter() { + String counterName = "my.counter"; + meterRegistry.counter(counterName, "t1", "v1", "t2", "v2"); + Counter counter = meterRegistry.get(counterName).counter(); + counter.increment(); + assertEquals(1, publisher.writeCounter(counter).count()); + assertEquals( + "put mm.counters.my_counter_total 1 " + clock.wallTime() + " t1=v1 t2=v2\n", + publisher.writeCounter(counter).collect(Collectors.joining())); + } + + @Test + void writeGauge() { + meterRegistry.gauge("my.gauge", 1d); + Gauge gauge = meterRegistry.get("my.gauge").gauge(); + assertEquals(1, publisher.writeGauge(gauge).count()); + } + + @Test + void writeTimeGauge() { + AtomicReference obj = new AtomicReference<>(1d); + meterRegistry + .more() + .timeGauge( + "my.time.gauge", Tags.empty(), obj, TimeUnit.SECONDS, AtomicReference::get); + TimeGauge timeGauge = meterRegistry.get("my.time.gauge").timeGauge(); + assertEquals(1, publisher.writeTimeGauge(timeGauge).count()); + } + + @Test + void writeFunctionCounter() { + FunctionCounter counter = + FunctionCounter.builder("my.function_counter", 1d, Number::doubleValue) + .register(meterRegistry); + clock.add(config.step()); + assertEquals(1, publisher.writeFunctionCounter(counter).count()); + } + + @Test + void histogramBucketsHaveCorrectBaseUnit() { + Timer timer = + Timer.builder("my.timer") + .publishPercentileHistogram() + .serviceLevelObjectives(Duration.ofMillis(900), Duration.ofSeconds(1)) + .register(meterRegistry); + + timer.record(1, TimeUnit.SECONDS); + clock.add(config.step()); + + publisher.writeTimer(timer).forEach(LOG::debug); + + assertTrue( + publisher + .writeTimer(timer) + .anyMatch( + t -> + t.equals( + "put mm.timers.my_timer_duration_seconds.bucket 60001 1 le=1.0\n"))); + assertTrue( + publisher + .writeTimer(timer) + .anyMatch( + t -> + t.equals( + "put mm.timers.my_timer_duration_seconds.bucket 60001 0 le=0.9\n"))); + } + + @Test + void longTaskTimer() { + LongTaskTimer timer = LongTaskTimer.builder("my.timer").tags(tags).register(meterRegistry); + publisher.writeLongTaskTimer(timer).forEach(LOG::debug); + + assertTrue( + publisher + .writeLongTaskTimer(timer) + .anyMatch( + t -> + t.equals( + "put mm.my_timer_duration_seconds.active_count 1 0 tag=value\n"))); + assertTrue( + publisher + .writeLongTaskTimer(timer) + .anyMatch( + t -> + t.equals( + "put mm.my_timer_duration_seconds.duration_sum 1 0 tag=value\n"))); + assertTrue( + publisher + .writeLongTaskTimer(timer) + .anyMatch( + t -> + t.equals( + "put mm.my_timer_duration_seconds.max 1 0 tag=value\n"))); + } + + @Test + void writeCustomMetric() { + String name = "my.meter"; + double value = 1.0; + + Meter m = Utils.registerOrReplaceMeter(meterRegistry, name, tags, value); + String putString = publisher.writeCustomMetric(m).collect(Collectors.joining()); + assertEquals("put my.meter 1 1 statistics=VALUE tag=value\n", putString); + + m = Utils.registerOrReplaceMeter(meterRegistry, name, tags, value, Type.GAUGE); + putString = publisher.writeCustomMetric(m).collect(Collectors.joining()); + assertEquals("put mm.gauges.my_meter 1 1 statistics=VALUE tag=value\n", putString); + } }