🧪 test(mq): add mock polling test case

This commit is contained in:
yangwei
2024-09-19 18:09:03 +08:00
parent 6d36a2fc9f
commit d9c3ac2448

View File

@@ -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 <env.N_round; i++)
{
env.current_round=i;
mq_runtime_publish_message(env.rt, env.polling_topic_id, NULL);
mq_runtime_dispatch(env.rt);
}
mq_runtime_free(env.rt);
mq_schema_free(env.s);
EXPECT_EQ(env.polling_dispatch_called, (env.N_round+1)*env.mod_num);
for(int i = 0; i < env.mod_num; i++)
{
EXPECT_EQ(env.mod[i].called, env.N_round+1);
}
}
#if 0
//TODO: test case mq for overlimit
static void overlimit_packet_msg_free_cb_func(void *msg, void *msg_free_arg)