Skip to content

Commit

Permalink
Merge pull request #221 from YannMoisan/add-integration-tests
Browse files Browse the repository at this point in the history
add integration tests
  • Loading branch information
YannMoisan authored Jul 17, 2024
2 parents aafaaf2 + d4fc741 commit 37a6597
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 5 deletions.
5 changes: 3 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
@@ -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"
}
86 changes: 86 additions & 0 deletions src/test/scala/graphite4s/GraphiteIntegrationTest.scala
Original file line number Diff line number Diff line change
@@ -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")
}

}

0 comments on commit 37a6597

Please sign in to comment.