Add zookeeper support

This commit is contained in:
Matias Fontanini
2016-06-12 10:43:38 -07:00
parent 65126b27f1
commit 0cf8369ef9
16 changed files with 457 additions and 21 deletions

View File

@@ -18,6 +18,10 @@ endmacro()
add_definitions("-DKAFKA_TEST_INSTANCE=\"${KAFKA_TEST_INSTANCE}\"")
add_definitions("-DZOOKEEPER_TEST_INSTANCE=\"${ZOOKEEPER_TEST_INSTANCE}\"")
if (ENABLE_ZOOKEEPER)
link_libraries(zookeeper_mt)
endif()
create_test(consumer)
create_test(producer)
create_test(kafka_handle_base)
@@ -25,5 +29,4 @@ create_test(topic_partition_list)
create_test(configuration)
if (ENABLE_ZOOKEEPER)
create_test(zookeeper_watcher)
target_link_libraries(zookeeper_watcher_test zookeeper_mt)
endif()

View File

@@ -276,3 +276,46 @@ TEST_F(ProducerTest, PartitionerCallbackOnDefaultTopicConfig) {
EXPECT_EQ(partition, message.get_partition());
EXPECT_TRUE(callback_called);
}
#ifdef CPPKAFKA_HAVE_ZOOKEEPER
#include "zookeeper/zookeeper_pool.h"
TEST_F(ProducerTest, ConnectUsingZookeeper) {
int partition = 0;
Configuration producer_config;
producer_config.set("zookeeper", ZOOKEEPER_TEST_INSTANCE);
Configuration consumer_config = producer_config;
consumer_config.set("enable.auto.commit", false);
consumer_config.set("group.id", "producer_test");
// Create a consumer and assign this topic/partition
Consumer consumer(consumer_config);
consumer.assign({ TopicPartition(KAFKA_TOPIC, partition) });
ConsumerRunner runner(consumer, 1, 1);
// Now create a producer and produce a message
Producer producer(producer_config);
Topic topic = producer.get_topic(KAFKA_TOPIC);
string payload = "Hello world! 2";
string key = "such key";
producer.produce(topic, partition, Buffer(payload.data(), payload.size()),
Buffer(key.data(), key.size()));
runner.try_join();
const auto& messages = runner.get_messages();
ASSERT_EQ(1, messages.size());
const auto& message = messages[0];
EXPECT_EQ(payload, message.get_payload().as_string());
EXPECT_EQ(key, message.get_key().as_string());
EXPECT_EQ(KAFKA_TOPIC, message.get_topic());
EXPECT_EQ(partition, message.get_partition());
EXPECT_EQ(0, message.get_error());
// We should have 2 watchers
EXPECT_EQ(2, ZookeeperPool::instance().get_subscriber_count(ZOOKEEPER_TEST_INSTANCE));
}
#endif // CPPKAFKA_HAVE_ZOOKEEPER

View File

@@ -1,5 +1,6 @@
#include <gtest/gtest.h>
#include "cppkafka/zookeeper/zookeeper_watcher.h"
#include "cppkafka/exceptions.h"
using std::string;
@@ -15,3 +16,16 @@ TEST_F(ZookeeperWatcherTest, GetBrokers) {
string brokers = watcher.get_brokers();
EXPECT_EQ(KAFKA_TEST_INSTANCE, brokers);
}
TEST_F(ZookeeperWatcherTest, InvalidEndpointThrows) {
ZookeeperWatcher watcher("127.0.0.1:1212");
EXPECT_THROW(watcher.get_brokers(), ZookeeperException);
}
TEST_F(ZookeeperWatcherTest, SubscribeUnsubscribe) {
ZookeeperWatcher watcher(ZOOKEEPER_TEST_INSTANCE);
string id = watcher.subscribe([](const string&) { });
EXPECT_EQ(1, watcher.get_subscriber_count());
watcher.unsubscribe(id);
EXPECT_EQ(0, watcher.get_subscriber_count());
}