Skip to content

Commit 0cb713d

Browse files
committed
fix(tests): resolve memory leak in bh_queue_test_suite and improve message handling
1 parent 9ec6237 commit 0cb713d

File tree

1 file changed

+54
-13
lines changed

1 file changed

+54
-13
lines changed

tests/unit/shared-utils/bh_queue_test.cc

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
#include "bh_platform.h"
1010

11+
// FIXME: Resolve memory leak in bh_queue_test_suite.
12+
// It includes release created queue and messages.
13+
1114
class bh_queue_test_suite : public testing::Test
1215
{
1316
protected:
@@ -87,6 +90,14 @@ enum {
8790
// If RES_CMP == 1, the function bh_queue_enter_loop_run run error.
8891
int RES_CMP = 0;
8992

93+
/* Don't touch .ro data in msg body */
94+
static void
95+
local_ro_msg_body_cleaner(void *body)
96+
{
97+
(void)body;
98+
return;
99+
}
100+
90101
TEST_F(bh_queue_test_suite, bh_queue_create)
91102
{
92103
EXPECT_NE(nullptr, bh_queue_create());
@@ -111,33 +122,44 @@ TEST_F(bh_queue_test_suite, bh_message_payload)
111122
{
112123
bh_message_t msg_ptr;
113124

114-
msg_ptr = bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
115-
sizeof("test_msg_body"), nullptr);
125+
msg_ptr =
126+
bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
127+
sizeof("test_msg_body"), (void *)local_ro_msg_body_cleaner);
116128
EXPECT_EQ("test_msg_body", bh_message_payload(msg_ptr));
129+
130+
bh_free_msg(msg_ptr);
117131
}
118132

119133
TEST_F(bh_queue_test_suite, bh_message_payload_len)
120134
{
121135
bh_message_t msg_ptr;
122136

123-
msg_ptr = bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
124-
sizeof("test_msg_body"), nullptr);
137+
msg_ptr =
138+
bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
139+
sizeof("test_msg_body"), (void *)local_ro_msg_body_cleaner);
125140
EXPECT_EQ(sizeof("test_msg_body"), bh_message_payload_len(msg_ptr));
141+
bh_free_msg(msg_ptr);
126142
}
127143

128144
TEST_F(bh_queue_test_suite, bh_message_type)
129145
{
130146
bh_message_t msg_ptr;
131147

132-
msg_ptr = bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
133-
sizeof("test_msg_body"), nullptr);
148+
msg_ptr =
149+
bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
150+
sizeof("test_msg_body"), (void *)local_ro_msg_body_cleaner);
134151
EXPECT_EQ(RESTFUL_REQUEST, bh_message_type(msg_ptr));
152+
bh_free_msg(msg_ptr);
135153
}
136154

137155
TEST_F(bh_queue_test_suite, bh_new_msg)
138156
{
139-
EXPECT_NE(nullptr, bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
140-
sizeof("test_msg_body"), nullptr));
157+
bh_message_t msg_ptr =
158+
bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
159+
sizeof("test_msg_body"), (void *)local_ro_msg_body_cleaner);
160+
161+
EXPECT_NE(nullptr, msg_ptr);
162+
bh_free_msg(msg_ptr);
141163
}
142164

143165
void
@@ -215,26 +237,45 @@ TEST_F(bh_queue_test_suite, bh_queue_get_message_count)
215237
bh_message_t msg_ptr;
216238
bh_queue *queue_ptr = bh_queue_create();
217239

218-
// Normally.
219-
msg_ptr = bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
220-
sizeof("test_msg_body"), nullptr);
221240
for (i = 1; i <= 20; i++) {
222-
bh_post_msg2(queue_ptr, msg_ptr);
241+
msg_ptr = bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
242+
sizeof("test_msg_body"),
243+
(void *)local_ro_msg_body_cleaner);
244+
EXPECT_NE(nullptr, msg_ptr);
245+
246+
bool post_result = bh_post_msg2(queue_ptr, msg_ptr);
247+
EXPECT_EQ(true, post_result);
223248
}
224249
i = i - 1;
225250
// The count of msg is less than queue_ptr->max.
226251
EXPECT_EQ(i, bh_queue_get_message_count(queue_ptr));
227252

228253
// The count of msg is more than queue_ptr->max.
229254
for (j = 1; j <= 60; j++) {
230-
bh_post_msg2(queue_ptr, msg_ptr);
255+
msg_ptr = bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
256+
sizeof("test_msg_body"),
257+
(void *)local_ro_msg_body_cleaner);
258+
EXPECT_NE(nullptr, msg_ptr);
259+
260+
bool post_result = bh_post_msg2(queue_ptr, msg_ptr);
261+
262+
// The first 30 messages should be posted successfully, and the rest
263+
// should be dropped.
264+
if (j <= 30) {
265+
EXPECT_EQ(true, post_result);
266+
}
267+
else {
268+
EXPECT_EQ(false, post_result);
269+
}
231270
}
232271
j = j - 1;
233272
EXPECT_EQ(queue_ptr->max, bh_queue_get_message_count(queue_ptr));
234273
EXPECT_EQ(j + i - queue_ptr->max, queue_ptr->drops);
235274

236275
// Illegal parameters.
237276
EXPECT_EQ(0, bh_queue_get_message_count(nullptr));
277+
278+
bh_queue_destroy(queue_ptr);
238279
}
239280

240281
void

0 commit comments

Comments
 (0)