@@ -26,8 +26,6 @@ using std::chrono::milliseconds;
26
26
27
27
using namespace cppkafka ;
28
28
29
- static const string KAFKA_TOPIC = " cppkafka_test1" ;
30
-
31
29
static Configuration make_producer_config () {
32
30
Configuration config = {
33
31
{ " metadata.broker.list" , KAFKA_TEST_INSTANCE },
@@ -53,29 +51,29 @@ TEST_CASE("simple production", "[producer]") {
53
51
54
52
// Create a consumer and assign this topic/partition
55
53
Consumer consumer (make_consumer_config ());
56
- consumer.assign ({ TopicPartition (KAFKA_TOPIC , partition) });
54
+ consumer.assign ({ TopicPartition (KAFKA_TOPICS[ 0 ] , partition) });
57
55
ConsumerRunner runner (consumer, 1 , 1 );
58
56
59
57
Configuration config = make_producer_config ();
60
58
SECTION (" message with no key" ) {
61
59
// Now create a producer and produce a message
62
60
const string payload = " Hello world! 1" ;
63
61
Producer producer (config);
64
- producer.produce (MessageBuilder (KAFKA_TOPIC ).partition (partition).payload (payload));
62
+ producer.produce (MessageBuilder (KAFKA_TOPICS[ 0 ] ).partition (partition).payload (payload));
65
63
runner.try_join ();
66
64
67
65
const auto & messages = runner.get_messages ();
68
66
REQUIRE (messages.size () == 1 );
69
67
const auto & message = messages[0 ];
70
68
CHECK (message.get_payload () == payload);
71
69
CHECK (!!message.get_key () == false );
72
- CHECK (message.get_topic () == KAFKA_TOPIC );
70
+ CHECK (message.get_topic () == KAFKA_TOPICS[ 0 ] );
73
71
CHECK (message.get_partition () == partition);
74
72
CHECK (!!message.get_error () == false );
75
73
76
74
int64_t low;
77
75
int64_t high;
78
- tie (low, high) = producer.query_offsets ({ KAFKA_TOPIC , partition });
76
+ tie (low, high) = producer.query_offsets ({ KAFKA_TOPICS[ 0 ] , partition });
79
77
CHECK (high > low);
80
78
}
81
79
@@ -84,7 +82,7 @@ TEST_CASE("simple production", "[producer]") {
84
82
const string key = " such key" ;
85
83
const milliseconds timestamp{15 };
86
84
Producer producer (config);
87
- producer.produce (MessageBuilder (KAFKA_TOPIC ).partition (partition)
85
+ producer.produce (MessageBuilder (KAFKA_TOPICS[ 0 ] ).partition (partition)
88
86
.key (key)
89
87
.payload (payload)
90
88
.timestamp (timestamp));
@@ -95,7 +93,7 @@ TEST_CASE("simple production", "[producer]") {
95
93
const auto & message = messages[0 ];
96
94
CHECK (message.get_payload () == payload);
97
95
CHECK (message.get_key () == key);
98
- CHECK (message.get_topic () == KAFKA_TOPIC );
96
+ CHECK (message.get_topic () == KAFKA_TOPICS[ 0 ] );
99
97
CHECK (message.get_partition () == partition);
100
98
CHECK (!!message.get_error () == false );
101
99
REQUIRE (!!message.get_timestamp () == true );
@@ -116,14 +114,14 @@ TEST_CASE("simple production", "[producer]") {
116
114
topic_config.set_partitioner_callback ([&](const Topic& topic, const Buffer& msg_key,
117
115
int32_t partition_count) {
118
116
CHECK (msg_key == key);
119
- CHECK (partition_count == 3 );
120
- CHECK (topic.get_name () == KAFKA_TOPIC );
117
+ CHECK (partition_count == KAFKA_NUM_PARTITIONS );
118
+ CHECK (topic.get_name () == KAFKA_TOPICS[ 0 ] );
121
119
return 0 ;
122
120
});
123
121
config.set_default_topic_configuration (topic_config);
124
122
125
123
Producer producer (config);
126
- producer.produce (MessageBuilder (KAFKA_TOPIC ).key (key).payload (payload));
124
+ producer.produce (MessageBuilder (KAFKA_TOPICS[ 0 ] ).key (key).payload (payload));
127
125
while (producer.get_out_queue_length () > 0 ) {
128
126
producer.poll ();
129
127
}
@@ -134,7 +132,7 @@ TEST_CASE("simple production", "[producer]") {
134
132
const auto & message = messages[0 ];
135
133
CHECK (message.get_payload () == payload);
136
134
CHECK (message.get_key () == key);
137
- CHECK (message.get_topic () == KAFKA_TOPIC );
135
+ CHECK (message.get_topic () == KAFKA_TOPICS[ 0 ] );
138
136
CHECK (message.get_partition () == partition);
139
137
CHECK (!!message.get_error () == false );
140
138
CHECK (delivery_report_called == true );
@@ -150,15 +148,15 @@ TEST_CASE("simple production", "[producer]") {
150
148
topic_config.set_partitioner_callback ([&](const Topic& topic, const Buffer& msg_key,
151
149
int32_t partition_count) {
152
150
CHECK (msg_key == key);
153
- CHECK (partition_count == 3 );
154
- CHECK (topic.get_name () == KAFKA_TOPIC );
151
+ CHECK (partition_count == KAFKA_NUM_PARTITIONS );
152
+ CHECK (topic.get_name () == KAFKA_TOPICS[ 0 ] );
155
153
callback_called = true ;
156
154
return 0 ;
157
155
});
158
156
config.set_default_topic_configuration (topic_config);
159
157
Producer producer (config);
160
158
161
- producer.produce (MessageBuilder (KAFKA_TOPIC ).key (key).payload (payload));
159
+ producer.produce (MessageBuilder (KAFKA_TOPICS[ 0 ] ).key (key).payload (payload));
162
160
producer.poll ();
163
161
runner.try_join ();
164
162
@@ -172,33 +170,32 @@ TEST_CASE("simple production", "[producer]") {
172
170
173
171
TEST_CASE (" multiple messages" , " [producer]" ) {
174
172
size_t message_count = 10 ;
175
- int partitions = 3 ;
176
173
set<string> payloads;
177
174
178
175
// Create a consumer and subscribe to this topic
179
176
Consumer consumer (make_consumer_config ());
180
- consumer.subscribe ({ KAFKA_TOPIC });
181
- ConsumerRunner runner (consumer, message_count, partitions );
177
+ consumer.subscribe ({ KAFKA_TOPICS[ 0 ] });
178
+ ConsumerRunner runner (consumer, message_count, KAFKA_NUM_PARTITIONS );
182
179
183
180
// Now create a producer and produce a message
184
181
Producer producer (make_producer_config ());
185
182
const string payload_base = " Hello world " ;
186
183
for (size_t i = 0 ; i < message_count; ++i) {
187
184
const string payload = payload_base + to_string (i);
188
185
payloads.insert (payload);
189
- producer.produce (MessageBuilder (KAFKA_TOPIC ).payload (payload));
186
+ producer.produce (MessageBuilder (KAFKA_TOPICS[ 0 ] ).payload (payload));
190
187
}
191
188
runner.try_join ();
192
189
193
190
const auto & messages = runner.get_messages ();
194
191
REQUIRE (messages.size () == message_count);
195
192
for (const auto & message : messages) {
196
- CHECK (message.get_topic () == KAFKA_TOPIC );
193
+ CHECK (message.get_topic () == KAFKA_TOPICS[ 0 ] );
197
194
CHECK (payloads.erase (message.get_payload ()) == 1 );
198
195
CHECK (!!message.get_error () == false );
199
196
CHECK (!!message.get_key () == false );
200
197
CHECK (message.get_partition () >= 0 );
201
- CHECK (message.get_partition () < 3 );
198
+ CHECK (message.get_partition () < KAFKA_NUM_PARTITIONS );
202
199
}
203
200
}
204
201
@@ -207,30 +204,30 @@ TEST_CASE("buffered producer", "[producer]") {
207
204
208
205
// Create a consumer and assign this topic/partition
209
206
Consumer consumer (make_consumer_config ());
210
- consumer.assign ({ TopicPartition (KAFKA_TOPIC , partition) });
207
+ consumer.assign ({ TopicPartition (KAFKA_TOPICS[ 0 ] , partition) });
211
208
ConsumerRunner runner (consumer, 3 , 1 );
212
209
213
210
// Now create a buffered producer and produce two messages
214
211
BufferedProducer<string> producer (make_producer_config ());
215
212
const string payload = " Hello world! 2" ;
216
213
const string key = " such key" ;
217
- producer.add_message (MessageBuilder (KAFKA_TOPIC ).partition (partition)
214
+ producer.add_message (MessageBuilder (KAFKA_TOPICS[ 0 ] ).partition (partition)
218
215
.key (key)
219
216
.payload (payload));
220
- producer.add_message (producer.make_builder (KAFKA_TOPIC ).partition (partition).payload (payload));
217
+ producer.add_message (producer.make_builder (KAFKA_TOPICS[ 0 ] ).partition (partition).payload (payload));
221
218
producer.flush ();
222
- producer.produce (MessageBuilder (KAFKA_TOPIC ).partition (partition).payload (payload));
219
+ producer.produce (MessageBuilder (KAFKA_TOPICS[ 0 ] ).partition (partition).payload (payload));
223
220
producer.wait_for_acks ();
224
221
// Add another one but then clear it
225
- producer.add_message (producer.make_builder (KAFKA_TOPIC ).partition (partition).payload (payload));
222
+ producer.add_message (producer.make_builder (KAFKA_TOPICS[ 0 ] ).partition (partition).payload (payload));
226
223
producer.clear ();
227
224
runner.try_join ();
228
225
229
226
const auto & messages = runner.get_messages ();
230
227
REQUIRE (messages.size () == 3 );
231
228
const auto & message = messages[0 ];
232
229
CHECK (message.get_key () == key);
233
- CHECK (message.get_topic () == KAFKA_TOPIC );
230
+ CHECK (message.get_topic () == KAFKA_TOPICS[ 0 ] );
234
231
CHECK (message.get_partition () == partition);
235
232
CHECK (!!message.get_error () == false );
236
233
0 commit comments