|
| 1 | +/** |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package io.streamnative.pulsar.handlers.kop; |
| 15 | + |
| 16 | + |
| 17 | +import static org.testng.Assert.assertEquals; |
| 18 | +import static org.testng.Assert.assertFalse; |
| 19 | +import static org.testng.Assert.assertNotNull; |
| 20 | +import static org.testng.Assert.assertTrue; |
| 21 | + |
| 22 | +import java.time.Duration; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Properties; |
| 26 | +import lombok.extern.slf4j.Slf4j; |
| 27 | +import org.apache.kafka.clients.consumer.ConsumerRecord; |
| 28 | +import org.apache.kafka.clients.consumer.ConsumerRecords; |
| 29 | +import org.apache.kafka.clients.producer.KafkaProducer; |
| 30 | +import org.apache.kafka.clients.producer.ProducerConfig; |
| 31 | +import org.apache.kafka.clients.producer.ProducerRecord; |
| 32 | +import org.apache.kafka.common.serialization.IntegerSerializer; |
| 33 | +import org.apache.kafka.common.serialization.StringSerializer; |
| 34 | +import org.apache.pulsar.common.policies.data.BundlesData; |
| 35 | +import org.awaitility.Awaitility; |
| 36 | +import org.testng.annotations.AfterClass; |
| 37 | +import org.testng.annotations.BeforeClass; |
| 38 | +import org.testng.annotations.Test; |
| 39 | + |
| 40 | +/** |
| 41 | + * Validate CacheInvalidator. |
| 42 | + */ |
| 43 | +@Slf4j |
| 44 | +public class CacheInvalidatorTest extends KopProtocolHandlerTestBase { |
| 45 | + |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testCacheInvalidatorIsTriggered() throws Exception { |
| 49 | + String topicName = "testCacheInvalidatorIsTriggered"; |
| 50 | + String kafkaServer = "localhost:" + getKafkaBrokerPort(); |
| 51 | + String transactionalId = "xxxx"; |
| 52 | + |
| 53 | + Properties producerProps = new Properties(); |
| 54 | + producerProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaServer); |
| 55 | + producerProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, IntegerSerializer.class.getName()); |
| 56 | + producerProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); |
| 57 | + producerProps.put(ProducerConfig.REQUEST_TIMEOUT_MS_CONFIG, 1000 * 10); |
| 58 | + producerProps.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, transactionalId); |
| 59 | + |
| 60 | + try (KafkaProducer<Integer, String> producer = new KafkaProducer<>(producerProps)) { |
| 61 | + producer.initTransactions(); |
| 62 | + producer.beginTransaction(); |
| 63 | + producer.send(new ProducerRecord<>(topicName, 1, "value")).get(); |
| 64 | + producer.commitTransaction(); |
| 65 | + } |
| 66 | + |
| 67 | + try (KConsumer kConsumer = new KConsumer(topicName, getKafkaBrokerPort(), true)) { |
| 68 | + kConsumer.getConsumer().subscribe(Collections.singleton(topicName)); |
| 69 | + ConsumerRecords<Integer, String> records = kConsumer.getConsumer().poll(Duration.ofSeconds(5)); |
| 70 | + assertNotNull(records); |
| 71 | + assertEquals(1, records.count()); |
| 72 | + ConsumerRecord<Integer, String> record = records.iterator().next(); |
| 73 | + assertEquals(1, record.key().intValue()); |
| 74 | + assertEquals("value", record.value()); |
| 75 | + } |
| 76 | + |
| 77 | + assertFalse(KopBrokerLookupManager.KOP_ADDRESS_CACHE.isEmpty()); |
| 78 | + assertFalse(KopBrokerLookupManager.LOOKUP_CACHE.isEmpty()); |
| 79 | + |
| 80 | + BundlesData bundles = pulsar.getAdminClient().namespaces().getBundles( |
| 81 | + conf.getKafkaTenant() + "/" + conf.getKafkaNamespace()); |
| 82 | + List<String> boundaries = bundles.getBoundaries(); |
| 83 | + for (int i = 0; i < boundaries.size() - 1; i++) { |
| 84 | + String bundle = String.format("%s_%s", boundaries.get(i), boundaries.get(i + 1)); |
| 85 | + pulsar.getAdminClient().namespaces() |
| 86 | + .unloadNamespaceBundle(conf.getKafkaTenant() + "/" + conf.getKafkaNamespace(), bundle); |
| 87 | + } |
| 88 | + |
| 89 | + Awaitility.await().untilAsserted(() -> { |
| 90 | + log.info("LOOKUP_CACHE {}", KopBrokerLookupManager.LOOKUP_CACHE); |
| 91 | + log.info("KOP_ADDRESS_CACHE {}", KopBrokerLookupManager.KOP_ADDRESS_CACHE); |
| 92 | + assertTrue(KopBrokerLookupManager.KOP_ADDRESS_CACHE.isEmpty()); |
| 93 | + assertTrue(KopBrokerLookupManager.LOOKUP_CACHE.isEmpty()); |
| 94 | + }); |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + @BeforeClass |
| 99 | + @Override |
| 100 | + protected void setup() throws Exception { |
| 101 | + super.internalSetup(); |
| 102 | + } |
| 103 | + |
| 104 | + @AfterClass(alwaysRun = true) |
| 105 | + @Override |
| 106 | + protected void cleanup() throws Exception { |
| 107 | + super.internalCleanup(); |
| 108 | + } |
| 109 | +} |
0 commit comments