-
Notifications
You must be signed in to change notification settings - Fork 7
/
TracedExecutionContextTest.scala
177 lines (143 loc) · 5.38 KB
/
TracedExecutionContextTest.scala
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Copyright 2018-2019 The OpenTracing Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package io.opentracing.contrib.concurrent
import java.util
import java.util.concurrent.{Callable, TimeUnit}
import io.opentracing.Span
import io.opentracing.mock.MockTracer.Propagator
import io.opentracing.mock.{MockSpan, MockTracer}
import io.opentracing.util.ThreadLocalScopeManager
import org.awaitility.Awaitility.await
import org.hamcrest.core.IsEqual.equalTo
import org.scalatest.{BeforeAndAfter, FunSuite}
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, ExecutionContext, Future}
import scala.util.{Failure, Success}
class TracedExecutionContextTest extends FunSuite with BeforeAndAfter {
val mockTracer = new MockTracer(new ThreadLocalScopeManager, Propagator.TEXT_MAP)
before {
mockTracer.reset()
}
test("testIllegalContext") {
intercept[IllegalArgumentException] {
new TracedExecutionContext(null, mockTracer, false)
}
}
test("testIllegalTracer") {
intercept[IllegalArgumentException] {
new TracedExecutionContext(ExecutionContext.global, null, false)
}
}
test("testPropagation") {
val ec = new TracedExecutionContext(ExecutionContext.global, mockTracer)
var f: Future[Span] = null
var span: Span = null
val scope = mockTracer.buildSpan("one").startActive(false)
try {
span = scope.span
f = Future[Span] {
assert(mockTracer.scopeManager.active != null)
mockTracer.scopeManager.active.span
}(ec)
} finally {
scope.close()
}
val result = Await.result(f, Duration(15, TimeUnit.SECONDS))
assert(span == result)
assert(0 == mockTracer.finishedSpans.size)
span.finish()
assert(1 == mockTracer.finishedSpans.size)
assert(span == mockTracer.finishedSpans.get(0))
}
test("testCreateSpans") {
val ec = new TracedExecutionContext(ExecutionContext.global, mockTracer, true)
var parentSpan: Span = null
val scope = mockTracer.buildSpan("parent").startActive(false)
var f: Future[Span] = null
try {
parentSpan = scope.span
f = Future[Span] {
assert(mockTracer.scopeManager.active != null)
mockTracer.scopeManager.active.span
}(ec)
} finally {
scope.close()
}
val span: Span = Await.result(f, Duration(15, TimeUnit.SECONDS))
await.atMost(15, TimeUnit.SECONDS).until(reportedSpansSize(mockTracer), equalTo(1))
assert(1 == mockTracer.finishedSpans.size)
parentSpan.finish()
val finishedSpans: util.List[MockSpan] = mockTracer.finishedSpans
assert(2 == finishedSpans.size)
assert(parentSpan == finishedSpans.get(1))
assert(span == finishedSpans.get(0))
assert(finishedSpans.get(1).context.spanId == finishedSpans.get(0).parentId)
}
test("testNoActiveSpan") {
val ec = new TracedExecutionContext(ExecutionContext.global, mockTracer)
val f: Future[Boolean] = Future[Boolean] {
assert(mockTracer.scopeManager.active == null)
mockTracer.scopeManager.active != null
}(ec)
val isActive = Await.result(f, Duration(15, TimeUnit.SECONDS))
assert(isActive === false)
assert(0 == mockTracer.finishedSpans.size)
}
test("testGlobalTracer") {
val ec = new TracedExecutionContext(ExecutionContext.global)
val f: Future[Boolean] = Future[Boolean] {
assert(mockTracer.scopeManager.active == null)
mockTracer.scopeManager.active != null
}(ec)
val isActive = Await.result(f, Duration(15, TimeUnit.SECONDS))
assert(isActive === false)
assert(0 == mockTracer.finishedSpans.size)
}
test("testConvert") {
val ec = new TracedExecutionContext(ExecutionContext.global, mockTracer)
val scope = mockTracer.buildSpan("one").startActive(false)
try {
Future[Boolean] {
assert(mockTracer.scopeManager.active != null)
mockTracer.scopeManager.active.span.setTag("main", true)
true
}(ec).andThen {
case Success(_) =>
assert(mockTracer.scopeManager.active != null)
mockTracer.scopeManager.active.span.setTag("interceptor", true)
case Failure(_: Throwable) => fail()
}(ec).onComplete { _ => {
assert(mockTracer.scopeManager.active != null)
mockTracer.scopeManager.active.span.setTag("done", true)
mockTracer.scopeManager.active.span.finish()
}
}(ec)
} finally {
scope.close()
}
await.atMost(15, TimeUnit.SECONDS).until(reportedSpansSize(mockTracer), equalTo(1))
val finishedSpans = mockTracer.finishedSpans
assert(1 == finishedSpans.size)
val tags = finishedSpans.get(0).tags
assert(3 == tags.size)
assert(tags.containsKey("main"))
assert(tags.containsKey("interceptor"))
assert(tags.containsKey("done"))
}
private def reportedSpansSize(mockTracer: MockTracer): Callable[Int] = {
new Callable[Int] {
override def call(): Int = mockTracer.finishedSpans().size()
}
}
}