Skip to content

Commit

Permalink
Fiber resource
Browse files Browse the repository at this point in the history
  • Loading branch information
bcarter97 committed Jun 29, 2023
1 parent 67de72a commit 55e77c4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/test/scala/integration/TopicLoaderIntSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -189,25 +189,25 @@ class TopicLoaderIntSpec extends KafkaSpecBase[IO] {

val (preLoad, postLoad) = records(1 to 15).splitAt(10)

val loadRef: IO[Ref[IO, Boolean]] = Ref.of(false)
val topicRef: IO[Ref[IO, Seq[(String, String)]]] = Ref.empty

for {
loadState <- loadRef
topicState <- topicRef
loadState <- Ref.of[IO, Boolean](false)
topicState <- Ref.empty[IO, Seq[(String, String)]]
_ <- createCustomTopics(NonEmptyList.one(testTopic1))
_ <- publishStringMessages(testTopic1, preLoad)
fiber <- loadAndRunLoader(NonEmptyList.one(testTopic1))(_ => loadState.set(true))
.map(recordToTuple)
.evalTap(r => topicState.getAndUpdate(_ :+ r))
.compile
.drain
.start
_ <- eventually(topicState.get.asserting(_ should contain theSameElementsAs preLoad))
_ <- loadState.get.asserting(_ shouldBe true)
_ <- publishStringMessages(testTopic1, postLoad)
assertion <- eventually(topicState.get.asserting(_ should contain theSameElementsAs (preLoad ++ postLoad)))
_ <- fiber.cancel
assertion <- loadAndRunR(NonEmptyList.one(testTopic1))(
_ => loadState.set(true),
r => topicState.getAndUpdate(_ :+ r).void
).use { _ =>
for {
_ <- eventually(topicState.get.asserting(_ should contain theSameElementsAs preLoad))
_ <- loadState.get.asserting(_ shouldBe true)
_ <- publishStringMessages(testTopic1, postLoad)
assertion <-
eventually(
topicState.get.asserting(_ should contain theSameElementsAs (preLoad ++ postLoad))
)
} yield assertion
}
} yield assertion
}

Expand Down
17 changes: 17 additions & 0 deletions src/test/scala/utils/KafkaHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import java.util.UUID

import base.AsyncIntSpec
import cats.data.{NonEmptyList, NonEmptySet}
import cats.effect.implicits.*
import cats.effect.kernel.Fiber
import cats.effect.{Async, Resource}
import cats.syntax.all.*
import fs2.Stream
Expand Down Expand Up @@ -66,6 +68,21 @@ trait KafkaHelpers[F[_]] {
TopicLoader.load(topics, strategy, consumerSettings).compile.toList.map(_.map(recordToTuple))
}

def loadAndRunR(topics: NonEmptyList[String])(
onLoad: Resource.ExitCase => F[Unit],
onRecord: ((String, String)) => F[Unit]
)(implicit
consumerSettings: ConsumerSettings[F, String, String],
F: Async[F]
): Resource[F, Fiber[F, Throwable, Unit]] = Resource.make {
loadAndRunLoader(topics)(onLoad)
.map(recordToTuple)
.evalTap(onRecord)
.compile
.drain
.start
}(_.cancel.void)

def loadAndRunLoader(topics: NonEmptyList[String])(onLoad: Resource.ExitCase => F[Unit])(implicit
consumerSettings: ConsumerSettings[F, String, String],
F: Async[F]
Expand Down

0 comments on commit 55e77c4

Please sign in to comment.