aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/spraytest/FutureTesting.scala
blob: b864d6d66b9979db2ac34dbf503c41879f56364b (plain) (blame)
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
package spraytest
/*
import akka.actor.ActorSystem
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Try, Success}
import kamon.actor.TransactionContext

object FutureTesting extends App {

  val actorSystem = ActorSystem("future-testing")
  implicit val ec = actorSystem.dispatcher
  implicit val tctx = TransactionContext(11, Nil)

  threadPrintln("In the initial Thread")


  val f = TraceableFuture {
    threadPrintln(s"Processing the Future, and the current context is: ${TransactionContext.current.get()}")
  }

  f.onComplete({
    case Success(a) => threadPrintln(s"Processing the first callback, and the current context is: ${TransactionContext.current.get()}")
  })

  f.onComplete({
    case Success(a) => threadPrintln(s"Processing the second callback, and the current context is: ${TransactionContext.current.get()}")
  })








  def threadPrintln(message: String) = println(s"Thread[${Thread.currentThread.getName}] says: [${message}]")

}




trait TransactionContextWrapper {
  def wrap[In, Out](f: => In => Out, tranContext: TransactionContext) = {
    TransactionContext.current.set(tranContext.fork)
    println(s"SetContext to: ${tranContext}")
    val result = f

    TransactionContext.current.remove()
    result
  }

}

class TraceableFuture[T](val future: Future[T]) extends TransactionContextWrapper {
  def onComplete[U](func: Try[T] => U)(implicit transactionContext: TransactionContext, executor: ExecutionContext): Unit = {
    future.onComplete(wrap(func, transactionContext))
  }
}

object TraceableFuture {

  implicit def toRegularFuture[T](tf: TraceableFuture[T]) = tf.future

  def apply[T](body: => T)(implicit transactionContext: TransactionContext, executor: ExecutionContext) = {
    val wrappedBody = contextSwitchWrapper(body, TransactionContext(transactionContext.dispatcherName, Nil))

    new TraceableFuture(Future { wrappedBody })
  }




  def contextSwitchWrapper[T](body: => T, transactionContext: TransactionContext) = {
    TransactionContext.current.set(transactionContext)
    val result = body
    TransactionContext.current.remove()
    result
  }
}*/