Skip to content

Commit

Permalink
Add FeatureClient error hook test
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcardell committed Sep 13, 2024
1 parent 9d0cc02 commit a01966c
Showing 1 changed file with 106 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import munit.CatsEffectSuite

import io.cardell.openfeature.provider.ProviderImpl
import io.cardell.openfeature.provider.StubEvaluationProvider
import io.cardell.openfeature.provider.EvaluationProvider
import io.cardell.openfeature.provider.ProviderMetadata
import io.cardell.openfeature.provider.ResolutionDetails
import cats.effect.kernel.Ref

class FeatureClientImplTest extends CatsEffectSuite {
val evalProvider = new StubEvaluationProvider[IO]()
Expand Down Expand Up @@ -49,4 +53,106 @@ class FeatureClientImplTest extends CatsEffectSuite {
assertEquals(expected, result)
}

test("before hooks run on boolean evaluation") {
val ref = Ref.unsafe[IO, Int](0)

val h1 = BeforeHook[IO] { case _ => ref.update(_ + 1).as(None) }
val h2 = BeforeHook[IO] { case _ => ref.update(_ + 2).as(None) }

val client = FeatureClientImpl[IO](provider).withHook(h1).withHook(h2)

val expected = 3

for {
_ <- client.getBooleanValue("test-flag", false)
result <- ref.get
} yield assertEquals(result, expected)
}

test("error hooks run on before hook error") {
val ref = Ref.unsafe[IO, Int](0)

val beforeHook = BeforeHook[IO] { case _ =>
IO.raiseError(new Throwable("before hook error"))
}

val errorHook = ErrorHook[IO] { case _ => ref.update(_ + 2) }

val client = FeatureClientImpl[IO](provider)
.withHook(beforeHook)
.withHook(errorHook)

val expectedCount = 2
val expectedFlag = false

for {
result <- client.getBooleanValue("test-flag", expectedFlag)
refCount <- ref.get
} yield {
assertEquals(refCount, expectedCount)
assertEquals(result, expectedFlag)
}
}

test("error hooks run on evaluation error") {
val ref = Ref.unsafe[IO, Int](0)

val errorHook = ErrorHook[IO] { case _ => ref.update(_ + 2) }

val client = FeatureClientImpl[IO](ThrowingEvaluationProvider)
.withHook(errorHook)

val expectedCount = 2
val expectedFlag = false

for {
result <- client.getBooleanValue("test-flag", expectedFlag)
refCount <- ref.get
} yield {
assertEquals(refCount, expectedCount)
assertEquals(result, expectedFlag)
}
}

}

object ThrowingEvaluationProvider extends EvaluationProvider[IO] {

override def metadata: ProviderMetadata = ProviderMetadata(
name = "throwing-provider"
)

val error = new Throwable("ThrowingEvaluationProvider error")
def raise[A] = IO.raiseError[A](error)

override def resolveBooleanValue(
flagKey: String,
defaultValue: Boolean,
context: EvaluationContext
): IO[ResolutionDetails[Boolean]] = raise

override def resolveStringValue(
flagKey: String,
defaultValue: String,
context: EvaluationContext
): IO[ResolutionDetails[String]] = raise

override def resolveIntValue(
flagKey: String,
defaultValue: Int,
context: EvaluationContext
): IO[ResolutionDetails[Int]] = raise

override def resolveDoubleValue(
flagKey: String,
defaultValue: Double,
context: EvaluationContext
): IO[ResolutionDetails[Double]] = raise

override def resolveStructureValue[A: StructureDecoder](
flagKey: String,
defaultValue: A,
context: EvaluationContext
): IO[ResolutionDetails[A]] = raise

}

0 comments on commit a01966c

Please sign in to comment.