Example #1
0
static void verify_consumed_msg0 (const char *func, int line,
				  uint64_t testid, int32_t partition,
				  int msgnum,
				  rd_kafka_message_t *rkmessage) {
	uint64_t in_testid;
	int in_part;
	int in_msgnum;
	char buf[128];

        if (rkmessage->len != 0)
                TEST_FAIL("Incoming message not NULL: %i bytes",
                          (int)rkmessage->len);

	if (rkmessage->key_len +1 >= sizeof(buf))
		TEST_FAIL("Incoming message key too large (%i): "
			  "not sourced by this test",
			  (int)rkmessage->key_len);

	rd_snprintf(buf, sizeof(buf), "%.*s",
		 (int)rkmessage->key_len, (char *)rkmessage->key);

	if (sscanf(buf, "testid=%"SCNu64", partition=%i, msg=%i",
		   &in_testid, &in_part, &in_msgnum) != 3)
		TEST_FAIL("Incorrect key format: %s", buf);

	if (testid != in_testid ||
	    (partition != -1 && partition != in_part) ||
	    (msgnum != -1 && msgnum != in_msgnum) ||
	    (in_msgnum < 0 || in_msgnum > cons_msgs_size))
		goto fail_match;

	if (test_level > 2) {
		TEST_SAY("%s:%i: Our testid %"PRIu64", part %i (%i), "
			 "msg %i/%i did "
			 ", key's: \"%s\"\n",
			 func, line,
			 testid, (int)partition, (int)rkmessage->partition,
			 msgnum, cons_msgs_size, buf);
	}

	if (cons_msgs_cnt == cons_msgs_size) {
		TEST_SAY("Too many messages in cons_msgs (%i) while reading "
			 "message key \"%s\"\n",
			 cons_msgs_cnt, buf);
		verify_consumed_msg_check();
		TEST_FAIL("See above error(s)");
	}

	cons_msgs[cons_msgs_cnt++] = in_msgnum;

	return;

 fail_match:
	TEST_FAIL("%s:%i: Our testid %"PRIu64", part %i, msg %i/%i did "
		  "not match message's key: \"%s\"\n",
		  func, line,
		  testid, (int)partition, msgnum, cons_msgs_size, buf);
}
/**
 * Produce to two partitions.
 * Consume with standard interface from both, one after the other.
 * Consume with queue interface from both, simultanously.
 */
static void test_produce_consume (void) {
	const char *topic = "rdkafkatest0012";
	int msgcnt = 10000;
	int partition_cnt = 2;
	int i;
	uint64_t testid;
	int msg_base = 0;

	/* Generate a testid so we can differentiate messages
	 * from other tests */
	testid = test_id_generate();

	TEST_SAY("Topic %s, testid %"PRIu64"\n", topic, testid);

	/* Produce messages */
	produce_messages(testid, topic, partition_cnt, msgcnt);


	/* Consume messages with standard interface */
	verify_consumed_msg_reset(msgcnt);
	for (i = 0 ; i < partition_cnt ; i++) {
		consume_messages(testid, topic, i,
				 msg_base, msgcnt / partition_cnt, msgcnt);
		msg_base += msgcnt / partition_cnt;
	}
	verify_consumed_msg_check();

	/* Consume messages with queue interface */
	verify_consumed_msg_reset(msgcnt);
	consume_messages_with_queues(testid, topic, partition_cnt, msgcnt);
	verify_consumed_msg_check();

	/* Wait for everything to be cleaned up since broker destroys are
	 * handled in its own thread. */
	test_wait_exit(10);

	/* If we havent failed at this point then
	 * there were no threads leaked */
	return;
}
/**
 * Produce to two partitions.
 * Consume with standard interface from both, one after the other.
 * Consume with queue interface from both, simultanously.
 */
static void test_produce_consume (void) {
	int msgcnt = 1000;
	int partition_cnt = 2;
	int i;
	uint64_t testid;
	int msg_base = 0;
        const char *topic;

	/* Generate a testid so we can differentiate messages
	 * from other tests */
	testid = test_id_generate();

        /* Read test.conf to configure topic name */
        test_conf_init(NULL, NULL, 20);
        topic = test_mk_topic_name("0012", 1);

	TEST_SAY("Topic %s, testid %"PRIu64"\n", topic, testid);

	/* Produce messages */
	produce_messages(testid, topic, partition_cnt, msgcnt);


	/* Consume messages with standard interface */
	verify_consumed_msg_reset(msgcnt);
	for (i = 0 ; i < partition_cnt ; i++) {
		consume_messages(testid, topic, i,
				 msg_base, msgcnt / partition_cnt, msgcnt);
		msg_base += msgcnt / partition_cnt;
	}
	verify_consumed_msg_check();

	/* Consume messages with queue interface */
	verify_consumed_msg_reset(msgcnt);
	consume_messages_with_queues(testid, topic, partition_cnt, msgcnt);
	verify_consumed_msg_check();

	return;
}