From d9c3ac24489d68aae8b43148aec0b1e90fa0ea00 Mon Sep 17 00:00:00 2001 From: yangwei Date: Thu, 19 Sep 2024 18:09:03 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20test(mq):=20add=20mock=20polling?= =?UTF-8?q?=20test=20case?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- infra/mq/test/gtest_mq_main.cpp | 109 ++++++++++++++++++++++++++++++-- 1 file changed, 105 insertions(+), 4 deletions(-) diff --git a/infra/mq/test/gtest_mq_main.cpp b/infra/mq/test/gtest_mq_main.cpp index a53f618..3cca4cc 100644 --- a/infra/mq/test/gtest_mq_main.cpp +++ b/infra/mq/test/gtest_mq_main.cpp @@ -444,10 +444,6 @@ TEST(mq_runtime, call_dispatch_when_dispatch) EXPECT_EQ(env.on_msg_called, env.N_round*2); } -/********************************************** - * MQ RUNTIME WITH DISPATCH * - **********************************************/ - struct mock_session_message { int id; @@ -675,6 +671,111 @@ TEST(mq_runtime, basic_mq_priority) { EXPECT_EQ(env.plugin_id_2_called,env.N_round*3); } + +struct test_polling_module +{ + int mod_id; + int called; +}; + +struct test_mock_polling_env +{ + struct mq_schema *s; + struct mq_runtime *rt; + int N_round; + int current_round; + int polling_topic_id; + int polling_dispatch_called; + int mod_num; + struct test_polling_module mod[1024]; +}; + +#define TOPIC_POLLING "POLLING" + +typedef void mock_on_polling_cb_func(void *polling_arg); + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-function-type" + +static void mock_on_polling_dispatch(int topic_id, + void *msg, + on_msg_cb_func* on_msg_cb, + void *on_msg_cb_arg, + void *dispatch_arg) +{ + mock_on_polling_cb_func *polling = (mock_on_polling_cb_func *)on_msg_cb; + polling(on_msg_cb_arg); + struct test_mock_polling_env *env=(struct test_mock_polling_env *)dispatch_arg; + env->polling_dispatch_called+=1; +} + +static int mock_polling_subscribe(struct test_mock_polling_env *env, mock_on_polling_cb_func *on_polling, void *polling_arg) +{ + int topic_id=mq_schema_get_topic_id(env->s, TOPIC_POLLING); + if(topic_id<0) + { + topic_id=mq_schema_create_topic(env->s, TOPIC_POLLING, mock_on_polling_dispatch, env, NULL, NULL); + } + return mq_schema_subscribe(env->s, topic_id, (on_msg_cb_func *)on_polling, polling_arg); +} + +#pragma GCC diagnostic pop + +static int mock_polling_work(struct test_mock_polling_env *env) +{ + mq_runtime_publish_message(env->rt, env->polling_topic_id, NULL); + return 0; +} + + + +static void mock_on_polling(void *polling_arg) +{ + struct test_polling_module *mod = (struct test_polling_module *)polling_arg; + mod->called+=1; + if(mod->mod_id==0 && mod->called==2) + { + struct test_mock_polling_env *env=container_of((const test_polling_module (*)[1024])polling_arg, struct test_mock_polling_env, mod); + mock_polling_work(env); + } + return; +} + +TEST(mq_runtime, polling) +{ + struct test_mock_polling_env env; + memset(&env, 0, sizeof(env)); + env.s=mq_schema_new(); + env.mod_num=10; + + for(int i=0; i < env.mod_num;i++) + { + env.mod[i].mod_id=i; + EXPECT_EQ(mock_polling_subscribe(&env, mock_on_polling, &env.mod[i]), 0); + } + + env.polling_topic_id=mq_schema_get_topic_id(env.s, TOPIC_POLLING); + env.rt=mq_runtime_new(env.s); + + env.N_round=10; + for(int i=0; i