|
| 1 | +/* |
| 2 | + * librdkafka - Apache Kafka C library |
| 3 | + * |
| 4 | + * Copyright (c) 2025, Confluent Inc. |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + */ |
| 28 | + |
| 29 | +#include "test.h" |
| 30 | +typedef struct consumer_s { |
| 31 | + const char *group_id; |
| 32 | + char *memberid; |
| 33 | +} consumer_t; |
| 34 | + |
| 35 | +static int |
| 36 | +is_fatal_cb(rd_kafka_t *rk, rd_kafka_resp_err_t err, const char *reason) { |
| 37 | + if (err == RD_KAFKA_RESP_ERR__TIMED_OUT) |
| 38 | + return 0; |
| 39 | + return 1; |
| 40 | +} |
| 41 | + |
| 42 | +static int consumer_thread(void *arg) { |
| 43 | + rd_kafka_conf_t *conf; |
| 44 | + rd_kafka_t *consumer; |
| 45 | + consumer_t *consumer_args = arg; |
| 46 | + |
| 47 | + test_curr->is_fatal_cb = is_fatal_cb; |
| 48 | + |
| 49 | + test_conf_init(&conf, NULL, 60); |
| 50 | + |
| 51 | + consumer = |
| 52 | + test_create_consumer(consumer_args->group_id, NULL, conf, NULL); |
| 53 | + |
| 54 | + consumer_args->memberid = rd_kafka_memberid(consumer); |
| 55 | + |
| 56 | + test_consumer_close(consumer); |
| 57 | + rd_kafka_destroy(consumer); |
| 58 | + test_curr->is_fatal_cb = NULL; |
| 59 | + return 0; |
| 60 | +} |
| 61 | + |
| 62 | +void do_test_unique_memberid() { |
| 63 | + int i; |
| 64 | + int j; |
| 65 | + int have_only_unique_memberids = 1; |
| 66 | + const char *group_id = test_mk_topic_name("0153-memberid", 1); |
| 67 | + |
| 68 | +#define CONSUMER_CNT 500 |
| 69 | + thrd_t thread_id[CONSUMER_CNT]; |
| 70 | + consumer_t consumer_args[CONSUMER_CNT]; |
| 71 | + |
| 72 | + SUB_TEST_QUICK(); |
| 73 | + |
| 74 | + for (i = 0; i < CONSUMER_CNT; i++) { |
| 75 | + consumer_args[i].group_id = group_id; |
| 76 | + consumer_args[i].memberid = NULL; |
| 77 | + thrd_create(&thread_id[i], consumer_thread, &consumer_args[i]); |
| 78 | + } |
| 79 | + |
| 80 | + for (i = 0; i < CONSUMER_CNT; i++) { |
| 81 | + thrd_join(thread_id[i], NULL); |
| 82 | + } |
| 83 | + |
| 84 | + for (i = 0; i < CONSUMER_CNT; i++) { |
| 85 | + if (have_only_unique_memberids) { |
| 86 | + for (j = i + 1; j < CONSUMER_CNT; j++) { |
| 87 | + if (strcmp(consumer_args[i].memberid, |
| 88 | + consumer_args[j].memberid) == 0) { |
| 89 | + TEST_SAY( |
| 90 | + "Consumer %d has the same member " |
| 91 | + "ID as consumer %d: %s\n", |
| 92 | + i, j, consumer_args[i].memberid); |
| 93 | + have_only_unique_memberids = 0; |
| 94 | + break; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + rd_free(consumer_args[i].memberid); |
| 99 | + } |
| 100 | + |
| 101 | + if (have_only_unique_memberids) { |
| 102 | + TEST_SAY("All %d consumers have unique member IDs\n", |
| 103 | + CONSUMER_CNT); |
| 104 | + } else { |
| 105 | + TEST_FAIL("Not all consumers have unique member IDs\n"); |
| 106 | + } |
| 107 | + |
| 108 | + SUB_TEST_PASS(); |
| 109 | +} |
| 110 | + |
| 111 | +int main_0153_memberid(int argc, char **argv) { |
| 112 | + |
| 113 | + if (test_consumer_group_protocol_classic()) { |
| 114 | + TEST_SKIP( |
| 115 | + "Member ID is not generated on the client side in classic " |
| 116 | + "protocol, skipping test"); |
| 117 | + return 0; |
| 118 | + } |
| 119 | + |
| 120 | + if (!strcmp(test_mode, "valgrind")) { |
| 121 | + TEST_SKIP("Test is too heavy for valgrind, skipping it"); |
| 122 | + return 0; |
| 123 | + } |
| 124 | + |
| 125 | + do_test_unique_memberid(); |
| 126 | + |
| 127 | + return 0; |
| 128 | +} |
0 commit comments