Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for spanR #681

Merged
merged 4 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"))
)
}
)
}