From d4fc7414ef269a6cf2149ba17ec8e2fbeb3c3713 Mon Sep 17 00:00:00 2001 From: Yann Moisan Date: Wed, 17 Jul 2024 21:55:13 +0200 Subject: [PATCH] add integration tests --- build.sbt | 5 +- project/Dependencies.scala | 7 +- .../graphite4s/GraphiteIntegrationTest.scala | 86 +++++++++++++++++++ 3 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 src/test/scala/graphite4s/GraphiteIntegrationTest.scala diff --git a/build.sbt b/build.sbt index 4bf1b69..806aaad 100644 --- a/build.sbt +++ b/build.sbt @@ -7,9 +7,10 @@ crossScalaVersions := Seq("2.12.17", "2.13.10") scalafmtOnCompile := true libraryDependencies ++= Seq( - catsEffect, + catsEffectKernel, scalaLogging, - scalaTest % Test + catsEffect % Test, + scalaTest % Test ) startYear := Some(2018) diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 2a546c6..95d5087 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -1,7 +1,8 @@ import sbt._ object Dependencies { - lazy val scalaTest = "org.scalatest" %% "scalatest" % "3.2.15" - lazy val catsEffect = "org.typelevel" %% "cats-effect-kernel" % "3.4.6" - lazy val scalaLogging = "com.typesafe.scala-logging" %% "scala-logging" % "3.9.5" + lazy val scalaTest = "org.scalatest" %% "scalatest" % "3.2.15" + lazy val catsEffectKernel = "org.typelevel" %% "cats-effect-kernel" % "3.4.6" + lazy val catsEffect = "org.typelevel" %% "cats-effect" % "3.4.6" + lazy val scalaLogging = "com.typesafe.scala-logging" %% "scala-logging" % "3.9.5" } diff --git a/src/test/scala/graphite4s/GraphiteIntegrationTest.scala b/src/test/scala/graphite4s/GraphiteIntegrationTest.scala new file mode 100644 index 0000000..b702f76 --- /dev/null +++ b/src/test/scala/graphite4s/GraphiteIntegrationTest.scala @@ -0,0 +1,86 @@ +package graphite4s + +import cats.effect.IO +import cats.effect.unsafe.implicits.global +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers + +import java.io._ +import java.net.{ServerSocket, Socket} +import java.time.Instant +import scala.collection.mutable.ArrayBuffer + +class GraphiteIntegrationTest extends AnyFlatSpec with Matchers { + private val p1 = GraphitePoint("root.1", 1, Instant.ofEpochSecond(11)) + + class MockTcpServer(port: Int) { + @volatile private var running = true + private var serverSocket: ServerSocket = _ + var handler: ClientHandler = _ + + def start(): Unit = { + serverSocket = new ServerSocket(port) + new Thread(() => + while (running) + try { + val clientSocket = serverSocket.accept() + handler = new ClientHandler(clientSocket) + handler.start() + } catch { + case e: IOException => + if (!running) { + // Server was stopped, so we break the loop + // break + } else { + e.printStackTrace() + } + } + ).start() + } + + def stop(): Unit = { + running = false + if (serverSocket != null && !serverSocket.isClosed) { + serverSocket.close() + } + } + + class ClientHandler(socket: Socket) extends Thread { + val messages = new ArrayBuffer[String]() + override def run(): Unit = + try { + val in = new BufferedReader(new InputStreamReader(socket.getInputStream)) + val out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream), true) + var message: String = null + + while ({ message = in.readLine(); message != null }) { + println(s"Received: $message") + messages += message + out.println(s"Echo: $message") + } + } catch { + case e: IOException => e.printStackTrace() + } finally + try + socket.close() + catch { + case e: IOException => e.printStackTrace() + } + } + } + + "BaseGraphite" should "send one message" in { + val server = new MockTcpServer(2003) + server.start() + + val graphite = + new BaseGraphite(new JavaTCPClient[IO]("localhost", 2003), new Prefixer("production")) + + graphite.send(p1).unsafeRunSync() + + server.stop() + + server.handler.messages should contain theSameElementsInOrderAs Seq("production.root.1 1.0 11") + } + +}