Skip to content

Commit

Permalink
refactor MetricFactoryTest
Browse files Browse the repository at this point in the history
  • Loading branch information
EddeCCC committed Nov 19, 2024
1 parent 741d79e commit 57af9fd
Showing 1 changed file with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<ObservableDoubleMeasurement> mockCallback;

// Interval to read and export metrics
private static final Duration INTERVAL = Duration.ofMillis(500);

Expand All @@ -39,23 +44,18 @@ static void beforeAll() {
}

@Test
void shouldRecordValueWithCallbackWhenCreatingGauge() throws InterruptedException {
AtomicBoolean valueRecorded = new AtomicBoolean(false);

Consumer<ObservableDoubleMeasurement> callback1 = (measurement) -> valueRecorded.set(true);
Consumer<ObservableDoubleMeasurement> callback2 =
void shouldRecordValueWithCallbackWhenCreatingGauge() {
Consumer<ObservableDoubleMeasurement> 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();
Expand Down

0 comments on commit 57af9fd

Please sign in to comment.