Skip to content

Commit

Permalink
Modernize universal tests
Browse files Browse the repository at this point in the history
commit-id:a1e3bceb
  • Loading branch information
tylerwowen committed Mar 6, 2024
1 parent 68d8174 commit ff21841
Show file tree
Hide file tree
Showing 8 changed files with 442 additions and 301 deletions.
Original file line number Diff line number Diff line change
@@ -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.events;

import static org.mockito.Matchers.any;
Expand All @@ -9,11 +24,10 @@
import static org.mockito.Mockito.when;

import java.util.concurrent.TimeUnit;

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 All @@ -36,10 +50,10 @@ public void setUp() {
mockChildTestEventListener = mockEventListener(ChildTestEvent.class);
mockSiblingTestEventListener = mockEventListener(SiblingTestEvent.class);
doAnswer(
invocation -> {
TimeUnit.MILLISECONDS.sleep(2);
return null;
})
invocation -> {
TimeUnit.MILLISECONDS.sleep(2);
return null;
})
.when(mockSlowListener)
.onEvent(any());
}
Expand All @@ -52,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 @@ -61,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 @@ -74,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 @@ -83,11 +97,12 @@ public void testPublishEvent_subscribeThenPublish() {
sut.terminate();

// All events are processed
verify(mockResourceChangedEventListener, times(numEvents)).onEvent(eq(resourceChangedEvent));
verify(mockResourceChangedEventListener, times(numEvents))
.onEvent(eq(resourceChangedEvent));
}

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

Expand All @@ -105,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

0 comments on commit ff21841

Please sign in to comment.