diff options
author | Ivan Topolnjak <ivantopo@gmail.com> | 2017-07-19 10:17:23 +0200 |
---|---|---|
committer | Ivan Topolnjak <ivantopo@gmail.com> | 2017-07-19 10:17:23 +0200 |
commit | 3076d7b7a499d1d7d3d2bc447d989e383dbb1b40 (patch) | |
tree | 4f5baddcd15058f126107a75ec86abe87dff72f6 /kamon-core/src/test/scala/kamon | |
parent | 7cf98a6043cf90a17b5d0a51cf2399e35239cc0c (diff) | |
download | Kamon-3076d7b7a499d1d7d3d2bc447d989e383dbb1b40.tar.gz Kamon-3076d7b7a499d1d7d3d2bc447d989e383dbb1b40.tar.bz2 Kamon-3076d7b7a499d1d7d3d2bc447d989e383dbb1b40.zip |
add tests for active span management
Diffstat (limited to 'kamon-core/src/test/scala/kamon')
-rw-r--r-- | kamon-core/src/test/scala/kamon/testkit/SpanInspector.scala | 8 | ||||
-rw-r--r-- | kamon-core/src/test/scala/kamon/trace/ActiveSpanManagementSpec.scala | 71 |
2 files changed, 75 insertions, 4 deletions
diff --git a/kamon-core/src/test/scala/kamon/testkit/SpanInspector.scala b/kamon-core/src/test/scala/kamon/testkit/SpanInspector.scala index 3ef1012b..e00c8b26 100644 --- a/kamon-core/src/test/scala/kamon/testkit/SpanInspector.scala +++ b/kamon-core/src/test/scala/kamon/testkit/SpanInspector.scala @@ -8,7 +8,7 @@ import scala.reflect.ClassTag import scala.util.Try class SpanInspector(span: Span) { - private val (realSpan, spanData) = { + private val (realSpan, spanData) = Try { val realSpan = span match { case _: Span.Real => span case a: ActiveSpan => @@ -17,10 +17,10 @@ class SpanInspector(span: Span) { val spanData = invoke[Span.Real, FinishedSpan](realSpan, "toFinishedSpan", classOf[Long] -> Long.box(Clock.microTimestamp())) (realSpan, spanData) - } + }.getOrElse((null, null)) - def nonEmpty: Boolean = - !span.isInstanceOf[Span.Empty] + def isEmpty: Boolean = + realSpan == null def spanTag(key: String): Option[Span.TagValue] = spanData.tags.get(key) diff --git a/kamon-core/src/test/scala/kamon/trace/ActiveSpanManagementSpec.scala b/kamon-core/src/test/scala/kamon/trace/ActiveSpanManagementSpec.scala new file mode 100644 index 00000000..ebee9f66 --- /dev/null +++ b/kamon-core/src/test/scala/kamon/trace/ActiveSpanManagementSpec.scala @@ -0,0 +1,71 @@ +package kamon.trace + +import kamon.Kamon +import kamon.testkit.SpanInspector +import kamon.trace.Span.Annotation +import kamon.util.Clock +import org.scalatest.{Matchers, WordSpec} + +class ActiveSpanManagementSpec extends WordSpec with Matchers { + + "Kamon acting as a ActiveSpanSource" should { + "return a ActiveSpan wrapping a empty span when there is no currently active Span" in { + inspect(Kamon.activeSpan()) shouldBe empty + } + + "safely operate on a ActiveSpan that wraps a empty Span" in { + val activeSpan = Kamon.activeSpan() + val activeSpanData = inspect(Kamon.activeSpan()) + activeSpanData shouldBe empty + + activeSpan + .setOperationName("test") + .addBaggage("key", "value") + .addMetricTag("key", "value") + .addSpanTag("string", "string") + .addSpanTag("number", 42) + .addSpanTag("boolean-true", true) + .addSpanTag("boolean-false", false) + .annotate(Annotation(Clock.microTimestamp(), "event", Map("k" -> "v"))) + + val baggage = activeSpan.context().baggage + baggage.add("key", "value") + baggage.get("key") shouldBe empty + baggage.getAll() shouldBe empty + + val continuation = activeSpan.capture() + val activatedSpan = continuation.activate() + inspect(Kamon.activeSpan()) shouldBe empty + activatedSpan.deactivate() + + inspect(Kamon.activeSpan()) shouldBe empty + } + + "set a Span as active when using makeActive" in { + val span = Kamon.buildSpan("mySpan").start() + val activeSpan = Kamon.makeActive(span) + Kamon.activeSpan() shouldBe theSameInstanceAs(activeSpan) + activeSpan.deactivate() + } + + "set a Span as active when using startActive" in { + val activeSpan = Kamon.buildSpan("mySpan").startActive() + Kamon.activeSpan() shouldBe theSameInstanceAs(activeSpan) + activeSpan.deactivate() + } + + "restore the previously active Span when a ActiveSpan gets deactivated" in { + val previouslyActiveSpan = Kamon.activeSpan() + inspect(Kamon.activeSpan()) shouldBe empty + + val activeSpan = Kamon.buildSpan("mySpan").startActive() + Kamon.activeSpan() shouldBe theSameInstanceAs(activeSpan) + activeSpan.deactivate() + + Kamon.activeSpan() shouldBe theSameInstanceAs(previouslyActiveSpan) + } + } + + def inspect(span: Span): SpanInspector = + SpanInspector(span) +} |