-
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.
Add unit test for
QueueSizeBasedFetchStrategy
(#1017)
Introduce abstract class `PartitionStream` to only expose the relevant fields to `FetchStrategy` (also it makes it easier to create test values).
- Loading branch information
1 parent
080d44a
commit 5c01343
Showing
3 changed files
with
64 additions
and
4 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
zio-kafka-test/src/test/scala/zio/kafka/consumer/fetch/QueueSizeBasedFetchStrategySpec.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,55 @@ | ||
package zio.kafka.consumer.fetch | ||
|
||
import org.apache.kafka.common.TopicPartition | ||
import zio.kafka.ZIOSpecDefaultSlf4j | ||
import zio.kafka.consumer.internal.PartitionStream | ||
import zio.test.{ assertTrue, Spec, TestEnvironment } | ||
import zio.{ Chunk, Scope, UIO, ZIO } | ||
|
||
object QueueSizeBasedFetchStrategySpec extends ZIOSpecDefaultSlf4j { | ||
|
||
private val partitionPreFetchBufferLimit = 50 | ||
private val fetchStrategy = QueueSizeBasedFetchStrategy(partitionPreFetchBufferLimit) | ||
|
||
private val tp10 = new TopicPartition("topic1", 0) | ||
private val tp11 = new TopicPartition("topic1", 1) | ||
private val tp20 = new TopicPartition("topic2", 0) | ||
|
||
override def spec: Spec[TestEnvironment with Scope, Any] = | ||
suite("QueueSizeBasedFetchStrategySpec")( | ||
test("stream with queue size above limit is paused") { | ||
val streams = Chunk(newStream(tp10, currentQueueSize = 100)) | ||
for { | ||
result <- fetchStrategy.selectPartitionsToFetch(streams) | ||
} yield assertTrue(result.isEmpty) | ||
}, | ||
test("stream with queue size equal to limit is paused") { | ||
val streams = Chunk(newStream(tp10, currentQueueSize = partitionPreFetchBufferLimit)) | ||
for { | ||
result <- fetchStrategy.selectPartitionsToFetch(streams) | ||
} yield assertTrue(result.isEmpty) | ||
}, | ||
test("stream with queue size below limit may resume") { | ||
val streams = Chunk(newStream(tp10, currentQueueSize = 10)) | ||
for { | ||
result <- fetchStrategy.selectPartitionsToFetch(streams) | ||
} yield assertTrue(result == Set(tp10)) | ||
}, | ||
test("some streams may resume") { | ||
val streams = Chunk( | ||
newStream(tp10, currentQueueSize = 10), | ||
newStream(tp11, currentQueueSize = partitionPreFetchBufferLimit - 1), | ||
newStream(tp20, currentQueueSize = 100) | ||
) | ||
for { | ||
result <- fetchStrategy.selectPartitionsToFetch(streams) | ||
} yield assertTrue(result == Set(tp10, tp11)) | ||
} | ||
) | ||
|
||
private def newStream(topicPartition: TopicPartition, currentQueueSize: Int): PartitionStream = | ||
new PartitionStream { | ||
override def tp: TopicPartition = topicPartition | ||
override def queueSize: UIO[Int] = ZIO.succeed(currentQueueSize) | ||
} | ||
} |
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