diff options
author | Ivan Topolnak <ivantopo@gmail.com> | 2013-11-07 18:41:33 -0300 |
---|---|---|
committer | Ivan Topolnak <ivantopo@gmail.com> | 2013-11-07 18:41:33 -0300 |
commit | bf86900669d649308f4914c54e6fe076510506a6 (patch) | |
tree | d8bf9af9f5c8a946d757137a303f6956c05edb03 /kamon-newrelic/src/test/scala | |
parent | 2b63540e5fffab545d0846cfb3dab5c0e1d0c9e1 (diff) | |
download | Kamon-bf86900669d649308f4914c54e6fe076510506a6.tar.gz Kamon-bf86900669d649308f4914c54e6fe076510506a6.tar.bz2 Kamon-bf86900669d649308f4914c54e6fe076510506a6.zip |
halfway to our own NewRelic Agent
Diffstat (limited to 'kamon-newrelic/src/test/scala')
-rw-r--r-- | kamon-newrelic/src/test/scala/kamon/newrelic/AgentSpec.scala | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/kamon-newrelic/src/test/scala/kamon/newrelic/AgentSpec.scala b/kamon-newrelic/src/test/scala/kamon/newrelic/AgentSpec.scala new file mode 100644 index 00000000..8868b8c0 --- /dev/null +++ b/kamon-newrelic/src/test/scala/kamon/newrelic/AgentSpec.scala @@ -0,0 +1,70 @@ +package kamon.newrelic + +import akka.testkit.{TestActor, TestProbe, TestKit} +import akka.actor.{Props, ActorRef, ActorSystem} +import org.scalatest.WordSpecLike +import kamon.AkkaExtensionSwap +import spray.can.Http +import akka.io.IO +import akka.testkit.TestActor.{KeepRunning, AutoPilot} +import spray.http._ +import spray.http.HttpRequest +import spray.http.HttpResponse + +class AgentSpec extends TestKit(ActorSystem("agent-spec")) with WordSpecLike { + + setupFakeHttpManager + + "the Newrelic Agent" should { + "try to connect upon creation" in { + val agent = system.actorOf(Props[Agent]) + + Thread.sleep(5000) + } + } + + def setupFakeHttpManager: Unit = { + val fakeHttpManager = TestProbe() + fakeHttpManager.setAutoPilot(new TestActor.AutoPilot { + def run(sender: ActorRef, msg: Any): AutoPilot = { + msg match { + case HttpRequest(_, uri, _, _, _) if rawMethodIs("get_redirect_host", uri) => + sender ! jsonResponse( + """ + | { + | "return_value": "collector-8.newrelic.com" + | } + | """.stripMargin) + + println("Selecting Collector") + + case HttpRequest(_, uri, _, _, _) if rawMethodIs("connect", uri) => + sender ! jsonResponse( + """ + | { + | "return_value": { + | "agent_run_id": 161221111 + | } + | } + | """.stripMargin) + println("Connecting") + } + + KeepRunning + } + + def jsonResponse(json: String): HttpResponse = { + HttpResponse(entity = HttpEntity(ContentTypes.`application/json`, json)) + } + + def rawMethodIs(method: String, uri: Uri): Boolean = { + uri.query.get("method").filter(_ == method).isDefined + } + }) + + + AkkaExtensionSwap.swap(system, Http, new IO.Extension { + def manager: ActorRef = fakeHttpManager.ref + }) + } +} |