|
Packit |
2997f0 |
/*
|
|
Packit |
2997f0 |
* librdkafka - Apache Kafka C library
|
|
Packit |
2997f0 |
*
|
|
Packit |
2997f0 |
* Copyright (c) 2012-2013, Magnus Edenhill
|
|
Packit |
2997f0 |
* All rights reserved.
|
|
Packit |
2997f0 |
*
|
|
Packit |
2997f0 |
* Redistribution and use in source and binary forms, with or without
|
|
Packit |
2997f0 |
* modification, are permitted provided that the following conditions are met:
|
|
Packit |
2997f0 |
*
|
|
Packit |
2997f0 |
* 1. Redistributions of source code must retain the above copyright notice,
|
|
Packit |
2997f0 |
* this list of conditions and the following disclaimer.
|
|
Packit |
2997f0 |
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
Packit |
2997f0 |
* this list of conditions and the following disclaimer in the documentation
|
|
Packit |
2997f0 |
* and/or other materials provided with the distribution.
|
|
Packit |
2997f0 |
*
|
|
Packit |
2997f0 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
Packit |
2997f0 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
2997f0 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
2997f0 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
Packit |
2997f0 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
Packit |
2997f0 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
Packit |
2997f0 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
Packit |
2997f0 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
Packit |
2997f0 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
Packit |
2997f0 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
Packit |
2997f0 |
* POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
2997f0 |
*/
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
/**
|
|
Packit |
2997f0 |
* Tests messages are produced in order.
|
|
Packit |
2997f0 |
*/
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
#include "test.h"
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
/* Typical include path would be <librdkafka/rdkafka.h>, but this program
|
|
Packit |
2997f0 |
* is built from within the librdkafka source tree and thus differs. */
|
|
Packit |
2997f0 |
#include "rdkafka.h" /* for Kafka driver */
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
static int msgid_next = 0;
|
|
Packit |
2997f0 |
static int fails = 0;
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
/**
|
|
Packit |
2997f0 |
* Delivery reported callback.
|
|
Packit |
2997f0 |
* Called for each message once to signal its delivery status.
|
|
Packit |
2997f0 |
*/
|
|
Packit |
2997f0 |
static void dr_cb (rd_kafka_t *rk, void *payload, size_t len,
|
|
Packit |
2997f0 |
rd_kafka_resp_err_t err, void *opaque, void *msg_opaque) {
|
|
Packit |
2997f0 |
int msgid = *(int *)msg_opaque;
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
free(msg_opaque);
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
if (err != RD_KAFKA_RESP_ERR_NO_ERROR)
|
|
Packit |
2997f0 |
TEST_FAIL("Message delivery failed: %s\n",
|
|
Packit |
2997f0 |
rd_kafka_err2str(err));
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
if (msgid != msgid_next) {
|
|
Packit |
2997f0 |
fails++;
|
|
Packit |
2997f0 |
TEST_FAIL("Delivered msg %i, expected %i\n",
|
|
Packit |
2997f0 |
msgid, msgid_next);
|
|
Packit |
2997f0 |
return;
|
|
Packit |
2997f0 |
}
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
msgid_next = msgid+1;
|
|
Packit |
2997f0 |
}
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
int main_0005_order (int argc, char **argv) {
|
|
Packit |
2997f0 |
int partition = 0;
|
|
Packit |
2997f0 |
int r;
|
|
Packit |
2997f0 |
rd_kafka_t *rk;
|
|
Packit |
2997f0 |
rd_kafka_topic_t *rkt;
|
|
Packit |
2997f0 |
rd_kafka_conf_t *conf;
|
|
Packit |
2997f0 |
rd_kafka_topic_conf_t *topic_conf;
|
|
Packit |
2997f0 |
char msg[128];
|
|
Packit |
2997f0 |
int msgcnt = 50000;
|
|
Packit |
2997f0 |
int i;
|
|
Packit |
2997f0 |
test_timing_t t_produce, t_delivery;
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
test_conf_init(&conf, &topic_conf, 10);
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
/* Set delivery report callback */
|
|
Packit |
2997f0 |
rd_kafka_conf_set_dr_cb(conf, dr_cb);
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
/* Create kafka instance */
|
|
Packit |
2997f0 |
rk = test_create_handle(RD_KAFKA_PRODUCER, conf);
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
rkt = rd_kafka_topic_new(rk, test_mk_topic_name("0005", 0),
|
|
Packit |
2997f0 |
topic_conf);
|
|
Packit |
2997f0 |
if (!rkt)
|
|
Packit |
2997f0 |
TEST_FAIL("Failed to create topic: %s\n",
|
|
Packit |
2997f0 |
rd_strerror(errno));
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
/* Produce messages */
|
|
Packit |
2997f0 |
TIMING_START(&t_produce, "PRODUCE");
|
|
Packit |
2997f0 |
for (i = 0 ; i < msgcnt ; i++) {
|
|
Packit |
2997f0 |
int *msgidp = malloc(sizeof(*msgidp));
|
|
Packit |
2997f0 |
*msgidp = i;
|
|
Packit |
2997f0 |
rd_snprintf(msg, sizeof(msg), "%s test message #%i", argv[0], i);
|
|
Packit |
2997f0 |
r = rd_kafka_produce(rkt, partition, RD_KAFKA_MSG_F_COPY,
|
|
Packit |
2997f0 |
msg, strlen(msg), NULL, 0, msgidp);
|
|
Packit |
2997f0 |
if (r == -1)
|
|
Packit |
2997f0 |
TEST_FAIL("Failed to produce message #%i: %s\n",
|
|
Packit |
2997f0 |
i, rd_strerror(errno));
|
|
Packit |
2997f0 |
}
|
|
Packit |
2997f0 |
TIMING_STOP(&t_produce);
|
|
Packit |
2997f0 |
TEST_SAY("Produced %i messages, waiting for deliveries\n", msgcnt);
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
/* Wait for messages to be delivered */
|
|
Packit |
2997f0 |
TIMING_START(&t_delivery, "DELIVERY");
|
|
Packit |
2997f0 |
while (rd_kafka_outq_len(rk) > 0)
|
|
Packit |
2997f0 |
rd_kafka_poll(rk, 50);
|
|
Packit |
2997f0 |
TIMING_STOP(&t_delivery);
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
if (fails)
|
|
Packit |
2997f0 |
TEST_FAIL("%i failures, see previous errors", fails);
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
if (msgid_next != msgcnt)
|
|
Packit |
2997f0 |
TEST_FAIL("Still waiting for messages: next %i != end %i\n",
|
|
Packit |
2997f0 |
msgid_next, msgcnt);
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
/* Destroy topic */
|
|
Packit |
2997f0 |
rd_kafka_topic_destroy(rkt);
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
/* Destroy rdkafka instance */
|
|
Packit |
2997f0 |
TEST_SAY("Destroying kafka instance %s\n", rd_kafka_name(rk));
|
|
Packit |
2997f0 |
rd_kafka_destroy(rk);
|
|
Packit |
2997f0 |
|
|
Packit |
2997f0 |
return 0;
|
|
Packit |
2997f0 |
}
|