forked from zio-archive/zio-connect
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
connectors/kafka-connector/src/main/scala/zio/connect/kafka/KafkaConnector.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package zio.connect.kafka | ||
|
||
import zio.kafka.consumer._ | ||
import zio.stream._ | ||
|
||
trait KafkaConnector { | ||
def read(topic: => String): ZStream[Any, Throwable, CommittableRecord[String, String]] | ||
} |
22 changes: 22 additions & 0 deletions
22
connectors/kafka-connector/src/main/scala/zio/connect/kafka/LiveKafkaConnector.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package zio.connect.kafka | ||
|
||
import zio._ | ||
import zio.kafka.consumer._ | ||
import zio.kafka.serde._ | ||
import zio.stream._ | ||
|
||
case class LiveKafkaConnector(consumer: Consumer) extends KafkaConnector { | ||
|
||
def read(topic: => String): ZStream[Any, Throwable, CommittableRecord[String, String]] = | ||
consumer | ||
.subscribeAnd(Subscription.Topics(Set(topic))) | ||
.plainStream(Serde.string, Serde.string) | ||
|
||
} | ||
|
||
|
||
object LiveKafkaConnector { | ||
val layer: ZLayer[Consumer, Nothing, KafkaConnector] = | ||
ZLayer.fromZIO(ZIO.service[Consumer].map(LiveKafkaConnector(_))) | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
connectors/kafka-connector/src/main/scala/zio/connect/kafka/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package zio.connect | ||
|
||
import zio.kafka.consumer._ | ||
import zio.stream._ | ||
|
||
package object kafka { | ||
def read(topic: => String): ZStream[KafkaConnector, Throwable, CommittableRecord[String, String]] = | ||
ZStream.serviceWithStream(_.read(topic)) | ||
|
||
val kafkaConnectorLiveLayer = LiveKafkaConnector.layer | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
connectors/kafka-connector/src/test/scala/zio/connect/kafka/KafkaConnectorSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package zio.connect.kafka | ||
|
||
/* | ||
import zio.ZLayer | ||
import zio.kafka.KafkaTestUtils._ | ||
import zio.kafka.consumer._ | ||
import zio.kafka.embedded._ | ||
import zio.kafka.producer._ | ||
import zio.test.Assertion._ | ||
*/ | ||
import zio.test._ | ||
|
||
trait KafkaConnectorSpec extends ZIOSpecDefault{ | ||
|
||
/* | ||
case class KafkaConnectorTestContext( | ||
tempTopic: String, | ||
tempClient: String, | ||
tempGroup: String | ||
) | ||
val kafkaConnectorSpec: Spec[KafkaConnectorTestContext with KafkaConnector with Kafka with Producer, Throwable] = | ||
readSuite | ||
val tempTopic = "tempTestTopic" | ||
val tempClient = "tempTestClient" | ||
val tempGroup = "tempTestGroup" | ||
val testKey1 = "testKey1" | ||
val testMessage1 = "testMessage1" | ||
private lazy val readSuite = | ||
suite("read")( | ||
test("succeeds") { | ||
val kvs = (1 to 5).toList.map(i => (s"key$i", s"msg$i")) | ||
for { | ||
_ <- produceMany(tempTopic, kvs) | ||
records <- read(tempTopic) | ||
.take(5) | ||
.runCollect | ||
kvOut = records.map(r => (r.record.key, r.record.value)).toList | ||
} yield assert(kvOut)(equalTo(kvs)) | ||
}.provide(ZLayer.succeed(KafkaConnectorTestContext("a", "b", "c"))) | ||
) | ||
*/ | ||
|
||
|
||
} |
49 changes: 49 additions & 0 deletions
49
connectors/kafka-connector/src/test/scala/zio/connect/kafka/LiveKafkaConnectorSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package zio.connect.kafka | ||
|
||
import zio._ | ||
import zio.kafka.KafkaTestUtils._ | ||
import zio.kafka.embedded.Kafka | ||
import zio.test._ | ||
import zio.test.Assertion._ | ||
import zio.test.TestAspect._ | ||
|
||
object LiveKafkaConnectorSpec extends KafkaConnectorSpec { | ||
|
||
/* | ||
override def spec = | ||
suite("LiveKafkaConnectorSpec")(kafkaConnectorSpec) | ||
.provide( | ||
zio.connect.kafka.kafkaConnectorLiveLayer, | ||
embedded, | ||
KafkaTestUtils.producer, | ||
//ZIO.serviceWithZIO[KafkaConnectorTestContext](ctx => KafkaTestUtils.consumer(ctx.tempClient) | ||
) | ||
*/ | ||
|
||
val tempTopic = "tempTestTopic" | ||
val tempClient = "tempTestClient" | ||
val tempGroup = "tempTestGroup" | ||
val testKey1 = "testKey1" | ||
val testMessage1 = "testMessage1" | ||
|
||
override def spec = | ||
suite("read")( | ||
test("succeeds") { | ||
val kvs = (1 to 5).toList.map(i => (s"key$i", s"msg$i")) | ||
for { | ||
_ <- produceMany(tempTopic, kvs) | ||
records <- read(tempTopic) | ||
.take(5) | ||
.runCollect | ||
kvOut = records.map(r => (r.record.key, r.record.value)).toList | ||
} yield assert(kvOut)(equalTo(kvs)) | ||
}.provide( | ||
zio.connect.kafka.kafkaConnectorLiveLayer, | ||
consumer("clientId", Some("groupId")), | ||
producer, | ||
Kafka.embedded | ||
) | ||
) @@ withLiveClock @@ timeout( | ||
300.seconds | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import sbt._ | ||
|
||
object KafkaDependencies { | ||
|
||
lazy val zioKafkaVersion = "2.0.1" | ||
lazy val `zio-kafka` = "dev.zio" %% "zio-kafka" % zioKafkaVersion | ||
lazy val `zio-kafka-test-utils` = "dev.zio" %% "zio-kafka-test-utils" % zioKafkaVersion % "test" | ||
|
||
} |