BewareMyPower commented on code in PR #230: URL: https://github.com/apache/pulsar-client-cpp/pull/230#discussion_r1165396338
########## include/pulsar/c/messages.h: ########## @@ -0,0 +1,62 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include <pulsar/defines.h> Review Comment: ```suggestion #include <pulsar/defines.h> #include "message.h" ``` Include `message.h` to have `pulsar_message_t`'s definition.  ########## tests/c/c_ConsumerTest.cc: ########## @@ -0,0 +1,78 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#include <gtest/gtest.h> +#include <pulsar/c/client.h> +const char *lookup_url = "pulsar://localhost:6650"; + +TEST(C_ConsumerConfigurationTest, testCBatchReceive) { + const char *topic_name = "persistent://public/default/test-c-batch-receive5"; + const char *sub_name = "my-sub-name"; + + pulsar_client_configuration_t *conf = pulsar_client_configuration_create(); + pulsar_client_t *client = pulsar_client_create(lookup_url, conf); + + pulsar_producer_configuration_t *producer_conf = pulsar_producer_configuration_create(); + pulsar_producer_t *producer; + pulsar_result result = pulsar_client_create_producer(client, topic_name, producer_conf, &producer); + ASSERT_EQ(pulsar_result_Ok, result); + + pulsar_consumer_configuration_t *consumer_conf = pulsar_consumer_configuration_create(); + const int batch_receive_max_size = 10; + + pulsar_consumer_batch_receive_policy_t batch_receive_policy{batch_receive_max_size, -1, -1}; + pulsar_consumer_configuration_set_batch_receive_policy(consumer_conf, &batch_receive_policy); + + pulsar_consumer_t *consumer; + result = pulsar_client_subscribe(client, topic_name, sub_name, consumer_conf, &consumer); + ASSERT_EQ(pulsar_result_Ok, result); + + // Send messages + const char *data = "my-content"; + for (int i = 0; i < batch_receive_max_size; i++) { + pulsar_message_t *message = pulsar_message_create(); + pulsar_message_set_content(message, data, strlen(data)); + pulsar_result res = pulsar_producer_send(producer, message); + ASSERT_EQ(pulsar_result_Ok, res); + pulsar_message_free(message); + } + + // Batch receive messages + pulsar_messages_t *msgs = pulsar_messages_create(); + pulsar_result res = pulsar_consumer_batch_receive(consumer, &msgs); Review Comment: We don't need the `pulsar_messages_create` function for users. Unlike `pulsar_message_t`, which should be created by users when sending messages, `pulsar_messages_t` should only be created by `pulsar_consumer_batch_receive`. ```suggestion pulsar_messages_t *msgs = NULL; pulsar_result res = pulsar_consumer_batch_receive(consumer, &msgs); ASSERT_TRUE(msgs != NULL); ``` When `res` is `pulsar_result_OK`, it's guaranteed that `msgs` points to an object allocated by the library and should be deallocated by `pulsar_messages_free`. With the current implementation, there is a memory leak that the instance created by `pulsar_messages_create` won't be released. ```bash $ valgrind --leak-check=full ./tests/pulsar-tests --gtest_filter='C_ConsumerConfigurationTest.*' ... ==5249== LEAK SUMMARY: ==5249== definitely lost: 360 bytes in 12 blocks ==5249== indirectly lost: 12,336 bytes in 87 blocks ==5249== possibly lost: 0 bytes in 0 blocks ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
