Skip to content

Commit 48b2057

Browse files
committed
Add test for async
1 parent 5da7503 commit 48b2057

File tree

6 files changed

+85
-0
lines changed

6 files changed

+85
-0
lines changed

WORK_IN_PROGRESS.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
./gradlew :instrumentation:spring:spring-webmvc:spring-webmvc-3.1:javaagent:test --tests SpringBootBasedTest.deferredResult
3+

instrumentation/spring/spring-webmvc/spring-webmvc-common/testing/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/boot/AbstractSpringBootBasedTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static io.opentelemetry.instrumentation.testing.junit.code.SemconvCodeStabilityUtil.codeFunctionSuffixAssertions;
1010
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.AUTH_ERROR;
1111
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.CAPTURE_HEADERS;
12+
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.DEFERRED_RESULT;
1213
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.EXCEPTION;
1314
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.INDEXED_CHILD;
1415
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.LOGIN;
@@ -144,6 +145,37 @@ void testCharacterEncodingOfTestPassword(String testPassword) {
144145
.hasKind(SpanKind.INTERNAL)));
145146
}
146147

148+
@Test
149+
void deferredResult() {
150+
AggregatedHttpResponse response =
151+
client.execute(request(DEFERRED_RESULT, "GET")).aggregate().join();
152+
153+
assertThat(response.status().code()).isEqualTo(DEFERRED_RESULT.getStatus());
154+
assertThat(response.contentUtf8()).isEqualTo(DEFERRED_RESULT.getBody());
155+
156+
testing()
157+
.waitAndAssertTraces(
158+
trace ->
159+
trace.hasSpansSatisfyingExactly(
160+
span -> {
161+
assertServerSpan(span, "GET", DEFERRED_RESULT, DEFERRED_RESULT.getStatus());
162+
span.hasNoParent();
163+
},
164+
span ->
165+
assertHandlerSpan(span, "GET", DEFERRED_RESULT)
166+
.hasParent(trace.getSpan(0))),
167+
trace ->
168+
trace.hasSpansSatisfyingExactly(
169+
span ->
170+
span.hasName("deferred-result-child")
171+
.hasKind(SpanKind.INTERNAL)
172+
.hasNoParent()
173+
.hasTotalAttributeCount(0),
174+
span ->
175+
assertHandlerSpan(span, "GET", DEFERRED_RESULT)
176+
.hasParent(trace.getSpan(0))));
177+
}
178+
147179
@Override
148180
protected List<Consumer<SpanDataAssert>> errorPageSpanAssertions(
149181
String method, ServerEndpoint endpoint) {
@@ -228,6 +260,8 @@ private static String getHandlerSpanName(ServerEndpoint endpoint) {
228260
return "TestController.captureHeaders";
229261
} else if (INDEXED_CHILD.equals(endpoint)) {
230262
return "TestController.indexedChild";
263+
} else if (DEFERRED_RESULT.equals(endpoint)) {
264+
return "TestController.deferredResult";
231265
}
232266
return "TestController." + endpoint.name().toLowerCase(Locale.ROOT);
233267
}

instrumentation/spring/spring-webmvc/spring-webmvc-common/testing/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/boot/AppConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package io.opentelemetry.instrumentation.spring.webmvc.boot;
77

88
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
import org.springframework.scheduling.annotation.EnableAsync;
910

1011
@SpringBootApplication
12+
@EnableAsync
1113
public class AppConfig {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.spring.webmvc.boot;
7+
8+
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.DEFERRED_RESULT;
9+
10+
import io.opentelemetry.api.GlobalOpenTelemetry;
11+
import io.opentelemetry.api.trace.Span;
12+
import io.opentelemetry.api.trace.Tracer;
13+
import io.opentelemetry.context.Scope;
14+
import org.springframework.scheduling.annotation.Async;
15+
import org.springframework.stereotype.Service;
16+
import org.springframework.web.context.request.async.DeferredResult;
17+
18+
@Service
19+
public class TestBean {
20+
21+
private static final Tracer tracer = GlobalOpenTelemetry.getTracer("test");
22+
23+
@Async
24+
public void asyncDependencyCall(DeferredResult<String> deferredResult) {
25+
Span span = tracer.spanBuilder("deferred-result-child").startSpan();
26+
try (Scope ignored = span.makeCurrent()) {
27+
deferredResult.setResult(DEFERRED_RESULT.getBody());
28+
} finally {
29+
span.end();
30+
}
31+
}
32+
}

instrumentation/spring/spring-webmvc/spring-webmvc-common/testing/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/boot/TestController.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS;
1717

1818
import java.util.Objects;
19+
import org.springframework.beans.factory.annotation.Autowired;
1920
import org.springframework.http.HttpStatus;
2021
import org.springframework.http.ResponseEntity;
2122
import org.springframework.stereotype.Controller;
@@ -25,11 +26,14 @@
2526
import org.springframework.web.bind.annotation.RequestMapping;
2627
import org.springframework.web.bind.annotation.RequestParam;
2728
import org.springframework.web.bind.annotation.ResponseBody;
29+
import org.springframework.web.context.request.async.DeferredResult;
2830
import org.springframework.web.servlet.view.RedirectView;
2931

3032
@Controller
3133
public class TestController {
3234

35+
@Autowired private TestBean testBean;
36+
3337
@RequestMapping("/basicsecured/endpoint")
3438
@ResponseBody
3539
String secureEndpoint() {
@@ -100,6 +104,14 @@ String indexedChild(@RequestParam("id") String id) {
100104
});
101105
}
102106

107+
@RequestMapping("/deferred-result")
108+
@ResponseBody
109+
DeferredResult<String> deferredResult() {
110+
DeferredResult<String> deferredResult = new DeferredResult<>();
111+
testBean.asyncDependencyCall(deferredResult);
112+
return deferredResult;
113+
}
114+
103115
@ExceptionHandler
104116
ResponseEntity<String> handleException(Throwable throwable) {
105117
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR.value())

testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/ServerEndpoint.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public class ServerEndpoint {
4848
new ServerEndpoint("AUTH_ERROR", "basicsecured/endpoint", 401, null);
4949
public static final ServerEndpoint INDEXED_CHILD =
5050
new ServerEndpoint("INDEXED_CHILD", "child", 200, "success");
51+
public static final ServerEndpoint DEFERRED_RESULT =
52+
new ServerEndpoint("DEFERRED_RESULT", "deferred-result", 200, "deferred result");
5153

5254
public static final String ID_ATTRIBUTE_NAME = "test.request.id";
5355
public static final String ID_PARAMETER_NAME = "id";

0 commit comments

Comments
 (0)