BewareMyPower commented on code in PR #230:
URL: https://github.com/apache/pulsar-client-cpp/pull/230#discussion_r1167064674


##########
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:
   > You seem to be executing C_ConsumerConfigurationTest,
   
   Then I find another problem. Your test class in `tests/c/c_ConsumerTest.cc` 
is `C_ConsumerConfigurationTest`, not `C_ConsumerTest`.
   
   ```c++
   TEST(C_ConsumerConfigurationTest, testCBatchReceive) {
   ```
   
   It's better to make class name similar with the file name. BTW, I ran the 
valgrind check for this single test and the memory leak still exists.
   
   ```bash
   $ valgrind --leak-check=full ./tests/pulsar-tests 
--gtest_filter='*.testCBatchReceive'
   ==24889== LEAK SUMMARY:
   ==24889==    definitely lost: 344 bytes in 11 blocks
   ==24889==    indirectly lost: 11,568 bytes in 82 blocks
   ==24889==      possibly lost: 0 bytes in 0 blocks
   ```
   
   After I changed the initial value of `message` to `NULL`, there were still 
memory leak:
   
   ```
   ==25180==    by 0x5A3537: 
C_ConsumerConfigurationTest_testCBatchReceive_Test::TestBody() 
(c_ConsumerTest.cc:61)
   ...
   ==25180==    definitely lost: 320 bytes in 10 blocks
   ==25180==    indirectly lost: 11,568 bytes in 82 blocks
   ==25180==      possibly lost: 0 bytes in 0 blocks
   ```
   
   ```c++
           pulsar_message_t *msg = pulsar_messages_get(msgs, i);
   ```
   
   So there is something wrong with the `pulsar_messages_get` function.



-- 
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]

Reply via email to