generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: issue with GlobalOpenTelemetry in tests (#152)
* add predestroy to opentelemetry factory it will trigger resetForTest if Environment is Environment.TEST * add predestroy to opentelemetry factory it will trigger resetForTest if Environment is Environment.TEST * Avoid bean pollution - Don't use ReactorHttpClient - Use @requires spec.name to avoid bean pollution - Annotate controller with @produces(MediaType.TEXT_PLAIN) and accept. Co-authored-by: Sergio del Amo <[email protected]>
- Loading branch information
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...ry/src/test/groovy/io/micronaut/tracing/instrument/util/HttpClientResetForTestSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package io.micronaut.tracing.instrument.util | ||
|
||
import groovy.util.logging.Slf4j | ||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.http.HttpRequest | ||
import io.micronaut.http.MediaType | ||
import io.micronaut.http.annotation.Controller | ||
import io.micronaut.http.annotation.Get | ||
import io.micronaut.http.annotation.Header | ||
import io.micronaut.http.annotation.Produces | ||
import io.micronaut.http.client.HttpClient | ||
import io.micronaut.reactor.http.client.ReactorHttpClient | ||
import io.micronaut.runtime.server.EmbeddedServer | ||
import io.micronaut.scheduling.annotation.ExecuteOn | ||
import io.micronaut.tracing.annotation.ContinueSpan | ||
import io.opentelemetry.extension.annotations.SpanAttribute | ||
import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter | ||
import reactor.core.publisher.Mono | ||
import spock.lang.AutoCleanup | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
import spock.util.concurrent.PollingConditions | ||
|
||
import static io.micronaut.scheduling.TaskExecutors.IO | ||
|
||
@Slf4j("LOG") | ||
class HttpClientResetForTestSpec extends Specification { | ||
|
||
@Shared | ||
@AutoCleanup | ||
EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer, [ | ||
'micronaut.application.name': 'test-app', | ||
'spec.name': 'HttpClientResetForTestSpec' | ||
]) | ||
|
||
@Shared | ||
@AutoCleanup | ||
HttpClient httpClient = HttpClient.create(embeddedServer.URL) | ||
|
||
void 'test pre destroy resetForTest'() { | ||
given: | ||
String tracingId = UUID.randomUUID() | ||
|
||
when: | ||
HttpRequest<Object> request = HttpRequest | ||
.GET("/test/test") | ||
.accept(MediaType.TEXT_PLAIN) | ||
.header("X-TrackingId", tracingId) | ||
String resp = httpClient.toBlocking().retrieve(request) | ||
|
||
then: | ||
noExceptionThrown() | ||
resp == tracingId | ||
} | ||
|
||
@Requires(property = "spec.name", value = "HttpClientResetForTestSpec") | ||
@Controller("/test") | ||
static class TestController { | ||
|
||
@ExecuteOn(IO) | ||
@Produces(MediaType.TEXT_PLAIN) | ||
@Get("/test") | ||
@ContinueSpan | ||
Mono<String> test(@SpanAttribute("tracing-annotation-span-attribute") | ||
@Header("X-TrackingId") String tracingId) { | ||
LOG.debug("test") | ||
return Mono.just(tracingId) | ||
} | ||
} | ||
} |