This repository has been archived by the owner on Dec 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleStreamTest.scala
57 lines (52 loc) · 1.93 KB
/
SimpleStreamTest.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
package io.moia.kamon
import akka.stream.scaladsl.{Keep, Sink, Source}
import org.scalacheck.Gen
class SimpleStreamTest extends UnitTest with AkkaTest {
"Simple Stream Flow" should {
"maintain the context during a simple map" in {
forAll(nonEmptyStringGen, Gen.listOf(Gen.posNum[Int])) { (traceId, nums) =>
inContext(traceId) {
currentTraceId() shouldBe Some(traceId)
val result = Source(nums)
.map(_ => currentTraceId() )
.toMat(Sink.seq)(Keep.right)
.run()
.futureValue
result.flatten should have size nums.size
currentTraceId() shouldBe Some(traceId)
}
}
}
"maintain the context if a nested context is created" in {
forAll(nonEmptyStringGen, nonEmptyStringGen, Gen.listOf(Gen.posNum[Int])) { (traceId, nested, nums) =>
inContext(traceId) {
currentTraceId() shouldBe Some(traceId)
val result = Source(nums)
.map(_ => inContext(nested)(currentTraceId()) -> currentTraceId())
.toMat(Sink.seq)(Keep.right)
.run()
.futureValue
result.flatMap(_._1) should have size nums.size
result.flatMap(_._2) should have size nums.size
currentTraceId() shouldBe Some(traceId)
}
}
}
"maintain traceIds created during the flow" in {
inContext("outer") {
Source(0.until(100))
.map( n => n -> setTraceId(n.toString) )
.map { case (n, scope) =>
assert( currentTraceId().contains(n.toString), s"In map async for n = $n: ${currentTraceId()} != $n" )
(n + 1) -> scope
}
.runFold(0) { case (acc, (m, scope)) =>
val n = m - 1
assert( currentTraceId().contains(n.toString), s"In run fold for n = $n: ${currentTraceId()} != $n" )
scope.close()
acc + m
}
}.futureValue shouldBe 5050
}
}
}