Skip to content

Commit b6ed6bb

Browse files
committed
update test
1 parent 68ff706 commit b6ed6bb

File tree

1 file changed

+48
-22
lines changed

1 file changed

+48
-22
lines changed

exporters/zipkin/src/test/java/io/opentelemetry/exporter/zipkin/ZipkinSpanExporterTest.java

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import static io.opentelemetry.exporter.zipkin.ZipkinTestUtil.zipkinSpanBuilder;
1010
import static org.assertj.core.api.Assertions.assertThat;
1111
import static org.assertj.core.api.Assertions.assertThatThrownBy;
12-
import static org.junit.jupiter.api.Assertions.assertFalse;
1312
import static org.junit.jupiter.api.Assertions.assertTrue;
1413
import static org.mockito.Mockito.doThrow;
1514
import static org.mockito.Mockito.verify;
@@ -26,6 +25,7 @@
2625
import java.net.InetAddress;
2726
import java.time.Duration;
2827
import java.util.Collections;
28+
import java.util.List;
2929
import java.util.concurrent.TimeUnit;
3030
import java.util.concurrent.atomic.AtomicBoolean;
3131
import org.junit.jupiter.api.Test;
@@ -38,7 +38,6 @@
3838
import zipkin2.reporter.BytesMessageSender;
3939
import zipkin2.reporter.Encoding;
4040
import zipkin2.reporter.SpanBytesEncoder;
41-
import zipkin2.reporter.okhttp3.OkHttpSender;
4241

4342
@ExtendWith(MockitoExtension.class)
4443
class ZipkinSpanExporterTest {
@@ -253,25 +252,52 @@ void stringRepresentation() {
253252

254253
@Test
255254
void testSuppressInstrumentation() {
256-
AtomicBoolean suppressInstrumentation =
257-
new AtomicBoolean(InstrumentationUtil.shouldSuppressInstrumentation(Context.current()));
258-
259-
assertFalse(suppressInstrumentation.get());
260-
261-
InstrumentationUtil.suppressInstrumentation(
262-
() -> {
263-
try (BytesMessageSender sender =
264-
OkHttpSender.newBuilder()
265-
.endpoint("https://localhost")
266-
.encoding(Encoding.PROTO3)
267-
.build()) {
268-
sender.send(Collections.singletonList(new byte[0]));
269-
} catch (IOException e) {
270-
// it always goes here
271-
suppressInstrumentation.set(
272-
InstrumentationUtil.shouldSuppressInstrumentation(Context.current()));
273-
}
274-
});
275-
assertTrue(suppressInstrumentation.get());
255+
TestSpanData testSpanData = spanBuilder().build();
256+
257+
SuppressCatchingSender suppressCatchingSender = new SuppressCatchingSender(Encoding.JSON);
258+
ZipkinSpanExporter zipkinSpanExporter =
259+
new ZipkinSpanExporter(
260+
new ZipkinSpanExporterBuilder(),
261+
mockEncoder,
262+
suppressCatchingSender,
263+
MeterProvider::noop,
264+
mockTransformer);
265+
266+
byte[] someBytes = new byte[0];
267+
Span zipkinSpan =
268+
zipkinSpanBuilder(Span.Kind.SERVER, localIp)
269+
.putTag(OtelToZipkinSpanTransformer.OTEL_STATUS_CODE, "OK")
270+
.build();
271+
when(mockTransformer.generateSpan(testSpanData)).thenReturn(zipkinSpan);
272+
when(mockEncoder.encode(zipkinSpan)).thenReturn(someBytes);
273+
274+
zipkinSpanExporter.export(Collections.singleton(testSpanData));
275+
276+
assertTrue(suppressCatchingSender.sent.get());
277+
assertTrue(suppressCatchingSender.suppressed.get());
278+
}
279+
280+
static class SuppressCatchingSender extends BytesMessageSender.Base {
281+
282+
final AtomicBoolean sent = new AtomicBoolean();
283+
final AtomicBoolean suppressed = new AtomicBoolean();
284+
285+
protected SuppressCatchingSender(Encoding encoding) {
286+
super(encoding);
287+
}
288+
289+
@Override
290+
public int messageMaxBytes() {
291+
return 1024;
292+
}
293+
294+
@Override
295+
public void send(List<byte[]> list) throws IOException {
296+
sent.set(true);
297+
suppressed.set(InstrumentationUtil.shouldSuppressInstrumentation(Context.current()));
298+
}
299+
300+
@Override
301+
public void close() throws IOException {}
276302
}
277303
}

0 commit comments

Comments
 (0)