diff --git a/infra/mq/mq.c b/infra/mq/mq.c index 13a388b..a0e87f6 100644 --- a/infra/mq/mq.c +++ b/infra/mq/mq.c @@ -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; diff --git a/infra/mq/test/gtest_mq_main.cpp b/infra/mq/test/gtest_mq_main.cpp index dbe0f25..a53f618 100644 --- a/infra/mq/test/gtest_mq_main.cpp +++ b/infra/mq/test/gtest_mq_main.cpp @@ -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