Skip to content

Commit

Permalink
Implement first integration test for health routes
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Nov 24, 2023
1 parent 4327729 commit 2c3cf37
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 10 deletions.
12 changes: 6 additions & 6 deletions app/src/main/scala/App.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ object App extends IOApp.Simple:
for
config <- Resource.eval(AppConfig.load)
_ <- Resource.eval(KamonInitiator.apply.init(config.kamon))
_ <- FishnetApp(config).run()
_ <- Resource.eval(Logger[IO].info(s"Starting lila-fishnet with config: $config"))
res <- AppResources.instance(config.redis)
_ <- FishnetApp(res, config).run()
yield ()

class FishnetApp(config: AppConfig)(using Logger[IO]):
class FishnetApp(res: AppResources, config: AppConfig)(using Logger[IO]):
def run(): Resource[IO, Unit] =
for
res <- AppResources.instance(config.redis)
_ <- Resource.eval(Logger[IO].info(s"Starting lila-fishnet with config: $config"))
lilaClient = LilaClient(res.redisPubsub)
monitor = Monitor.apply
lilaClient <- Resource.pure(LilaClient(res.redisPubsub))
monitor = Monitor.apply
executor <- Resource.eval(Executor.instance(lilaClient, monitor))
workListenerJob = RedisSubscriberJob(executor, res.redisPubsub)
cleanJob = CleanJob(executor)
Expand Down
57 changes: 57 additions & 0 deletions app/src/test/scala/IntegrationTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package lila.fishnet

import cats.effect.IO
import cats.effect.kernel.Resource
import cats.syntax.all.*
import com.comcast.ip4s.{ Host, Port }
import com.dimafeng.testcontainers.GenericContainer
import org.http4s.ember.client.EmberClientBuilder
import org.testcontainers.containers.wait.strategy.Wait
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.noop.NoOpLogger
import org.http4s.circe.CirceEntityDecoder.*
import weaver.*
import lila.fishnet.http.HealthCheck.AppStatus

object IntegrationTest extends SimpleIOSuite:

given Logger[IO] = NoOpLogger[IO]
private def resource: Resource[IO, Unit] =
for
redis <- RedisContainer.startRedis
defaultConfig <- Resource.eval(AppConfig.load)
config = defaultConfig.copy(redis = redis)
_ <- Resource.eval(IO.println(s"Starting lila-fishnet with config: $config"))
res <- AppResources.instance(config.redis)
_ <- FishnetApp(res, config).run()
yield ()

test("health check should return healthy"):
(resource >> client)
.use(
_.expect[AppStatus]("http://localhost:9665/health")
.map(expect.same(_, AppStatus(true)))
)

private def client = EmberClientBuilder.default[IO].build

object RedisContainer:

private val REDIS_PORT = 6379
private val redisContainer =
val start = IO(
GenericContainer(
"redis:6-alpine",
exposedPorts = Seq(REDIS_PORT),
waitStrategy = Wait.forListeningPort()
)
)
.flatTap(cont => IO(cont.start()))
Resource.make(start)(cont => IO(cont.stop()))

def parseConfig(redis: GenericContainer): RedisConfig =
println(s"redis: ${redis.host} ${redis.containerIpAddress} ${redis.envMap}")
RedisConfig(Host.fromString(redis.host).get, Port.fromInt(redis.mappedPort(REDIS_PORT)).get)

def startRedis: Resource[IO, RedisConfig] =
redisContainer.map(parseConfig)
5 changes: 4 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ lazy val app = project
log4Cats,
logback,
redis,
circeLiteral,
chessTestKit,
munit,
munitScalacheck,
weaver,
weaverScalaCheck,
circeLiteral
testContainers,
log4CatsNoop,
http4sClient
)
)
.enablePlugins(JavaAppPackaging)
Expand Down
6 changes: 3 additions & 3 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ object Dependencies {

val chessTestKit = "org.lichess" %% "scalachess-test-kit" % V.chess % Test
val log4CatsNoop = "org.typelevel" %% "log4cats-noop" % "2.6.0" % Test
val testContainers = "com.dimafeng" %% "testcontainers-scala-redis" % "0.41.0" % Test
val weaver = "com.disneystreaming" %% "weaver-cats" % "0.8.3" % Test
val weaverScalaCheck = "com.disneystreaming" %% "weaver-scalacheck" % "0.8.3" % Test
val munit = "org.scalameta" %% "munit" % V.munit % Test
val munitScalacheck = "org.scalameta" %% "munit-scalacheck" % V.munit % Test
val testContainers = "com.dimafeng" %% "testcontainers-scala-core" % "0.41.0" % Test
val weaver = "com.disneystreaming" %% "weaver-cats" % "0.8.3" % Test
val weaverScalaCheck = "com.disneystreaming" %% "weaver-scalacheck" % "0.8.3" % Test

}

0 comments on commit 2c3cf37

Please sign in to comment.