-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests for PartitionStreamControl + fix issue with interruptionPromise (…
…#1371) The issue became apparent from the tests. It unfortunately means that #1251 did not fix the issue of blocking when partitions are lost when there is no more data in the queue. * Fix: race should be raceFirst, because interruptionPromise will only ever fail (relates to #1251) * Reset queue size on lost * Abstract requesting data for testing purposes
- Loading branch information
1 parent
0f46a92
commit 727e7d7
Showing
3 changed files
with
200 additions
and
7 deletions.
There are no files selected for viewing
184 changes: 184 additions & 0 deletions
184
zio-kafka-test/src/test/scala/zio/kafka/consumer/internal/PartitionStreamControlSpec.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,184 @@ | ||
package zio.kafka.consumer.internal | ||
|
||
import org.apache.kafka.clients.consumer.ConsumerRecord | ||
import org.apache.kafka.common.TopicPartition | ||
import zio._ | ||
import zio.kafka.consumer.CommittableRecord | ||
import zio.kafka.consumer.diagnostics.Diagnostics | ||
import zio.kafka.consumer.internal.Runloop.ByteArrayCommittableRecord | ||
import zio.test._ | ||
|
||
import java.util.concurrent.TimeoutException | ||
|
||
object PartitionStreamControlSpec extends ZIOSpecDefault { | ||
override def spec: Spec[Any, Throwable] = suite("PartitionStreamControl")( | ||
suite("Queue info")( | ||
test("offerRecords updates queue size correctly") { | ||
for { | ||
control <- createTestControl | ||
records = createTestRecords(3) | ||
_ <- control.offerRecords(records) | ||
size <- control.queueSize | ||
} yield assertTrue(size == 3) | ||
}, | ||
test("empty offerRecords updates outstandingPolls") { | ||
for { | ||
control <- createTestControl | ||
_ <- control.offerRecords(Chunk.empty) | ||
_ <- control.offerRecords(Chunk.empty) | ||
polls <- control.outstandingPolls | ||
} yield assertTrue(polls == 2) | ||
}, | ||
test("multiple offers accumulate correctly") { | ||
for { | ||
control <- createTestControl | ||
_ <- control.offerRecords(createTestRecords(2)) | ||
_ <- control.offerRecords(createTestRecords(3)) | ||
size <- control.queueSize | ||
} yield assertTrue(size == 5) | ||
} | ||
), | ||
// Stream Control Tests | ||
suite("Stream control")( | ||
test("offered records end up in the stream") { | ||
for { | ||
control <- createTestControl | ||
records = createTestRecords(3) | ||
_ <- control.offerRecords(records) | ||
stream = control.stream | ||
pulledRecords <- stream.take(3).runCollect | ||
} yield assertTrue(records == pulledRecords) | ||
}, | ||
test("end will end the stream") { | ||
for { | ||
control <- createTestControl | ||
records = createTestRecords(3) | ||
_ <- control.offerRecords(records) | ||
_ <- control.end | ||
pulledRecords <- control.stream.runCollect | ||
} yield assertTrue(records == pulledRecords) | ||
}, | ||
test("halt completes the stream with a TimeoutException") { | ||
for { | ||
control <- createTestControl | ||
_ <- control.halt | ||
result <- control.stream.runCollect.exit | ||
} yield assertTrue(result.isFailure, result.causeOrNull.squash.isInstanceOf[TimeoutException]) | ||
}, | ||
test("lost clears queue and ends stream") { | ||
for { | ||
control <- createTestControl | ||
_ <- control.offerRecords(createTestRecords(5)) | ||
_ <- control.lost | ||
_ <- control.stream.runCollect | ||
size <- control.queueSize | ||
completed <- control.isCompleted | ||
} yield assertTrue(size == 0, completed) | ||
}, | ||
test("finalizing the stream will set isCompleted") { | ||
for { | ||
control <- createTestControl | ||
initialIsCompleted <- control.isCompleted | ||
records = createTestRecords(3) | ||
_ <- control.offerRecords(records) | ||
_ <- control.end | ||
_ <- control.stream.runCollect | ||
finalIsCompleted <- control.isCompleted | ||
} yield assertTrue(!initialIsCompleted, finalIsCompleted) | ||
}, | ||
test("pulling from the stream will request data") { | ||
ZIO.scoped { | ||
for { | ||
requested <- Promise.make[Nothing, Unit] | ||
control <- createTestControlWithRequestData(requested.succeed(())) | ||
_ <- control.stream.runCollect.forkScoped | ||
_ <- requested.await | ||
} yield assertCompletes | ||
} | ||
}, | ||
test("pulling from the stream when there are records in the queue will request additional data") { | ||
ZIO.scoped { | ||
for { | ||
requested <- Promise.make[Nothing, Unit] | ||
control <- createTestControlWithRequestData(requested.succeed(())) | ||
records = createTestRecords(3) | ||
_ <- control.offerRecords(records) | ||
_ <- control.stream.runCollect.forkScoped | ||
_ <- requested.await | ||
} yield assertCompletes | ||
} | ||
} | ||
), | ||
suite("Poll deadline")( | ||
test("maxPollIntervalExceeded returns false initially") { | ||
for { | ||
control <- createTestControl | ||
now <- Clock.nanoTime | ||
exceeded <- control.maxPollIntervalExceeded(now) | ||
} yield assertTrue(!exceeded) | ||
}, | ||
test("maxPollIntervalExceeded returns true after timeout") { | ||
for { | ||
control <- createTestControl | ||
_ <- control.offerRecords(createTestRecords(1)) | ||
now <- Clock.nanoTime | ||
futureTime = now + Duration.fromSeconds(31).toNanos | ||
exceeded <- control.maxPollIntervalExceeded(futureTime) | ||
} yield assertTrue(exceeded) | ||
} | ||
), | ||
suite("Offset Tracking")( | ||
test("lastPulledOffset updates correctly after each pull") { | ||
for { | ||
control <- createTestControl | ||
records = createTestRecords(6) | ||
_ <- control.offerRecords(records.take(3)) | ||
offerNextBatch = control.offerRecords(records.slice(3, 6)) | ||
|
||
offsetsAfterChunks <- Ref.make(Chunk.empty[Option[Long]]) | ||
_ <- { | ||
def updateLastPulledOffsets = | ||
control.lastPulledOffset.flatMap(offset => offsetsAfterChunks.update(_ :+ offset.map(_.offset))) | ||
|
||
updateLastPulledOffsets *> control.stream | ||
.mapChunksZIO(updateLastPulledOffsets *> offerNextBatch.as(_)) | ||
.take(6) | ||
.runCollect | ||
} | ||
lastPulledOffsets <- offsetsAfterChunks.get | ||
} yield assertTrue(lastPulledOffsets == Chunk(None, Some(3L), Some(6L))) | ||
} | ||
) | ||
) @@ TestAspect.withLiveClock @@ TestAspect.timeout(1.minute) | ||
|
||
private def createTestControl: ZIO[Any, Nothing, PartitionStreamControl] = | ||
createTestControlWithRequestData(ZIO.unit) | ||
|
||
private def createTestControlWithRequestData(requestData: UIO[Any]): ZIO[Any, Nothing, PartitionStreamControl] = { | ||
val tp = new TopicPartition("test-topic", 0) | ||
val diagnostics = Diagnostics.NoOp | ||
PartitionStreamControl.newPartitionStream( | ||
tp, | ||
requestData.unit, | ||
diagnostics, | ||
Duration.fromSeconds(30) | ||
) | ||
} | ||
|
||
private def createTestRecords(count: Int): Chunk[ByteArrayCommittableRecord] = | ||
Chunk.fromIterable( | ||
(1 to count).map(i => | ||
CommittableRecord( | ||
record = new ConsumerRecord[Array[Byte], Array[Byte]]( | ||
"test-topic", | ||
0, | ||
i.toLong, | ||
Array[Byte](), | ||
Array[Byte]() | ||
), | ||
commitHandle = _ => ZIO.unit, | ||
consumerGroupMetadata = None | ||
) | ||
) | ||
) | ||
} |
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
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