Skip to content

Commit

Permalink
Merge pull request #681 from armanbilge/topic/test-spanR
Browse files Browse the repository at this point in the history
Add test for `spanR`
  • Loading branch information
mpilquist authored Dec 14, 2022
2 parents d1c1215 + 0192f43 commit ae8d688
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
8 changes: 4 additions & 4 deletions modules/core/shared/src/test/scala/InMemory.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package natchez
import java.net.URI

import cats.data.{Chain, Kleisli}
import cats.effect.{IO, Ref, Resource}
import cats.effect.{IO, MonadCancelThrow, Ref, Resource}

import natchez.Span.Options
import munit.CatsEffectSuite
Expand Down Expand Up @@ -118,15 +118,15 @@ trait InMemorySuite extends CatsEffectSuite {
val NatchezCommand = InMemory.NatchezCommand

trait TraceTest {
def program[F[_]: Trace]: F[Unit]
def program[F[_]: MonadCancelThrow: Trace]: F[Unit]
def expectedHistory: List[(Lineage, NatchezCommand)]
}

def traceTest(name: String, tt: TraceTest) = {
test(s"$name - Kleisli")(
testTraceKleisli(tt.program[Kleisli[IO, Span[IO], *]](_), tt.expectedHistory)
testTraceKleisli(tt.program[Kleisli[IO, Span[IO], *]](implicitly, _), tt.expectedHistory)
)
test(s"$name - IOLocal")(testTraceIoLocal(tt.program[IO](_), tt.expectedHistory))
test(s"$name - IOLocal")(testTraceIoLocal(tt.program[IO](implicitly, _), tt.expectedHistory))
}

def testTraceKleisli(
Expand Down
6 changes: 4 additions & 2 deletions modules/core/shared/src/test/scala/SpanCoalesceTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

package natchez

import cats.effect.MonadCancelThrow

class SpanCoalesceTest extends InMemorySuite {

traceTest(
"suppress - nominal",
new TraceTest {
def program[F[_]: Trace] = {
def program[F[_]: MonadCancelThrow: Trace] = {
def detailed =
Trace[F].span("parent")(Trace[F].span("child")(Trace[F].put("answer" -> 42)))
Trace[F].span("suppressed", Span.Options.Suppress)(detailed)
Expand All @@ -27,7 +29,7 @@ class SpanCoalesceTest extends InMemorySuite {
traceTest(
"coaslesce - nominal",
new TraceTest {
def program[F[_]: Trace] = {
def program[F[_]: MonadCancelThrow: Trace] = {
def detailed =
Trace[F].span("parent")(Trace[F].span("child")(Trace[F].put("answer" -> 42)))
Trace[F].span("coalesced", Span.Options.Coalesce)(detailed)
Expand Down
28 changes: 27 additions & 1 deletion modules/core/shared/src/test/scala/SpanPropagationTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

package natchez

import cats.effect.MonadCancelThrow
import cats.syntax.all._

class SpanPropagationTest extends InMemorySuite {

traceTest(
"propagation",
new TraceTest {
def program[F[_]: Trace] =
def program[F[_]: MonadCancelThrow: Trace] =
Trace[F].span("parent")(Trace[F].span("child")(Trace[F].put("answer" -> 42)))

def expectedHistory = List(
Expand All @@ -23,4 +26,27 @@ class SpanPropagationTest extends InMemorySuite {
)
}
)

traceTest(
"spanR",
new TraceTest {
def program[F[_]: MonadCancelThrow: Trace] =
Trace[F].spanR("spanR").use { f =>
Trace[F].span("span")(
f(Trace[F].put("question" -> "ultimate")) *> Trace[F].put("answer" -> 42)
)
}

def expectedHistory = List(
(Lineage.Root, NatchezCommand.CreateRootSpan("root", Kernel(Map()))),
(Lineage.Root, NatchezCommand.CreateSpan("spanR", None)),
(Lineage.Root, NatchezCommand.CreateSpan("span", None)),
(Lineage.Root / "spanR", NatchezCommand.Put(List("question" -> "ultimate"))),
(Lineage.Root / "span", NatchezCommand.Put(List("answer" -> 42))),
(Lineage.Root, NatchezCommand.ReleaseSpan("span")),
(Lineage.Root, NatchezCommand.ReleaseSpan("spanR")),
(Lineage.Root, NatchezCommand.ReleaseRootSpan("root"))
)
}
)
}

0 comments on commit ae8d688

Please sign in to comment.