|
43 | 43 | import java.util.concurrent.ExecutionException;
|
44 | 44 | import java.util.concurrent.ExecutorService;
|
45 | 45 | import java.util.concurrent.Executors;
|
| 46 | +import java.util.concurrent.TimeUnit; |
46 | 47 | import java.util.concurrent.atomic.AtomicReference;
|
47 | 48 | import java.util.function.Consumer;
|
48 | 49 | import java.util.stream.Collectors;
|
@@ -958,6 +959,75 @@ void highConcurrencyOnSingleConnection() {
|
958 | 959 | pool.shutdown();
|
959 | 960 | }
|
960 | 961 |
|
| 962 | + @Test |
| 963 | + void spanEndsAfterBodyReceived() throws Exception { |
| 964 | + assumeTrue(options.isSpanEndsAfterBody()); |
| 965 | + |
| 966 | + String method = "GET"; |
| 967 | + URI uri = resolveAddress("/long-request"); |
| 968 | + |
| 969 | + int responseCode = |
| 970 | + doRequest( |
| 971 | + method, |
| 972 | + uri, |
| 973 | + // the time that server waits before completing the response |
| 974 | + Collections.singletonMap("delay", String.valueOf(TimeUnit.SECONDS.toMillis(1)))); |
| 975 | + |
| 976 | + assertThat(responseCode).isEqualTo(200); |
| 977 | + |
| 978 | + testing.waitAndAssertTraces( |
| 979 | + trace -> { |
| 980 | + trace.hasSpansSatisfyingExactly( |
| 981 | + span -> |
| 982 | + assertClientSpan(span, uri, method, 200, null) |
| 983 | + .hasNoParent() |
| 984 | + .hasStatus(StatusData.unset()), |
| 985 | + span -> assertServerSpan(span).hasParent(trace.getSpan(0))); |
| 986 | + SpanData span = trace.getSpan(0); |
| 987 | + // make sure the span is at least as long as the delay we set when sending the request |
| 988 | + assertThat( |
| 989 | + span.getEndEpochNanos() - span.getStartEpochNanos() |
| 990 | + >= TimeUnit.SECONDS.toNanos(1)) |
| 991 | + .describedAs("Span duration should be at least 1s") |
| 992 | + .isTrue(); |
| 993 | + }); |
| 994 | + } |
| 995 | + |
| 996 | + @Test |
| 997 | + void spanEndsAfterHeadersReceived() throws Exception { |
| 998 | + assumeTrue(options.isSpanEndsAfterHeaders()); |
| 999 | + |
| 1000 | + String method = "GET"; |
| 1001 | + URI uri = resolveAddress("/long-request"); |
| 1002 | + |
| 1003 | + int responseCode = |
| 1004 | + doRequest( |
| 1005 | + method, |
| 1006 | + uri, |
| 1007 | + // the time that server waits before completing the response, we expect the response |
| 1008 | + // headers to arrive much sooner |
| 1009 | + Collections.singletonMap("delay", String.valueOf(TimeUnit.SECONDS.toMillis(2)))); |
| 1010 | + |
| 1011 | + assertThat(responseCode).isEqualTo(200); |
| 1012 | + |
| 1013 | + testing.waitAndAssertTraces( |
| 1014 | + trace -> { |
| 1015 | + trace.hasSpansSatisfyingExactly( |
| 1016 | + span -> |
| 1017 | + assertClientSpan(span, uri, method, 200, null) |
| 1018 | + .hasNoParent() |
| 1019 | + .hasStatus(StatusData.unset()), |
| 1020 | + span -> assertServerSpan(span).hasParent(trace.getSpan(0))); |
| 1021 | + SpanData span = trace.getSpan(0); |
| 1022 | + // verify that the span length is less than the delay used to complete the response body |
| 1023 | + assertThat( |
| 1024 | + span.getEndEpochNanos() - span.getStartEpochNanos() |
| 1025 | + <= TimeUnit.SECONDS.toNanos(2)) |
| 1026 | + .describedAs("Span duration should be less than 2s") |
| 1027 | + .isTrue(); |
| 1028 | + }); |
| 1029 | + } |
| 1030 | + |
961 | 1031 | // Visible for spock bridge.
|
962 | 1032 | SpanDataAssert assertClientSpan(
|
963 | 1033 | SpanDataAssert span,
|
@@ -1053,7 +1123,7 @@ static SpanDataAssert assertServerSpan(SpanDataAssert span) {
|
1053 | 1123 | return span.hasName("test-http-server").hasKind(SpanKind.SERVER);
|
1054 | 1124 | }
|
1055 | 1125 |
|
1056 |
| - protected int doRequest(String method, URI uri) throws Exception { |
| 1126 | + private int doRequest(String method, URI uri) throws Exception { |
1057 | 1127 | return doRequest(method, uri, Collections.emptyMap());
|
1058 | 1128 | }
|
1059 | 1129 |
|
|
0 commit comments