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

Provide tooling for unit testing #61

Open
rc-mairovichaa opened this issue Sep 23, 2024 · 1 comment
Open

Provide tooling for unit testing #61

rc-mairovichaa opened this issue Sep 23, 2024 · 1 comment
Labels
enhancement New feature or request
Milestone

Comments

@rc-mairovichaa
Copy link
Collaborator

To simplify unit testing it would be nice to have utility methods, which will ease retrieval of current Metric's values.

Example, which could be used as the starting point - showing needs at least some of our clients have

import com.ringcentral.platform.metrics.MetricInstance;
import com.ringcentral.platform.metrics.MetricKey;
import com.ringcentral.platform.metrics.MetricRegistry;
import com.ringcentral.platform.metrics.counter.Counter;
import com.ringcentral.platform.metrics.dimensions.MetricDimensionValues;
import com.ringcentral.platform.metrics.var.longVar.LongVar;
import org.awaitility.Awaitility;

import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Optional;
import java.util.stream.StreamSupport;

import static com.ringcentral.platform.metrics.dimensions.MetricDimensionValues.NO_DIMENSION_VALUES;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class MetricAssertions {

    public static void assertLongVar(
            MetricRegistry registry,
            long expectedValue,
            MetricKey name,
            MetricDimensionValues dimensionValues,
            Duration maxWaitTime) {

        Awaitility.await().atMost(maxWaitTime)
                .untilAsserted(() -> {
                    assertEquals(expectedValue, findMetric(registry, name, dimensionValues)
                            .map(m -> m.valueOf(LongVar.LONG_VALUE))
                            .orElse(0L));
                });
    }

    public static void assertLongVar(
            MetricRegistry registry,
            long expectedValue,
            MetricKey name) {

        assertLongVar(registry, expectedValue, name, NO_DIMENSION_VALUES, Duration.of(5, ChronoUnit.SECONDS));
    }

    public static void assertCounterMetric(
            MetricRegistry registry,
            long expectedValue,
            MetricKey name,
            MetricDimensionValues dimensionValues,
            Duration maxWaitTime) {

        Awaitility.await().pollInterval(Duration.ofMillis(10)).atMost(maxWaitTime)
                .untilAsserted(() -> assertEquals(expectedValue, getCounterMetric(registry, name, dimensionValues)));
    }

    public static void assertCounterMetric(
            MetricRegistry registry,
            long expectedValue,
            MetricKey name) {

        assertCounterMetric(registry, expectedValue, name, NO_DIMENSION_VALUES, Duration.of(5, ChronoUnit.SECONDS));
    }

    public static long getCounterMetric(
            MetricRegistry registry,
            MetricKey name,
            MetricDimensionValues dimensionValues) {

        return findMetric(registry, name, dimensionValues)
                .map(m -> (Long) m.valueOf(Counter.COUNT))
                .orElse(0L);
    }

    public static long getCounterMetric(MetricRegistry registry, MetricKey name) {
        return getCounterMetric(registry, name, NO_DIMENSION_VALUES);
    }

    public static void assertTimerCountMetric(
            MetricRegistry registry,
            long expectedValue,
            MetricKey name,
            MetricDimensionValues dimensionValues,
            Duration maxWaitTime) {

        Awaitility.await().atMost(maxWaitTime)
                .untilAsserted(() -> {
                    assertEquals(expectedValue, findMetric(registry, name, dimensionValues)
                            .map(m -> m.valueOf(Counter.COUNT))
                            .orElse(0L));
                });
    }

    public static void assertTimerCountMetric(
            MetricRegistry registry,
            long expectedValue,
            MetricKey name) {

        assertTimerCountMetric(registry, expectedValue, name, NO_DIMENSION_VALUES, Duration.of(5, ChronoUnit.SECONDS));
    }

    public static void assertMetricExist(MetricRegistry registry, MetricKey name) {
        assertTrue(
                registry.metrics()
                        .entrySet()
                        .stream()
                        .anyMatch(m -> m.getKey().equals(name)),
                "Metric " + name + " should be presented"
        );
    }

    public static void assertMetricWithPrefixExist(MetricRegistry registry, MetricKey prefix) {
        assertTrue(
                registry.metrics()
                        .entrySet()
                        .stream()
                        .anyMatch(m -> m.getKey().toString().startsWith(prefix.toString())),
                "Metric prefix should be presented"
        );
    }

    private static Optional<MetricInstance> findMetric(MetricRegistry registry, MetricKey name, MetricDimensionValues dimensionValues) {
        return registry.metrics().entrySet()
                .stream()
                .filter(m -> m.getKey().equals(name))
                .flatMap(m -> StreamSupport.stream(m.getValue().spliterator(), false))
                .filter(metric -> dimensionValues.list().stream()
                        .allMatch(d -> d.value().equals(metric.valueOf(d.dimension()))))
                .findFirst();
    }
}

@rc-mairovichaa rc-mairovichaa added the enhancement New feature or request label Sep 23, 2024
@jeremyspb
Copy link

It might be great also to have find out metric measurable by metricname and metricdimensionvalues. If null - only by metricname.

@devromik devromik added this to the 4.1 milestone Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants