90 lines
2.3 KiB
C++
90 lines
2.3 KiB
C++
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
#include "polling_manager/polling_manager_internal.h"
|
|
#include "module_manager/module_manager_interna.h"
|
|
|
|
/***********************************
|
|
* TEST POLLING MANAGER POLLING API *
|
|
***********************************/
|
|
|
|
struct test_module_polling_env
|
|
{
|
|
int N_round;
|
|
int polling_count;
|
|
int polling_active_count;
|
|
};
|
|
|
|
void test_module_on_polling(struct stellar_polling_manager* mod_mgr, void *polling_arg)
|
|
{
|
|
struct test_module_polling_env *env = (struct test_module_polling_env*)polling_arg;
|
|
env->polling_count++;
|
|
if(env->polling_count%2==0)
|
|
{
|
|
stellar_polling_active(mod_mgr);
|
|
env->polling_active_count++;
|
|
}
|
|
}
|
|
|
|
const char *gtest_mock_spec_toml =
|
|
"[[module]]\n"
|
|
"path = \"\"\n"
|
|
"init = \"polling_manager_on_init\"\n"
|
|
"exit = \"polling_manager_on_exit\"\n";
|
|
|
|
TEST(polling_manager, basic_polling_module) {
|
|
|
|
struct mq_schema *mq_schema=mq_schema_new();
|
|
|
|
char toml_template[] = "./stellar.toml.XXXXXX";
|
|
int fd = mkstemp(toml_template);
|
|
EXPECT_TRUE(fd>=0);
|
|
write(fd, gtest_mock_spec_toml, strlen(gtest_mock_spec_toml));
|
|
close(fd);
|
|
|
|
struct stellar_module_manager *mod_mgr=stellar_module_manager_new(toml_template, 10, mq_schema, NULL);
|
|
EXPECT_TRUE(mod_mgr!=NULL);
|
|
|
|
struct stellar_polling_manager *polling_mgr=stellar_module_get_polling_manager(mod_mgr);
|
|
EXPECT_TRUE(polling_mgr!=NULL);
|
|
|
|
EXPECT_EQ(stellar_module_manager_get_max_thread_num(mod_mgr), 10);
|
|
EXPECT_EQ(stellar_module_manager_get_mq_schema(mod_mgr), mq_schema);
|
|
|
|
struct test_module_polling_env env={};
|
|
env.N_round=10;
|
|
|
|
stellar_polling_subscribe(polling_mgr, test_module_on_polling, &env);
|
|
|
|
struct mq_runtime *mq_rt = mq_runtime_new(mq_schema);
|
|
stellar_module_manager_register_thread(mod_mgr, 1, mq_rt);
|
|
|
|
EXPECT_EQ((long)stellar_module_manager_get_thread_id(mod_mgr), 1);
|
|
EXPECT_EQ(stellar_module_manager_get_mq_runtime(mod_mgr), mq_rt);
|
|
|
|
for(int i=0; i<env.N_round; i++)
|
|
{
|
|
stellar_polling_dispatch(polling_mgr);
|
|
}
|
|
|
|
stellar_module_manager_free(mod_mgr);
|
|
|
|
EXPECT_EQ(env.polling_count, env.N_round+env.polling_active_count);
|
|
|
|
unlink(toml_template);
|
|
|
|
}
|
|
|
|
/**********************************************
|
|
* GTEST MAIN *
|
|
**********************************************/
|
|
|
|
int main(int argc, char ** argv)
|
|
{
|
|
int ret=0;
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
ret=RUN_ALL_TESTS();
|
|
return ret;
|
|
} |