diff options
Diffstat (limited to 'src/test/scala/kamon/instrumentation/RunnableInstrumentationSpec.scala')
-rw-r--r-- | src/test/scala/kamon/instrumentation/RunnableInstrumentationSpec.scala | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/test/scala/kamon/instrumentation/RunnableInstrumentationSpec.scala b/src/test/scala/kamon/instrumentation/RunnableInstrumentationSpec.scala new file mode 100644 index 00000000..f2e83824 --- /dev/null +++ b/src/test/scala/kamon/instrumentation/RunnableInstrumentationSpec.scala @@ -0,0 +1,84 @@ +package kamon.instrumentation + +import scala.concurrent.{Await, Promise, Future} +import scala.concurrent.ExecutionContext.Implicits.global +import org.scalatest.{OptionValues, WordSpec} +import org.scalatest.matchers.MustMatchers +import org.scalatest.concurrent.PatienceConfiguration +import kamon.{Kamon, TraceContext} +import java.util.UUID +import scala.util.Success +import scala.concurrent.duration._ +import java.util.concurrent.TimeUnit +import akka.actor.ActorSystem + + +class RunnableInstrumentationSpec extends WordSpec with MustMatchers with ScalaFutures with PatienceConfiguration with OptionValues { + + "a instrumented runnable" when { + "created in a thread that does have a TraceContext" must { + "preserve the TraceContext" which { + "should be available during the run method execution" in { new FutureWithContext { + + whenReady(futureWithContext) { result => + result.value must be === testContext + } + }} + + "should be available during the execution of onComplete callbacks" in { new FutureWithContext { + val onCompleteContext = Promise[TraceContext]() + + futureWithContext.onComplete({ + case _ => onCompleteContext.complete(Success(Kamon.context.get)) + }) + + whenReady(onCompleteContext.future) { result => + result must be === testContext + } + }} + } + } + + "created in a thread that doest have a TraceContext" must { + "not capture any TraceContext for the body execution" in { new FutureWithoutContext{ + + whenReady(futureWithoutContext) { result => + result must be === None + } + }} + + "not make any TraceContext available during the onComplete callback" in { new FutureWithoutContext { + val onCompleteContext = Promise[Option[TraceContext]]() + + futureWithoutContext.onComplete({ + case _ => onCompleteContext.complete(Success(Kamon.context)) + }) + + whenReady(onCompleteContext.future) { result => + result must be === None + } + }} + } + } + + + /** + * We are using Futures for the test since they exercise Runnables in the back and also resemble the real use case we have. + */ + + + trait FutureWithContext { + implicit val as = ActorSystem("test-actorsystem") + val testContext = TraceContext() + Kamon.set(testContext) + + val futureWithContext = Future { Kamon.context} + } + + trait FutureWithoutContext { + Kamon.clear // Make sure no TraceContext is available + val futureWithoutContext = Future { Kamon.context } + } +} + + |