Skip to content

Commit 454379f

Browse files
committed
fixing vertx client test
1 parent e092f55 commit 454379f

File tree

2 files changed

+158
-118
lines changed

2 files changed

+158
-118
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
* Copyright The Hypertrace Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opentelemetry.javaagent.instrumentation.hypertrace.vertx;
18+
19+
import io.opentelemetry.proto.trace.v1.Span;
20+
import io.vertx.core.Vertx;
21+
import io.vertx.core.VertxOptions;
22+
import io.vertx.core.buffer.Buffer;
23+
import io.vertx.core.http.HttpClient;
24+
import io.vertx.core.http.HttpClientOptions;
25+
import io.vertx.core.http.HttpClientRequest;
26+
import io.vertx.core.http.HttpMethod;
27+
import java.util.List;
28+
import java.util.concurrent.CountDownLatch;
29+
import java.util.concurrent.TimeoutException;
30+
import org.hypertrace.agent.testing.AbstractInstrumenterTest;
31+
import org.hypertrace.agent.testing.TestHttpServer;
32+
import org.junit.jupiter.api.AfterAll;
33+
import org.junit.jupiter.api.Assertions;
34+
import org.junit.jupiter.api.BeforeAll;
35+
import org.junit.jupiter.api.Test;
36+
37+
public class VertxClientInstrumentationPostTests extends AbstractInstrumenterTest {
38+
39+
private static final TestHttpServer testHttpServer = new TestHttpServer();
40+
private final Vertx vertx = Vertx.vertx(new VertxOptions());
41+
private final HttpClientOptions clientOptions = new HttpClientOptions();
42+
private final HttpClient httpClient = vertx.createHttpClient(clientOptions);
43+
44+
@BeforeAll
45+
public static void startServer() throws Exception {
46+
testHttpServer.start();
47+
}
48+
49+
@AfterAll
50+
public static void closeServer() throws Exception {
51+
testHttpServer.close();
52+
}
53+
54+
@Test
55+
public void postJson_write_end() throws TimeoutException, InterruptedException {
56+
String uri = String.format("http://localhost:%d/echo", testHttpServer.port());
57+
58+
CountDownLatch countDownLatch = new CountDownLatch(1);
59+
HttpClientRequest request = httpClient.requestAbs(HttpMethod.POST, uri);
60+
request = request.putHeader("Content-Type", "application/json");
61+
request.setChunked(true);
62+
VertxClientInstrumentationTest.BufferHandler bufferHandler =
63+
new VertxClientInstrumentationTest.BufferHandler(countDownLatch);
64+
VertxClientInstrumentationTest.ResponseHandler responseHandler =
65+
new VertxClientInstrumentationTest.ResponseHandler(bufferHandler);
66+
67+
request
68+
.handler(responseHandler)
69+
.write("write")
70+
.write(Buffer.buffer().appendString(" buffer"))
71+
.write(" str_encoding ", "utf-8")
72+
.end();
73+
countDownLatch.await();
74+
75+
TEST_WRITER.waitForTraces(1);
76+
List<List<Span>> traces =
77+
TEST_WRITER.waitForSpans(
78+
1,
79+
span ->
80+
span.getKind().equals(Span.SpanKind.SPAN_KIND_SERVER)
81+
|| span.getKind().equals(Span.SpanKind.SPAN_KIND_INTERNAL));
82+
Assertions.assertEquals(1, traces.size());
83+
Span clientSpan = traces.get(0).get(0);
84+
Assertions.assertEquals(
85+
"write buffer str_encoding ",
86+
TEST_WRITER.getAttributesMap(clientSpan).get("http.request.body").getStringValue());
87+
}
88+
89+
@Test
90+
public void postJson_write_end_string() throws TimeoutException, InterruptedException {
91+
String uri = String.format("http://localhost:%d/echo", testHttpServer.port());
92+
93+
CountDownLatch countDownLatch = new CountDownLatch(1);
94+
HttpClientRequest request = httpClient.requestAbs(HttpMethod.POST, uri);
95+
request = request.putHeader("Content-Type", "application/json");
96+
request.setChunked(true);
97+
VertxClientInstrumentationTest.BufferHandler bufferHandler =
98+
new VertxClientInstrumentationTest.BufferHandler(countDownLatch);
99+
VertxClientInstrumentationTest.ResponseHandler responseHandler =
100+
new VertxClientInstrumentationTest.ResponseHandler(bufferHandler);
101+
102+
request
103+
.handler(responseHandler)
104+
.write("write")
105+
.write(Buffer.buffer().appendString(" buffer"))
106+
.write(" str_encoding ", "utf-8")
107+
.end("end");
108+
countDownLatch.await();
109+
110+
TEST_WRITER.waitForTraces(1);
111+
List<List<Span>> traces =
112+
TEST_WRITER.waitForSpans(1, span -> span.getKind().equals(Span.SpanKind.SPAN_KIND_SERVER));
113+
Assertions.assertEquals(1, traces.size(), String.format("was: %d", traces.size()));
114+
Span clientSpan = traces.get(0).get(0);
115+
Assertions.assertEquals(
116+
"write buffer str_encoding end",
117+
TEST_WRITER.getAttributesMap(clientSpan).get("http.request.body").getStringValue());
118+
}
119+
120+
@Test
121+
public void postJson_write_end_buffer() throws TimeoutException, InterruptedException {
122+
String uri = String.format("http://localhost:%d/echo", testHttpServer.port());
123+
124+
CountDownLatch countDownLatch = new CountDownLatch(1);
125+
HttpClientRequest request = httpClient.requestAbs(HttpMethod.POST, uri);
126+
request = request.putHeader("Content-Type", "application/json");
127+
request.setChunked(true);
128+
VertxClientInstrumentationTest.BufferHandler bufferHandler =
129+
new VertxClientInstrumentationTest.BufferHandler(countDownLatch);
130+
VertxClientInstrumentationTest.ResponseHandler responseHandler =
131+
new VertxClientInstrumentationTest.ResponseHandler(bufferHandler);
132+
133+
request
134+
.handler(responseHandler)
135+
.write("write")
136+
.write(Buffer.buffer().appendString(" buffer"))
137+
.write(" str_encoding ", "utf-8")
138+
.end(Buffer.buffer("end"));
139+
countDownLatch.await();
140+
141+
TEST_WRITER.waitForTraces(1);
142+
List<List<Span>> traces =
143+
TEST_WRITER.waitForSpans(
144+
1,
145+
span ->
146+
!span.getKind().equals(Span.SpanKind.SPAN_KIND_CLIENT)
147+
|| span.getAttributesList().stream()
148+
.noneMatch(
149+
keyValue ->
150+
keyValue.getKey().equals("http.url")
151+
&& keyValue.getValue().getStringValue().contains("/echo")));
152+
Assertions.assertEquals(1, traces.size(), String.format("was: %d", traces.size()));
153+
Span clientSpan = traces.get(0).get(0);
154+
Assertions.assertEquals(
155+
"write buffer str_encoding end",
156+
TEST_WRITER.getAttributesMap(clientSpan).get("http.request.body").getStringValue());
157+
}
158+
}

instrumentation/vertx/vertx-web-3.0/src/test/java/io/opentelemetry/javaagent/instrumentation/hypertrace/vertx/VertxClientInstrumentationTest.java

Lines changed: 0 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package io.opentelemetry.javaagent.instrumentation.hypertrace.vertx;
1818

19-
import io.opentelemetry.proto.trace.v1.Span;
2019
import io.vertx.core.Handler;
2120
import io.vertx.core.Vertx;
2221
import io.vertx.core.VertxOptions;
@@ -26,14 +25,9 @@
2625
import io.vertx.core.http.HttpClientRequest;
2726
import io.vertx.core.http.HttpClientResponse;
2827
import io.vertx.core.http.HttpMethod;
29-
import java.util.List;
3028
import java.util.Map;
3129
import java.util.concurrent.CountDownLatch;
32-
import java.util.concurrent.TimeoutException;
3330
import org.hypertrace.agent.testing.AbstractHttpClientTest;
34-
import org.junit.jupiter.api.Assertions;
35-
import org.junit.jupiter.api.Disabled;
36-
import org.junit.jupiter.api.Test;
3731

3832
public class VertxClientInstrumentationTest extends AbstractHttpClientTest {
3933

@@ -120,116 +114,4 @@ public void handle(Buffer responseBodyBuffer) {
120114
countDownLatch.countDown();
121115
}
122116
}
123-
124-
@Test
125-
public void postJson_write_end() throws TimeoutException, InterruptedException {
126-
String uri = String.format("http://localhost:%d/echo", testHttpServer.port());
127-
128-
CountDownLatch countDownLatch = new CountDownLatch(1);
129-
HttpClientRequest request = httpClient.requestAbs(HttpMethod.POST, uri);
130-
request = request.putHeader("Content-Type", "application/json");
131-
request.setChunked(true);
132-
BufferHandler bufferHandler = new BufferHandler(countDownLatch);
133-
ResponseHandler responseHandler = new ResponseHandler(bufferHandler);
134-
135-
request
136-
.handler(responseHandler)
137-
.write("write")
138-
.write(Buffer.buffer().appendString(" buffer"))
139-
.write(" str_encoding ", "utf-8")
140-
.end();
141-
countDownLatch.await();
142-
143-
TEST_WRITER.waitForTraces(1);
144-
List<List<Span>> traces =
145-
TEST_WRITER.waitForSpans(
146-
1,
147-
span ->
148-
span.getKind().equals(Span.SpanKind.SPAN_KIND_SERVER)
149-
|| span.getKind().equals(Span.SpanKind.SPAN_KIND_INTERNAL));
150-
Assertions.assertEquals(1, traces.size());
151-
Span clientSpan = traces.get(0).get(0);
152-
Assertions.assertEquals(
153-
"write buffer str_encoding ",
154-
TEST_WRITER.getAttributesMap(clientSpan).get("http.request.body").getStringValue());
155-
}
156-
157-
@Test
158-
public void postJson_write_end_string() throws TimeoutException, InterruptedException {
159-
String uri = String.format("http://localhost:%d/echo", testHttpServer.port());
160-
161-
CountDownLatch countDownLatch = new CountDownLatch(1);
162-
HttpClientRequest request = httpClient.requestAbs(HttpMethod.POST, uri);
163-
request = request.putHeader("Content-Type", "application/json");
164-
request.setChunked(true);
165-
BufferHandler bufferHandler = new BufferHandler(countDownLatch);
166-
ResponseHandler responseHandler = new ResponseHandler(bufferHandler);
167-
168-
request
169-
.handler(responseHandler)
170-
.write("write")
171-
.write(Buffer.buffer().appendString(" buffer"))
172-
.write(" str_encoding ", "utf-8")
173-
.end("end");
174-
countDownLatch.await();
175-
176-
TEST_WRITER.waitForTraces(1);
177-
List<List<Span>> traces =
178-
TEST_WRITER.waitForSpans(
179-
1,
180-
span ->
181-
span.getKind().equals(Span.SpanKind.SPAN_KIND_SERVER)
182-
|| span.getKind().equals(Span.SpanKind.SPAN_KIND_INTERNAL)
183-
|| span.getAttributesList().stream()
184-
.noneMatch(
185-
keyValue ->
186-
keyValue.getKey().equals("http.response.body")
187-
&& keyValue
188-
.getValue()
189-
.getStringValue()
190-
.contains("write buffer str_encoding end")));
191-
Assertions.assertEquals(1, traces.size(), String.format("was: %d", traces.size()));
192-
Span clientSpan = traces.get(0).get(0);
193-
Assertions.assertEquals(
194-
"write buffer str_encoding end",
195-
TEST_WRITER.getAttributesMap(clientSpan).get("http.request.body").getStringValue());
196-
}
197-
198-
@Test
199-
@Disabled("This is flaky on github actions!!")
200-
public void postJson_write_end_buffer() throws TimeoutException, InterruptedException {
201-
String uri = String.format("http://localhost:%d/echo", testHttpServer.port());
202-
203-
CountDownLatch countDownLatch = new CountDownLatch(1);
204-
HttpClientRequest request = httpClient.requestAbs(HttpMethod.POST, uri);
205-
request = request.putHeader("Content-Type", "application/json");
206-
request.setChunked(true);
207-
BufferHandler bufferHandler = new BufferHandler(countDownLatch);
208-
ResponseHandler responseHandler = new ResponseHandler(bufferHandler);
209-
210-
request
211-
.handler(responseHandler)
212-
.write("write")
213-
.write(Buffer.buffer().appendString(" buffer"))
214-
.write(" str_encoding ", "utf-8")
215-
.end(Buffer.buffer("end"));
216-
countDownLatch.await();
217-
218-
TEST_WRITER.waitForTraces(1);
219-
List<List<Span>> traces =
220-
TEST_WRITER.waitForSpans(
221-
1,
222-
span ->
223-
!span.getKind().equals(Span.SpanKind.SPAN_KIND_CLIENT)
224-
|| span.getAttributesList().stream()
225-
.noneMatch(
226-
keyValue ->
227-
keyValue.getKey().equals("http.url")
228-
&& keyValue.getValue().getStringValue().contains("/echo")));
229-
Assertions.assertEquals(1, traces.size(), String.format("was: %d", traces.size()));
230-
Span clientSpan = traces.get(0).get(0);
231-
Assertions.assertEquals(
232-
"write buffer str_encoding end",
233-
TEST_WRITER.getAttributesMap(clientSpan).get("http.request.body").getStringValue());
234-
}
235117
}

0 commit comments

Comments
 (0)