-
Notifications
You must be signed in to change notification settings - Fork 774
/
ActorPropagationTest.java
114 lines (100 loc) · 4.04 KB
/
ActorPropagationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.opentracingshim.testbed.actorpropagation;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.opentracingshim.OpenTracingShim;
import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.tag.Tags;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.Phaser;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
/**
* These tests are intended to simulate the kind of async models that are common in java async
* frameworks.
*
* <p>For improved readability, ignore the phaser lines as those are there to ensure deterministic
* execution for the tests without sleeps.
*/
@SuppressWarnings("FutureReturnValueIgnored")
class ActorPropagationTest {
@RegisterExtension
static final OpenTelemetryExtension otelTesting = OpenTelemetryExtension.create();
private final Tracer tracer = OpenTracingShim.createTracerShim(otelTesting.getOpenTelemetry());
private Phaser phaser;
@BeforeEach
void before() {
phaser = new Phaser();
}
@Test
void testActorTell() {
try (Actor actor = new Actor(tracer, phaser)) {
phaser.register();
Span parent =
tracer
.buildSpan("actorTell")
.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_PRODUCER)
.withTag(Tags.COMPONENT.getKey(), "example-actor")
.start();
try (Scope scope = tracer.activateSpan(parent)) {
actor.tell("my message 1");
actor.tell("my message 2");
} finally {
parent.finish();
}
phaser.arriveAndAwaitAdvance(); // child tracer started
assertThat(otelTesting.getSpans().size()).isEqualTo(1);
phaser.arriveAndAwaitAdvance(); // continue...
phaser.arriveAndAwaitAdvance(); // child tracer finished
assertThat(otelTesting.getSpans().size()).isEqualTo(3);
phaser.arriveAndDeregister(); // continue...
List<SpanData> finished = otelTesting.getSpans();
assertThat(finished.size()).isEqualTo(3);
assertThat(finished.get(0).getTraceId()).isEqualTo(finished.get(1).getTraceId());
assertThat(tracer.scopeManager().activeSpan()).isNull();
}
}
@Test
void testActorAsk() throws ExecutionException, InterruptedException {
try (Actor actor = new Actor(tracer, phaser)) {
phaser.register();
Future<String> future1;
Future<String> future2;
Span span =
tracer
.buildSpan("actorAsk")
.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_PRODUCER)
.withTag(Tags.COMPONENT.getKey(), "example-actor")
.start();
try (Scope scope = tracer.activateSpan(span)) {
future1 = actor.ask("my message 1");
future2 = actor.ask("my message 2");
} finally {
span.finish();
}
phaser.arriveAndAwaitAdvance(); // child tracer started
assertThat(otelTesting.getSpans().size()).isEqualTo(1);
phaser.arriveAndAwaitAdvance(); // continue...
phaser.arriveAndAwaitAdvance(); // child tracer finished
assertThat(otelTesting.getSpans().size()).isEqualTo(3);
phaser.arriveAndDeregister(); // continue...
List<SpanData> finished = otelTesting.getSpans();
String message1 = future1.get(); // This really should be a non-blocking callback...
String message2 = future2.get(); // This really should be a non-blocking callback...
assertThat(message1).isEqualTo("received my message 1");
assertThat(message2).isEqualTo("received my message 2");
assertThat(finished.size()).isEqualTo(3);
assertThat(finished.get(0).getTraceId()).isEqualTo(finished.get(1).getTraceId());
assertThat(tracer.scopeManager().activeSpan()).isNull();
}
}
}