🧪 test(mq): add mq clean test case

This commit is contained in:
yangwei
2024-09-19 16:36:12 +08:00
parent 87944eb115
commit d668cc3bbd
2 changed files with 62 additions and 3 deletions

View File

@@ -129,6 +129,8 @@ static void mq_dispatch_one_message(struct mq_schema *s, struct mq_message *mq_e
void mq_runtime_clean(struct mq_runtime *rt)
{
if(rt==NULL)return;
struct mq_message *mq_elt, *tmp;
struct mq_topic *topic;
rt->publish_enabled=false;
@@ -142,7 +144,7 @@ void mq_runtime_clean(struct mq_runtime *rt)
{
topic->free_cb(mq_elt->body, topic->free_cb_arg);
}
DL_DELETE(rt->priority_mq[STELLAR_MQ_DEATH_LETTER], mq_elt);
DL_DELETE(rt->priority_mq[i], mq_elt);
FREE(mq_elt);
}
}
@@ -200,8 +202,8 @@ int mq_schema_subscribe(struct mq_schema *s, int topic_id, on_msg_cb_func *on_ms
int mq_runtime_publish_message_with_priority(struct mq_runtime *rt, int topic_id, void *data, enum mq_property priority)
{
if(rt==NULL || rt->schema == NULL || rt->schema->topic_array == NULL)return -1;
if(rt->publish_enabled==false)return -1;
if(priority < STELLAR_MQ_PRIORITY_LOW || priority > STELLAR_MQ_PRIORITY_HIGH)return -1;
unsigned int len = utarray_len(rt->schema->topic_array);
if (len <= (unsigned int)topic_id)return -1;

View File

@@ -135,6 +135,63 @@ TEST(mq_runtime, new_and_free) {
}
struct test_pub_and_clean_env
{
struct mq_schema *s;
struct mq_runtime *rt;
int N_round;
int current_round;
int topic_id;
int on_msg_free_called;
int on_msg_called;
};
void test_pub_and_clean_free(void *msg, void *msg_free_arg)
{
struct test_pub_and_clean_env *env = (struct test_pub_and_clean_env *)msg_free_arg;
env->on_msg_free_called+=1;
FREE(msg);
return;
}
void test_pub_and_clean_on_msg(int topic_id, void *msg, void *sub_arg)
{
struct test_pub_and_clean_env *env = (struct test_pub_and_clean_env *)sub_arg;
env->on_msg_called+=1;
return;
}
TEST(mq_runtime, pub_then_clean) {
struct test_pub_and_clean_env env={};
env.s = mq_schema_new();
EXPECT_TRUE(env.s!=NULL);
env.topic_id=mq_schema_create_topic(env.s,"TEST", NULL, NULL, test_pub_and_clean_free , &env);
EXPECT_EQ(mq_schema_subscribe(env.s, env.topic_id, test_pub_and_clean_on_msg, &env), 0);
env.N_round=10;
env.rt=mq_runtime_new(env.s);
EXPECT_TRUE(env.rt!=NULL);
for(int i=0; i<env.N_round;i++)
{
env.current_round=i;
void *msg = CALLOC(int, 1);
EXPECT_TRUE(msg!=NULL);
*(int *)msg=i;
EXPECT_EQ(mq_runtime_publish_message(env.rt, env.topic_id, msg), 0);
mq_runtime_clean(env.rt);
}
mq_runtime_free(env.rt);
mq_schema_free(env.s);
EXPECT_EQ(env.on_msg_free_called, env.N_round);
EXPECT_EQ(env.on_msg_called, 0);
}
#define PACKET_EXDATA_NUM 2
#define PACKET_TOPIC_NUM 2
@@ -362,7 +419,7 @@ static void test_dispatch_on_msg(int topic_id, void *msg, void *sub_arg)
return;
}
TEST(mq_runtime, call_dispatch_on_msg_dispatch)
TEST(mq_runtime, call_dispatch_when_dispatch)
{
struct test_dispatch_on_msg_env env={};
env.s=mq_schema_new();