213 lines
6.6 KiB
C++
213 lines
6.6 KiB
C++
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
#include "module_manager/module_manager_interna.h"
|
|
|
|
/***********************************
|
|
* TEST MODUEL MANAGER INTERNAL API *
|
|
***********************************/
|
|
TEST(module_manager_internal, toml_parse_filepath_null) {
|
|
toml_table_t *conf = toml_parse_file_path("");
|
|
EXPECT_TRUE(conf==NULL);
|
|
}
|
|
|
|
TEST(module_manager_internal, toml_parse_filepath_dev_null) {
|
|
toml_table_t *conf = toml_parse_file_path("/dev/null");
|
|
EXPECT_TRUE(conf!=NULL);
|
|
toml_free(conf);
|
|
}
|
|
|
|
extern "C" struct stellar_module *gtest_mock_init(struct stellar_module_manager *mod_mgr){return NULL;}
|
|
extern "C" void gtest_mock_exit(struct stellar_module_manager *mod_mgr, struct stellar_module *mod){}
|
|
const char *gtest_mock_spec_toml =
|
|
"[[module]]\n"
|
|
"path = \"\"\n"
|
|
"init = \"gtest_mock_init\"\n"
|
|
"exit = \"gtest_mock_exit\"\n";
|
|
|
|
TEST(module_manager_internal, module_specs_load) {
|
|
|
|
toml_table_t *conf = toml_parse((char*)gtest_mock_spec_toml, NULL, 0);
|
|
EXPECT_TRUE(conf!=NULL);
|
|
|
|
struct module_spec_load *specs=NULL;
|
|
int mod_num=module_specs_load(conf, &specs);
|
|
EXPECT_EQ(mod_num, 1);
|
|
|
|
EXPECT_EQ(specs[0].on_init_cb, gtest_mock_init);
|
|
EXPECT_EQ(specs[0].on_exit_cb, gtest_mock_exit);
|
|
|
|
|
|
if(specs->path)free(specs->path);
|
|
if(specs->init_cb_name)free(specs->init_cb_name);
|
|
if(specs->exit_cb_name)free(specs->exit_cb_name);
|
|
free(specs);
|
|
toml_free(conf);
|
|
|
|
}
|
|
|
|
TEST(module_manager_internal, stellar_module_manager_new_with_toml) {
|
|
|
|
struct mq_schema *mq_schema=NULL;
|
|
toml_table_t *conf = toml_parse((char*)gtest_mock_spec_toml, NULL, 0);
|
|
EXPECT_TRUE(conf!=NULL);
|
|
|
|
struct stellar_module_manager *mod_mgr=stellar_module_manager_new_with_toml(conf, 10, mq_schema);
|
|
|
|
EXPECT_TRUE(mod_mgr!=NULL);
|
|
EXPECT_TRUE(stellar_module_manager_get_module(mod_mgr, "test")==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);
|
|
|
|
EXPECT_EQ(stellar_module_manager_get_thread_id(mod_mgr), -1);// no thread registered
|
|
EXPECT_TRUE(stellar_module_manager_get_mq_runtime(mod_mgr)==NULL);
|
|
|
|
stellar_module_manager_free(mod_mgr);
|
|
toml_free(conf);
|
|
|
|
}
|
|
|
|
/***********************************
|
|
* TEST STELLAR MODULE API *
|
|
***********************************/
|
|
|
|
TEST(stellar_module, basic_new_and_free) {
|
|
|
|
struct stellar_module *mod = stellar_module_new("test", NULL);
|
|
EXPECT_TRUE(mod!=NULL);
|
|
|
|
stellar_module_set_name(mod, "test1");
|
|
EXPECT_STREQ(stellar_module_get_name(mod), "test1");
|
|
|
|
stellar_module_set_ctx(mod, (void*)1);
|
|
EXPECT_EQ((long)stellar_module_get_ctx(mod), 1);
|
|
|
|
stellar_module_free(mod);
|
|
}
|
|
|
|
/***********************************
|
|
* TEST MODULE MANAGER API *
|
|
***********************************/
|
|
|
|
TEST(stellar_module_manager, new_with_null_toml) {
|
|
|
|
struct mq_schema *mq_schema=NULL;
|
|
struct stellar_module_manager *mod_mgr = stellar_module_manager_new(NULL, 10, mq_schema);
|
|
EXPECT_TRUE(mod_mgr!=NULL);
|
|
EXPECT_TRUE(stellar_module_manager_get_module(mod_mgr, "test")==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);
|
|
|
|
EXPECT_TRUE(stellar_module_manager_get_mq_runtime(mod_mgr)==NULL);
|
|
EXPECT_EQ(stellar_module_manager_get_thread_id(mod_mgr), -1);// no thread registered
|
|
|
|
stellar_module_manager_free(mod_mgr);
|
|
}
|
|
|
|
TEST(stellar_module_manager, new_with_empty_toml) {
|
|
|
|
struct mq_schema *mq_schema=NULL;
|
|
struct stellar_module_manager *mod_mgr = stellar_module_manager_new("/dev/null", 10, mq_schema);
|
|
EXPECT_TRUE(mod_mgr!=NULL);
|
|
EXPECT_TRUE(stellar_module_manager_get_module(mod_mgr, "test")==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);
|
|
|
|
EXPECT_EQ(stellar_module_manager_get_thread_id(mod_mgr), -1);// no thread registered
|
|
EXPECT_TRUE(stellar_module_manager_get_mq_runtime(mod_mgr)==NULL);
|
|
|
|
stellar_module_manager_free(mod_mgr);
|
|
}
|
|
|
|
TEST(stellar_module_manager, register_thread) {
|
|
|
|
struct mq_schema *mq_schema=(struct mq_schema*)1;
|
|
struct stellar_module_manager *mod_mgr=stellar_module_manager_new(NULL, 10, mq_schema);
|
|
|
|
EXPECT_TRUE(mod_mgr!=NULL);
|
|
|
|
EXPECT_EQ((long)stellar_module_manager_get_mq_schema(mod_mgr), 1);
|
|
|
|
EXPECT_EQ(stellar_module_manager_get_thread_id(mod_mgr), -1);// no thread registered
|
|
EXPECT_TRUE(stellar_module_manager_get_mq_runtime(mod_mgr)==NULL);
|
|
|
|
struct mq_runtime *mq_rt = (struct mq_runtime*)2;
|
|
stellar_module_manager_register_thread(mod_mgr, 1, mq_rt);
|
|
|
|
EXPECT_EQ(stellar_module_manager_get_thread_id(mod_mgr), 1);
|
|
EXPECT_EQ((long)stellar_module_manager_get_mq_runtime(mod_mgr), 2);
|
|
|
|
stellar_module_manager_free(mod_mgr);
|
|
|
|
}
|
|
|
|
/***********************************
|
|
* TEST MODULE MANAGER API *
|
|
***********************************/
|
|
|
|
extern "C" struct stellar_module *gtest_module_init(struct stellar_module_manager *mod_mgr)
|
|
{
|
|
struct stellar_module *mod = stellar_module_new("gtest", NULL);
|
|
EXPECT_STREQ(stellar_module_get_name(mod), "gtest");
|
|
stellar_module_set_ctx(mod, (void*)1);
|
|
|
|
return mod;
|
|
}
|
|
|
|
extern "C" void gtest_module_exit(struct stellar_module_manager *mod_mgr, struct stellar_module *mod)
|
|
{
|
|
EXPECT_STREQ(stellar_module_get_name(mod), "gtest");
|
|
EXPECT_EQ((long)stellar_module_get_ctx(mod), 1);
|
|
|
|
EXPECT_EQ(stellar_module_manager_get_module(mod_mgr, "gtest"), mod);
|
|
|
|
EXPECT_EQ(stellar_module_manager_get_thread_id(mod_mgr), 1);
|
|
EXPECT_EQ((long)stellar_module_manager_get_mq_runtime(mod_mgr), 2);
|
|
|
|
stellar_module_free(mod);
|
|
}
|
|
|
|
const char *gtest_module_spec_toml =
|
|
"[[module]]\n"
|
|
"path = \"\"\n"
|
|
"init = \"gtest_module_init\"\n"
|
|
"exit = \"gtest_module_exit\"\n";
|
|
|
|
TEST(module_manager, basic_module) {
|
|
|
|
struct mq_schema *mq_schema=(struct mq_schema *)1;
|
|
toml_table_t *conf = toml_parse((char*)gtest_module_spec_toml, NULL, 0);
|
|
EXPECT_TRUE(conf!=NULL);
|
|
|
|
struct stellar_module_manager *mod_mgr=stellar_module_manager_new_with_toml(conf, 10, mq_schema);
|
|
EXPECT_TRUE(mod_mgr!=NULL);
|
|
|
|
EXPECT_TRUE(stellar_module_manager_get_module(mod_mgr, "gtest")!=NULL);
|
|
|
|
EXPECT_EQ(stellar_module_manager_get_max_thread_num(mod_mgr), 10);
|
|
EXPECT_EQ((long)stellar_module_manager_get_mq_schema(mod_mgr), 1);
|
|
|
|
struct mq_runtime *mq_rt = (struct mq_runtime*)2;
|
|
stellar_module_manager_register_thread(mod_mgr, 1, mq_rt);
|
|
|
|
EXPECT_EQ((long)stellar_module_manager_get_thread_id(mod_mgr), 1);
|
|
EXPECT_EQ((long)stellar_module_manager_get_mq_runtime(mod_mgr), 2);
|
|
|
|
stellar_module_manager_free(mod_mgr);
|
|
toml_free(conf);
|
|
|
|
}
|
|
|
|
/**********************************************
|
|
* GTEST MAIN *
|
|
**********************************************/
|
|
|
|
int main(int argc, char ** argv)
|
|
{
|
|
int ret=0;
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
ret=RUN_ALL_TESTS();
|
|
return ret;
|
|
} |