Skip to content

Commit d25e358

Browse files
committed
fix test
1 parent e19d69c commit d25e358

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

Diff for: clients/src/test/java/org/apache/kafka/clients/consumer/KafkaConsumerTest.java

+30-8
Original file line numberDiff line numberDiff line change
@@ -3085,7 +3085,7 @@ public void testPollTimeMetrics(GroupProtocol groupProtocol) {
30853085

30863086
@ParameterizedTest
30873087
@EnumSource(GroupProtocol.class)
3088-
public void testPollIdleRatio(GroupProtocol groupProtocol) {
3088+
public void testPollIdleRatio(GroupProtocol groupProtocol) {
30893089
ConsumerMetadata metadata = createMetadata(subscription);
30903090
MockClient client = new MockClient(time, metadata);
30913091
initMetadata(client, Collections.singletonMap(topic, 1));
@@ -3098,31 +3098,53 @@ public void testPollIdleRatio(GroupProtocol groupProtocol) {
30983098
assertEquals(Double.NaN, consumer.metrics().get(pollIdleRatio).metricValue());
30993099

31003100
// 1st poll
3101-
// Spend 50ms in poll so value = 1.0
3101+
// Spend 50ms in poll. value=NaN because "the fraction of time spent inside poll" is undefined until the polling interval has an end point,
3102+
// which is only known once the second poll starts.
3103+
// This also means the metric will always exclude the latest poll, since we don't know how much time is spent outside poll for that interval
3104+
// until poll is called again
31023105
consumer.kafkaConsumerMetrics().recordPollStart(time.milliseconds());
31033106
time.sleep(50);
31043107
consumer.kafkaConsumerMetrics().recordPollEnd(time.milliseconds());
31053108

3106-
assertEquals(1.0d, consumer.metrics().get(pollIdleRatio).metricValue());
3109+
assertEquals(Double.NaN, consumer.metrics().get(pollIdleRatio).metricValue());
31073110

31083111
// 2nd poll
3109-
// Spend 50m outside poll and 0ms in poll so value = 0.0
3112+
// Spend 50m outside poll and 0ms in poll. value=0.5 from the previous poll plus the time until this poll starts.
31103113
time.sleep(50);
31113114
consumer.kafkaConsumerMetrics().recordPollStart(time.milliseconds());
31123115
consumer.kafkaConsumerMetrics().recordPollEnd(time.milliseconds());
31133116

3114-
// Avg of first two data points
3115-
assertEquals((1.0d + 0.0d) / 2, consumer.metrics().get(pollIdleRatio).metricValue());
3117+
// Avg of first single data point where we spent half of the time inside poll
3118+
assertEquals(0.5, consumer.metrics().get(pollIdleRatio).metricValue());
31163119

31173120
// 3rd poll
3118-
// Spend 25ms outside poll and 25ms in poll so value = 0.5
3121+
// Spend 25ms outside poll and 25ms in poll. value=0.0 from the previous empty poll.
31193122
time.sleep(25);
31203123
consumer.kafkaConsumerMetrics().recordPollStart(time.milliseconds());
31213124
time.sleep(25);
31223125
consumer.kafkaConsumerMetrics().recordPollEnd(time.milliseconds());
31233126

3127+
// Avg of two data points
3128+
assertEquals((0.5d + 0.0d) / 2, consumer.metrics().get(pollIdleRatio).metricValue());
3129+
3130+
// 4th poll
3131+
// Spend 0ms inside and outside poll. value = 1.0 since the last poll spent 25ms inside poll and 0ms outside afterward.
3132+
consumer.kafkaConsumerMetrics().recordPollStart(time.milliseconds());
3133+
consumer.kafkaConsumerMetrics().recordPollEnd(time.milliseconds());
3134+
31243135
// Avg of three data points
3125-
assertEquals((1.0d + 0.0d + 0.5d) / 3, consumer.metrics().get(pollIdleRatio).metricValue());
3136+
assertEquals((0.5d + 0.0d + 1.0d) / 3, consumer.metrics().get(pollIdleRatio).metricValue());
3137+
3138+
// Spend 10ms inside and outside poll 5 times. value = 0.0 the first time, then 0.5 after that.
3139+
for (int i = 0; i < 5; i++) {
3140+
time.sleep(10);
3141+
consumer.kafkaConsumerMetrics().recordPollStart(time.milliseconds());
3142+
time.sleep(10);
3143+
consumer.kafkaConsumerMetrics().recordPollEnd(time.milliseconds());
3144+
}
3145+
3146+
// Avg of the previous data points, plus the 0ms poll, plus the first 4 10ms polls.
3147+
assertEquals((0.5d + 0.0d + 1.0d + 0.0d + 0.5d * 4) / (3 + 1 + 4), consumer.metrics().get(pollIdleRatio).metricValue());
31263148
}
31273149

31283150
private static boolean consumerMetricPresent(KafkaConsumer<String, String> consumer, String name) {

0 commit comments

Comments
 (0)