diff --git a/inspectit-gepard-agent/src/test/java/rocks/inspectit/gepard/agent/internal/metrics/MetricFactoryTest.java b/inspectit-gepard-agent/src/test/java/rocks/inspectit/gepard/agent/internal/metrics/MetricFactoryTest.java index 18d504e..8b8f044 100644 --- a/inspectit-gepard-agent/src/test/java/rocks/inspectit/gepard/agent/internal/metrics/MetricFactoryTest.java +++ b/inspectit-gepard-agent/src/test/java/rocks/inspectit/gepard/agent/internal/metrics/MetricFactoryTest.java @@ -1,7 +1,7 @@ /* (C) 2024 */ package rocks.inspectit.gepard.agent.internal.metrics; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.*; import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.api.metrics.ObservableDoubleGauge; @@ -11,14 +11,19 @@ import io.opentelemetry.sdk.metrics.SdkMeterProvider; import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader; import java.time.Duration; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Consumer; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import rocks.inspectit.gepard.agent.internal.otel.OpenTelemetryAccessor; +@ExtendWith(MockitoExtension.class) class MetricFactoryTest { + @Mock private Consumer mockCallback; + // Interval to read and export metrics private static final Duration INTERVAL = Duration.ofMillis(500); @@ -39,23 +44,18 @@ static void beforeAll() { } @Test - void shouldRecordValueWithCallbackWhenCreatingGauge() throws InterruptedException { - AtomicBoolean valueRecorded = new AtomicBoolean(false); - - Consumer callback1 = (measurement) -> valueRecorded.set(true); - Consumer callback2 = + void shouldRecordValueWithCallbackWhenCreatingGauge() { + Consumer errorCallback = (measurement) -> { throw new RuntimeException("Test exception"); }; ObservableDoubleGauge gauge1 = - MetricFactory.createObservableDoubleGauge("test-gauge", callback1); + MetricFactory.createObservableDoubleGauge("test-gauge", mockCallback); ObservableDoubleGauge gauge2 = - MetricFactory.createObservableDoubleGauge("fail-gauge", callback2); - // Wait for MetricReader - Thread.sleep(INTERVAL.toMillis() + 100); + MetricFactory.createObservableDoubleGauge("fail-gauge", errorCallback); - assertTrue(valueRecorded.get()); + verify(mockCallback, timeout(INTERVAL.toMillis() + 100)).accept(any()); gauge1.close(); gauge2.close();