Skip to content

Commit ffcf895

Browse files
author
Alexander Damian
committed
Allow to pass-in via cmake all the kafka config options for testing: broker, partitions and topics
1 parent 2287e09 commit ffcf895

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

tests/CMakeLists.txt

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
11
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include/)
22
include_directories(SYSTEM ${CATCH_INCLUDE})
33

4-
set(KAFKA_TEST_INSTANCE "kafka-vm:9092"
4+
if (NOT KAFKA_TEST_INSTANCE)
5+
set(KAFKA_TEST_INSTANCE kafka-vm:9092
56
CACHE STRING "The kafka instance to which to connect to run tests")
7+
endif()
8+
if (NOT KAFKA_NUM_PARTITIONS)
9+
set(KAFKA_NUM_PARTITIONS 3 CACHE STRING "Kafka Number of partitions")
10+
endif()
11+
if (NOT KAFKA_TOPICS)
12+
set(KAFKA_TOPICS "cppkafka_test1;cppkafka_test2" CACHE STRING "Kafka topics")
13+
endif()
14+
15+
# Convert list of topics into a C++ initializer list
16+
FOREACH(TOPIC ${KAFKA_TOPICS})
17+
if (NOT TOPIC_LIST)
18+
set(TOPIC_LIST "\"${TOPIC}\"")
19+
else()
20+
set(TOPIC_LIST "${TOPIC_LIST},\"${TOPIC}\"")
21+
endif()
22+
ENDFOREACH()
23+
624
add_custom_target(tests)
725

826
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
9-
add_definitions("-DKAFKA_TEST_INSTANCE=\"${KAFKA_TEST_INSTANCE}\"")
27+
add_definitions(
28+
"-DKAFKA_TEST_INSTANCE=\"${KAFKA_TEST_INSTANCE}\""
29+
-DKAFKA_NUM_PARTITIONS=${KAFKA_NUM_PARTITIONS}
30+
-DKAFKA_TOPIC_NAMES=${TOPIC_LIST}
31+
)
1032

1133
add_executable(cppkafka_tests
1234
buffer_test.cpp
@@ -25,6 +47,6 @@ add_executable(cppkafka_tests
2547
)
2648

2749
# In CMake >= 3.15 Boost::boost == Boost::headers
28-
target_link_libraries(cppkafka_tests cppkafka RdKafka::rdkafka Boost::boost Boost::program_options )
50+
target_link_libraries(cppkafka_tests cppkafka RdKafka::rdkafka Boost::boost Boost::program_options)
2951
add_dependencies(tests cppkafka_tests)
3052
add_test(cppkafka cppkafka_tests)

tests/test_main.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ using Catch::TestCaseStats;
1515
using Catch::Totals;
1616
using Catch::Session;
1717

18-
std::vector<std::string> KAFKA_TOPICS = {"cppkafka_test1", "cppkafka_test2"};
19-
int KAFKA_NUM_PARTITIONS = 3;
18+
std::vector<std::string> KAFKA_TOPICS = {KAFKA_TOPIC_NAMES};
2019

2120
namespace cppkafka {
2221

tests/test_utils.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "cppkafka/utils/consumer_dispatcher.h"
1010

1111
extern const std::vector<std::string> KAFKA_TOPICS;
12-
extern const int KAFKA_NUM_PARTITIONS;
1312

1413
using namespace cppkafka;
1514

0 commit comments

Comments
 (0)