From bf86900669d649308f4914c54e6fe076510506a6 Mon Sep 17 00:00:00 2001 From: Ivan Topolnak Date: Thu, 7 Nov 2013 18:41:33 -0300 Subject: halfway to our own NewRelic Agent --- .../src/test/scala/kamon/newrelic/AgentSpec.scala | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 kamon-newrelic/src/test/scala/kamon/newrelic/AgentSpec.scala (limited to 'kamon-newrelic/src/test') 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 + }) + } +} -- cgit v1.2.3