|
| 1 | +package org.hypertrace.core.kafka.event.listener; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | +import static org.mockito.Mockito.mock; |
| 7 | + |
| 8 | +import com.github.benmanes.caffeine.cache.AsyncLoadingCache; |
| 9 | +import com.github.benmanes.caffeine.cache.Caffeine; |
| 10 | +import com.typesafe.config.Config; |
| 11 | +import com.typesafe.config.ConfigException; |
| 12 | +import com.typesafe.config.ConfigFactory; |
| 13 | +import java.time.Duration; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.concurrent.CompletableFuture; |
| 18 | +import java.util.concurrent.TimeUnit; |
| 19 | +import org.apache.kafka.clients.consumer.Consumer; |
| 20 | +import org.apache.kafka.clients.consumer.ConsumerRecord; |
| 21 | +import org.apache.kafka.clients.consumer.MockConsumer; |
| 22 | +import org.apache.kafka.clients.consumer.OffsetResetStrategy; |
| 23 | +import org.apache.kafka.common.Node; |
| 24 | +import org.apache.kafka.common.PartitionInfo; |
| 25 | +import org.apache.kafka.common.TopicPartition; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +class KafkaLiveEventListenerTest { |
| 29 | + |
| 30 | + @Test |
| 31 | + void testThrowOnInvalidInputs() { |
| 32 | + // no callback |
| 33 | + assertThrows( |
| 34 | + IllegalArgumentException.class, |
| 35 | + () -> |
| 36 | + new KafkaLiveEventListener.Builder<String, Long>() |
| 37 | + .build( |
| 38 | + "", |
| 39 | + ConfigFactory.parseMap(Map.of("topic.name", "")), |
| 40 | + new MockConsumer<>(OffsetResetStrategy.LATEST))); |
| 41 | + // no topic name |
| 42 | + assertThrows( |
| 43 | + ConfigException.class, |
| 44 | + () -> |
| 45 | + new KafkaLiveEventListener.Builder<String, Long>() |
| 46 | + .registerCallback((String k, Long v) -> System.out.println(k + ":" + v)) |
| 47 | + .build("", ConfigFactory.empty(), new MockConsumer<>(OffsetResetStrategy.LATEST))); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void testEventModificationCache() throws Exception { |
| 52 | + // kafka consumer mock setup |
| 53 | + MockConsumer<String, Long> kafkaConsumer = new MockConsumer<>(OffsetResetStrategy.LATEST); |
| 54 | + String topic = "event-update-topic"; |
| 55 | + kafkaConsumer.updatePartitions( |
| 56 | + topic, |
| 57 | + List.of( |
| 58 | + getPartitionInfo(topic, 0), |
| 59 | + getPartitionInfo(topic, 1), |
| 60 | + getPartitionInfo(topic, 2), |
| 61 | + getPartitionInfo(topic, 3))); |
| 62 | + HashMap<TopicPartition, Long> endOffsets = new HashMap<>(); |
| 63 | + endOffsets.put(new TopicPartition(topic, 0), 50L); |
| 64 | + endOffsets.put(new TopicPartition(topic, 1), 50L); |
| 65 | + endOffsets.put(new TopicPartition(topic, 2), 50L); |
| 66 | + endOffsets.put(new TopicPartition(topic, 3), 50L); |
| 67 | + kafkaConsumer.updateEndOffsets(endOffsets); |
| 68 | + // create instance of event modification cache consuming from this consumer |
| 69 | + EventModificationCache eventModificationCache = |
| 70 | + new EventModificationCache( |
| 71 | + "modification-event-consumer", |
| 72 | + ConfigFactory.parseMap(Map.of("topic.name", topic, "poll.timeout", "5ms")), |
| 73 | + kafkaConsumer); |
| 74 | + Thread.sleep(10); |
| 75 | + assertEquals(10L, eventModificationCache.get(10)); |
| 76 | + assertEquals(100L, eventModificationCache.get(100)); |
| 77 | + // not present key won't trigger any population but callback function should be called |
| 78 | + kafkaConsumer.addRecord(new ConsumerRecord<>(topic, 0, 100, "32", 89L)); |
| 79 | + Thread.sleep(100); |
| 80 | + assertFalse(eventModificationCache.hasKey(32)); |
| 81 | + // existing key will be modified based on entry |
| 82 | + kafkaConsumer.addRecord(new ConsumerRecord<>(topic, 3, 200, "10", -3L)); |
| 83 | + Thread.sleep(100); |
| 84 | + assertEquals(-3L, eventModificationCache.get(10)); |
| 85 | + eventModificationCache.close(); |
| 86 | + } |
| 87 | + |
| 88 | + private PartitionInfo getPartitionInfo(String topic, int partition) { |
| 89 | + return new PartitionInfo(topic, partition, mock(Node.class), new Node[0], new Node[0]); |
| 90 | + } |
| 91 | + |
| 92 | + static class EventModificationCache { |
| 93 | + private final AsyncLoadingCache<Integer, Long> cache; |
| 94 | + private final KafkaLiveEventListener<String, Long> eventListener; |
| 95 | + |
| 96 | + EventModificationCache( |
| 97 | + String consumerName, Config kafkaConfig, Consumer<String, Long> consumer) { |
| 98 | + cache = |
| 99 | + Caffeine.newBuilder() |
| 100 | + .maximumSize(10_000) |
| 101 | + .expireAfterAccess(Duration.ofHours(6)) |
| 102 | + .refreshAfterWrite(Duration.ofHours(1)) |
| 103 | + .buildAsync(this::load); |
| 104 | + eventListener = |
| 105 | + new KafkaLiveEventListener.Builder<String, Long>() |
| 106 | + .registerCallback(this::actOnEvent) |
| 107 | + .registerCallback(this::log) |
| 108 | + .build(consumerName, kafkaConfig, consumer); |
| 109 | + } |
| 110 | + |
| 111 | + public void close() throws Exception { |
| 112 | + eventListener.close(); |
| 113 | + } |
| 114 | + |
| 115 | + long get(int key) throws Exception { |
| 116 | + return cache.get(key).get(10, TimeUnit.SECONDS); |
| 117 | + } |
| 118 | + |
| 119 | + boolean hasKey(int key) { |
| 120 | + return cache.asMap().containsKey(key); |
| 121 | + } |
| 122 | + |
| 123 | + private Long load(Integer key) { |
| 124 | + // ideally this will be remote call, just for sake of dummy test we returned a cast |
| 125 | + return (long) (key); |
| 126 | + } |
| 127 | + |
| 128 | + public void actOnEvent(String eventKey, Long eventValue) { |
| 129 | + int key = Integer.parseInt(eventKey); |
| 130 | + if (cache.asMap().containsKey(key)) { |
| 131 | + long value = eventValue; |
| 132 | + cache.put(key, CompletableFuture.completedFuture(value)); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + // just a dummy logger to showcase multiple callbacks |
| 137 | + public void log(String eventKey, Long eventValue) { |
| 138 | + System.out.println("updated cache with event data from topic " + eventKey + ":" + eventValue); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments