Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize universal tests #1492

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class GenericEventPublisherTest {
class GenericEventPublisherTest {
private AppEventListener<ResourceChangedEvent> mockResourceChangedEventListener;
private AppEventListener<ResourceChangedEvent> mockSlowListener;
private AppEventListener<ChildTestEvent> mockChildTestEventListener;
Expand Down Expand Up @@ -66,7 +66,7 @@ private <E extends AppEvent> AppEventListener<E> mockEventListener(Class<E> even
}

@Test
public void testSubscribe() {
void testSubscribe() {
sut.subscribe(mockResourceChangedEventListener);
sut.publishEvent(resourceChangedEvent);
sut.terminate();
Expand All @@ -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);
}
Expand All @@ -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++) {
Expand All @@ -102,7 +102,7 @@ public void testPublishEvent_subscribeThenPublish() {
}

@Test
public void testMultipleListeners() {
void testMultipleListeners() {
sut.subscribe(mockResourceChangedEventListener);
sut.subscribe(mockSlowListener);

Expand All @@ -120,7 +120,7 @@ public void testMultipleListeners() {
}

@Test
public void testMultipleListeners_differentEventTypes() {
void testMultipleListeners_differentEventTypes() {
sut.subscribe(mockResourceChangedEventListener);
sut.subscribe(mockSlowListener);
sut.subscribe(mockChildTestEventListener);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ResourceChangedEvent> sut;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> 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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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());
}
}
Loading