|
1 | 1 | package org.hypertrace.core.kafkastreams.framework.interceptors.metrics; |
2 | 2 |
|
3 | | -import static org.mockito.Mockito.mock; |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
4 | 5 |
|
5 | | -import io.micrometer.core.instrument.Counter; |
| 6 | +import com.google.common.collect.Maps; |
| 7 | +import com.typesafe.config.ConfigFactory; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | +import org.apache.kafka.clients.consumer.ConsumerRecord; |
| 12 | +import org.apache.kafka.clients.consumer.ConsumerRecords; |
| 13 | +import org.apache.kafka.common.TopicPartition; |
| 14 | +import org.hypertrace.core.serviceframework.metrics.PlatformMetricsRegistry; |
6 | 15 | import org.junit.jupiter.api.BeforeEach; |
7 | 16 | import org.junit.jupiter.api.Test; |
8 | 17 |
|
9 | 18 | public class MetricsInterceptorTest { |
10 | 19 |
|
11 | | - private Counter timeLagCounter; |
12 | | - private Counter numRecordsCounter; |
13 | | - private MetricsInterceptor interceptor; |
14 | | - |
15 | 20 | @BeforeEach |
16 | 21 | void setup() { |
17 | | - timeLagCounter = mock(Counter.class); |
18 | | - numRecordsCounter = mock(Counter.class); |
| 22 | + Map<String, Object> config = new HashMap<>(); |
| 23 | + config.put("reporter.names", List.of("testing")); |
| 24 | + |
| 25 | + PlatformMetricsRegistry.initMetricsRegistry("test", ConfigFactory.parseMap(config)); |
19 | 26 | } |
20 | 27 |
|
21 | 28 | @Test |
22 | | - void shouldIncrementCounters() { |
23 | | - // Record<Object, Object> record = |
24 | | - // new Record<>("key", "value", System.currentTimeMillis() - 50000); |
25 | | - // interceptor.process(record); |
26 | | - // |
27 | | - // verify(numRecordsCounter, times(1)).increment(); |
28 | | - // verify(timeLagCounter, times(1)).increment(anyDouble()); |
| 29 | + public void testOnConsume() { |
| 30 | + TopicPartition tp = new TopicPartition("test-topic", 0); |
| 31 | + List<ConsumerRecord<Object, Object>> records = |
| 32 | + List.of( |
| 33 | + new ConsumerRecord<>("test-topic", 0, 0, "k1", "v1"), |
| 34 | + new ConsumerRecord<>("test-topic", 0, 1, "k2", null)); |
| 35 | + |
| 36 | + Map<TopicPartition, List<ConsumerRecord<Object, Object>>> map = Map.of(tp, records); |
| 37 | + ConsumerRecords<Object, Object> input = new ConsumerRecords<>(map); |
| 38 | + MetricsInterceptor interceptor = new MetricsInterceptor(); |
| 39 | + interceptor.configure(Maps.newTreeMap()); |
| 40 | + ConsumerRecords<Object, Object> output = interceptor.onConsume(input); |
| 41 | + assertEquals(2, output.count()); |
| 42 | + |
| 43 | + assertEquals( |
| 44 | + 2.0, PlatformMetricsRegistry.getMeterRegistry().counter("kafka_records_count").count()); |
| 45 | + assertNotEquals( |
| 46 | + 0, PlatformMetricsRegistry.getMeterRegistry().counter("kafka_records_time_lag").count()); |
29 | 47 | } |
30 | 48 | } |
0 commit comments