Skip to content

Commit

Permalink
test: add util tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EddeCCC committed Oct 14, 2024
1 parent 72c2b42 commit 2ea1c8b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ private SpanUtil() {}
* prevent span duplicates. For example, we would like to create a span for a method, which is
* already recorded by OpenTelemetry. In this case, we should not create a new span.
*
* @param spanName the name of the span with the format 'simple-class-name.method-name', for
* instance 'SpanUtil.spanAlreadyExists'
* @param spanName the name of the span with the format 'SimpleClassName.methodName', for instance
* 'SpanUtil.spanAlreadyExists'
* @return true, if the current span uses the provided span name
*/
public static boolean spanAlreadyExists(String spanName)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* (C) 2024 */
package rocks.inspectit.gepard.agent.instrumentation.hook.action.util;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Scope;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class SpanUtilTest {

private Tracer tracer;

private final String spanName = "SpanUtilTest.method";

@BeforeEach
void beforeEach() {
OpenTelemetry openTelemetry = OpenTelemetrySdk.builder().buildAndRegisterGlobal();
tracer = openTelemetry.getTracer("inspectit-gepard");
}

@AfterEach
void afterEach() {
GlobalOpenTelemetry.resetForTest();
}

@Test
void shouldReturnTrueWhenSpanExists() throws Exception {
Span span = tracer.spanBuilder(spanName).startSpan();
Scope scope = span.makeCurrent();

boolean exists = SpanUtil.spanAlreadyExists(spanName);

assertTrue(exists);

scope.close();
span.end();
}

@Test
void shouldReturnFalseWhenNoSpanExists() throws Exception {
boolean exists = SpanUtil.spanAlreadyExists(spanName);

assertFalse(exists);
}

@Test
void shouldReturnFalseWhenOtherSpanExists() throws Exception {
Span span = tracer.spanBuilder("dummy").startSpan();
Scope scope = span.makeCurrent();

boolean exists = SpanUtil.spanAlreadyExists(spanName);

assertFalse(exists);

scope.close();
span.end();
}
}

0 comments on commit 2ea1c8b

Please sign in to comment.