Skip to content

Commit

Permalink
change interface of healthcheck route constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
everpeace committed Oct 25, 2017
1 parent 924bd5a commit 64bb722
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ package com.github.everpeace.healthchecks.route

import akka.http.scaladsl.model.StatusCodes._
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.PathMatchers
import akka.http.scaladsl.server.{PathMatchers, Route}
import akka.http.scaladsl.server.directives.PathDirectives
import cats.data.Validated.{Invalid, Valid}
import com.github.everpeace.healthchecks.{HealthCheck, HealthCheckResult}
import de.heikoseeberger.akkahttpcirce.CirceSupport._
Expand Down Expand Up @@ -63,19 +64,25 @@ object HealthCheckRoutes extends DecorateAsScala {
)

def health(
checks: List[HealthCheck],
pathString: String = "health"
checks: HealthCheck*
)(implicit
ec: ExecutionContext
) = {
require(checks.nonEmpty, "checks must not empty.")
): Route = health("health", checks.toList)

def health(
path: String,
checks: List[HealthCheck]
)(implicit
ec: ExecutionContext
): Route = {
require(checks.toList.nonEmpty, "checks must not empty.")
require(
checks.toList.map(_.name).toSet.size == checks.toList.length,
s"HealthCheck name should be unique (given HealthCheck names = [${checks.toList.map(_.name).mkString(",")}])."
)
val rootSlashRemoved =
if (pathString.startsWith("/")) pathString.substring(1) else pathString
path(PathMatchers.separateOnSlashes(rootSlashRemoved)) {
if (path.startsWith("/")) path.substring(1) else path
PathDirectives.path(PathMatchers.separateOnSlashes(rootSlashRemoved)) {
get {
complete {
Future
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,23 @@ class HealthRoutesTest
describe("HealthCheck route") {
it("should raise exception when no healthcheck is given.") {
val exception = the[IllegalArgumentException] thrownBy HealthCheckRoutes
.health(List())
.health()
exception.getMessage shouldEqual "requirement failed: checks must not empty."
}

it("should raise exception when given healthchecks have same names") {
val exception = the[IllegalArgumentException] thrownBy HealthCheckRoutes
.health(List(healthCheck("test")(healthy), healthCheck("test")(healthy)))
val exception = the[IllegalArgumentException] thrownBy HealthCheckRoutes.health(
healthCheck("test")(healthy),
healthCheck("test")(healthy)
)
exception.getMessage shouldEqual "requirement failed: HealthCheck name should be unique (given HealthCheck names = [test,test])."
}

it("should return correct healthy response when all healthchecks are healthy.") {
val ok1 = healthCheck("test1")(healthy)
val ok2 = healthCheck("test2")(healthy)

Get("/health") ~> HealthCheckRoutes.health(List(ok1, ok2)) ~> check {
Get("/health") ~> HealthCheckRoutes.health(ok1, ok2) ~> check {
status shouldEqual OK
responseAs[String] shouldEqual
"""
Expand All @@ -78,7 +80,7 @@ class HealthRoutesTest
val failedButNonFatal =
healthCheck("test2", Severity.NonFatal)(unhealthy("error"))

Get("/health") ~> HealthCheckRoutes.health(List(ok1, failedButNonFatal)) ~> check {
Get("/health") ~> HealthCheckRoutes.health(ok1, failedButNonFatal) ~> check {
status shouldEqual OK
responseAs[String] shouldEqual
"""
Expand All @@ -100,7 +102,7 @@ class HealthRoutesTest
healthCheck("test2", Severity.NonFatal)(unhealthy("error"))
val failedFatal = healthCheck("test3")(throw new Exception("exception"))

Get("/health") ~> HealthCheckRoutes.health(List(ok, failedButNonFatal, failedFatal)) ~> check {
Get("/health") ~> HealthCheckRoutes.health(ok, failedButNonFatal, failedFatal) ~> check {
status shouldEqual InternalServerError
responseAs[String] shouldEqual
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ sealed abstract class K8sProbe protected (
val ec: ExecutionContext) {
require(checks.nonEmpty, "checks must not be empty.")

def toRoute = HealthCheckRoutes.health(checks, path)(ec)
def toRoute = HealthCheckRoutes.health(path, checks)(ec)
}

case class LivenessProbe protected (
Expand Down

0 comments on commit 64bb722

Please sign in to comment.