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

add more tests for PolledMeter #1008

Merged
merged 1 commit into from Dec 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -26,9 +26,15 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;

public class PolledMeterTest {

Expand Down Expand Up @@ -197,4 +203,71 @@ public void poll() {
Assertions.assertEquals(1.0, g.value(), 1e-12);
Assertions.assertEquals(1, c.count());
}

@Test
public void monitorValueLongAdder() {
final Registry r = new DefaultRegistry(Clock.SYSTEM, p -> null);
final Id id = r.createId("test");

final LongAdder v = PolledMeter.using(r)
.withName("test")
.monitorValue(new LongAdder());

v.increment();
PolledMeter.update(r);
Assertions.assertEquals(1.0, r.gauge(id).value());

v.decrement();
PolledMeter.update(r);
Assertions.assertEquals(0.0, r.gauge(id).value());

v.add(50);
PolledMeter.update(r);
Assertions.assertEquals(50.0, r.gauge(id).value());
}

@Test
public void monitorSizeOfList() {
final Registry r = new DefaultRegistry(Clock.SYSTEM, p -> null);
final Id id = r.createId("test");

final List<String> list = PolledMeter.using(r)
.withName("test")
.monitorSize(Collections.synchronizedList(new ArrayList<String>()));

list.add("a");
PolledMeter.update(r);
Assertions.assertEquals(1.0, r.gauge(id).value());

list.add("b");
PolledMeter.update(r);
Assertions.assertEquals(2.0, r.gauge(id).value());

list.clear();
PolledMeter.update(r);
Assertions.assertEquals(0.0, r.gauge(id).value());
}

@Test
public void monitorSizeOfMap() {
final Registry r = new DefaultRegistry(Clock.SYSTEM, p -> null);
final Id id = r.createId("test");

final Map<String, String> map = PolledMeter.using(r)
.withName("test")
.monitorSize(new ConcurrentHashMap<String, String>());

map.put("a", "a-value");
PolledMeter.update(r);
Assertions.assertEquals(1.0, r.gauge(id).value());

map.put("b", "b-value");
PolledMeter.update(r);
Assertions.assertEquals(2.0, r.gauge(id).value());

map.remove("a");
map.remove("b");
PolledMeter.update(r);
Assertions.assertEquals(0.0, r.gauge(id).value());
}
}