FS2 Kafka Vulcan

Testing

For testing purposes, TestSchemaRegistry provides an in-memory schema registry. In the following example, we use a test schema registry to create a serializer for Person from the quick example.

import cats.effect.{IO, Resource}
import cats.effect.syntax.all.*
import fs2.kafka.ValueSerializer
import fs2.kafka.vulcan.AvroSerializer
import org.http4s.schemaregistry.TestSchemaRegistry

val testSerializer: Resource[IO, ValueSerializer[IO, Person]] =
  for {
    testSchemaRegistry <- TestSchemaRegistry[IO].toResource
    schemaRegistryClient <- testSchemaRegistry.client.build.toResource
    serializer <- AvroSerializer.default(schemaRegistryClient).forValue[Person]
  } yield serializer

MUnit Testkit

The fs2-kafka-vulcan-testkit-munit module provides an MUnit fixture for checking whether Vulcan codecs are compatible with subjects in a schema registry.

To get started with sbt, add the following line to your build.sbt file.

libraryDependencies += "org.typelevel" %% "fs2-kafka-vulcan-testkit-munit" % "5.0.0-RC1"

Following is a quick example showing how the module can be used.

import cats.effect.IO
import cats.effect.unsafe.implicits.global
import fs2.kafka.vulcan.testkit.SchemaSuite
import org.http4s.ember.client.EmberClientBuilder
import org.http4s.schemaregistry.{SchemaRegistryClientBuilder, SubjectName}
import org.http4s.syntax.literals.*
import org.typelevel.vulcan.Codec

class MySpec extends SchemaSuite {
  val checker = compatibilityChecker(
    EmberClientBuilder.default[IO].build.evalMap { client =>
      SchemaRegistryClientBuilder.default(client, uri"https://some-schema-registry:1234").build
    }
  )

  override def munitFixtures = List(checker)

  test("my codec is compatible") {
    val myCodec: Codec[String] = Codec.string

    checker()
      .assertCompatibility(myCodec, SubjectName("my-schema-subject"))
      .unsafeRunSync()
  }
}

The schema registry checks whether the schema of the codec is compatible with the latest version of the subject, according to the compatibility level configured for the subject.