diff options
| author | yangwei <[email protected]> | 2024-09-29 14:18:20 +0800 |
|---|---|---|
| committer | yangwei <[email protected]> | 2024-09-29 14:18:20 +0800 |
| commit | 2ea8d96c5cd4a12a0b1f3cfb5a0fdc4bdaf40ec5 (patch) | |
| tree | 294d7faf4344b4fe0ffca8d8b9ebb1d3dc050198 | |
| parent | dc4805fbb880714331876b6c3795037ddd95a99e (diff) | |
✨ feat(module manager internal API): remove new_with_file
| -rw-r--r-- | infra/module_manager/module_manager.c | 50 | ||||
| -rw-r--r-- | infra/module_manager/module_manager_interna.h | 2 | ||||
| -rw-r--r-- | infra/module_manager/test/gtest_module_manager_main.cpp | 32 | ||||
| -rw-r--r-- | infra/polling_manager/test/gtest_polling_manager_main.cpp | 12 |
4 files changed, 45 insertions, 51 deletions
diff --git a/infra/module_manager/module_manager.c b/infra/module_manager/module_manager.c index ed0d2fe..154fa6c 100644 --- a/infra/module_manager/module_manager.c +++ b/infra/module_manager/module_manager.c @@ -9,44 +9,33 @@ /******************************************* - * module manager internal API * + * module manager API * *******************************************/ #include "toml/toml.h" #include <fcntl.h> #include <unistd.h> -static int stellar_module_manager_get_filepath_from_fp(FILE *fp, char *path, size_t n) -{ - if(fp==NULL)return -1; - int fd = fileno(fp); - char buf[256]; - snprintf(buf, sizeof(buf), "/proc/self/fd/%d", fd); - return readlink(buf, path, n - 1); -} - -struct stellar_module_manager *stellar_module_manager_new_with_file(FILE *fp, int max_thread_num, struct mq_schema *mq_schema) +struct stellar_module_manager *stellar_module_manager_new(const char *module_spec_toml_path, int max_thread_num, struct mq_schema *mq_schema) { struct stellar_module_manager *mod_mgr = CALLOC(struct stellar_module_manager, 1); mod_mgr->schema.max_thread_num=max_thread_num; mod_mgr->schema.mq_schema=mq_schema; - if(fp==NULL)return mod_mgr; + if(module_spec_toml_path==NULL)return mod_mgr; + FILE *fp = fopen(module_spec_toml_path, "r"); + if (fp == NULL)return mod_mgr; + mod_mgr->module_spec_toml_path = strdup(module_spec_toml_path); - char pathname[FILENAME_MAX] = {}; - if(stellar_module_manager_get_filepath_from_fp(fp, pathname, sizeof(pathname)) > 0) - { - mod_mgr->module_spec_toml_path = strdup(pathname); - } - int mod_num = 0; + int mod_num = 0; toml_table_t *conf = toml_parse_file(fp, NULL, 0); toml_array_t* mod_array = toml_array_in(conf, "module"); char *path = NULL; char *init_func_name = NULL; char *exit_func_name = NULL; - if(mod_array==NULL)goto MODULE_SPEC_LOAD_ERROR; + if(mod_array==NULL)goto MODULE_SPEC_LOAD_END; mod_num = toml_array_nelem(mod_array); mod_mgr->module_specs = CALLOC(struct module_spec_load, mod_num); @@ -61,12 +50,12 @@ struct stellar_module_manager *stellar_module_manager_new_with_file(FILE *fp, in if (toml_rtos(path_raw, &path) || toml_rtos(init_func_name_raw, &init_func_name) || toml_rtos(exit_func_name_raw, &exit_func_name)) { - goto MODULE_SPEC_LOAD_ERROR; + goto MODULE_SPEC_LOAD_END; } void* handle = dlopen(path, RTLD_NOW|RTLD_LAZY|RTLD_GLOBAL); if (!handle) { fprintf(stderr, "Error loading module %s: %s\n", path, dlerror()); - goto MODULE_SPEC_LOAD_ERROR; + goto MODULE_SPEC_LOAD_END; } mod_mgr->module_specs[i].on_init_cb = (module_on_init_func *) dlsym(handle, init_func_name); @@ -82,7 +71,6 @@ struct stellar_module_manager *stellar_module_manager_new_with_file(FILE *fp, in fprintf(stderr, "Module %s already exists\n", mod_mgr->module_specs[i].mod->name); if (mod_mgr->module_specs[i].on_exit_cb) mod_mgr->module_specs[i].on_exit_cb(mod_mgr, mod_mgr->module_specs[i].mod); - assert(0); } } else @@ -97,27 +85,19 @@ struct stellar_module_manager *stellar_module_manager_new_with_file(FILE *fp, in mod_mgr->module_specs[i].path=path; mod_mgr->module_specs[i].init_cb_name=init_func_name; mod_mgr->module_specs[i].exit_cb_name=exit_func_name; + mod_mgr->load_module_num+=1; } - mod_mgr->load_module_num=mod_num; - -MODULE_SPEC_LOAD_ERROR: - if(conf )toml_free(conf); - return mod_mgr; -} +MODULE_SPEC_LOAD_END: -/******************************************* - * stellar module manager API * - *******************************************/ + assert(mod_mgr->load_module_num==mod_num); -struct stellar_module_manager *stellar_module_manager_new(const char *module_spec_toml_path, int max_thread_num, struct mq_schema *mq_schema) -{ - FILE *fp=fopen(module_spec_toml_path, "r"); - struct stellar_module_manager *mod_mgr=stellar_module_manager_new_with_file(fp, max_thread_num, mq_schema); + if(conf )toml_free(conf); if(fp)fclose(fp); return mod_mgr; } + void stellar_module_manager_free(struct stellar_module_manager *mod_mgr) { if(mod_mgr==NULL)return; diff --git a/infra/module_manager/module_manager_interna.h b/infra/module_manager/module_manager_interna.h index 7b07552..0938ad2 100644 --- a/infra/module_manager/module_manager_interna.h +++ b/infra/module_manager/module_manager_interna.h @@ -46,8 +46,6 @@ struct stellar_module_manager }__attribute__((aligned(sizeof(void*)))); -struct stellar_module_manager *stellar_module_manager_new_with_file(FILE *fp, int max_thread_num, struct mq_schema *mq_schema) __attribute__((visibility("hidden"))); - #ifdef __cplusplus } #endif
\ No newline at end of file diff --git a/infra/module_manager/test/gtest_module_manager_main.cpp b/infra/module_manager/test/gtest_module_manager_main.cpp index 9ee7bef..40a2f21 100644 --- a/infra/module_manager/test/gtest_module_manager_main.cpp +++ b/infra/module_manager/test/gtest_module_manager_main.cpp @@ -5,6 +5,10 @@ #include "module_manager/module_manager_interna.h" +#include <unistd.h> +#include <stdlib.h> + + /*********************************** * TEST MODUEL MANAGER INTERNAL API * ***********************************/ @@ -20,22 +24,27 @@ const char *gtest_mock_spec_toml = TEST(module_manager_internal, stellar_module_manager_new_with_toml) { struct mq_schema *mq_schema=NULL; - FILE *fp = fmemopen((void *)gtest_mock_spec_toml, strlen(gtest_mock_spec_toml), "r"); - EXPECT_TRUE(fp!=NULL); - struct stellar_module_manager *mod_mgr=stellar_module_manager_new_with_file(fp, 10, mq_schema); - fclose(fp); + 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); 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_STREQ(stellar_module_manager_get_toml_path(mod_mgr), toml_template); 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); + unlink(toml_template); } /*********************************** @@ -148,18 +157,21 @@ TEST(module_manager, basic_module) { struct mq_schema *mq_schema=(struct mq_schema *)1; - FILE *fp = fmemopen((void *)gtest_module_spec_toml, strlen(gtest_module_spec_toml), "r"); - EXPECT_TRUE(fp!=NULL); + char toml_template[] = "./stellar.toml.XXXXXX"; + int fd = mkstemp(toml_template); + EXPECT_TRUE(fd>=0); + write(fd, gtest_module_spec_toml, strlen(gtest_module_spec_toml)); + close(fd); - struct stellar_module_manager *mod_mgr=stellar_module_manager_new_with_file(fp, 10, mq_schema); - fclose(fp); + struct stellar_module_manager *mod_mgr=stellar_module_manager_new(toml_template, 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); - + EXPECT_STREQ(stellar_module_manager_get_toml_path(mod_mgr), toml_template); + struct mq_runtime *mq_rt = (struct mq_runtime*)2; stellar_module_manager_register_thread(mod_mgr, 1, mq_rt); @@ -167,7 +179,7 @@ TEST(module_manager, basic_module) { EXPECT_EQ((long)stellar_module_manager_get_mq_runtime(mod_mgr), 2); stellar_module_manager_free(mod_mgr); - + unlink(toml_template); } /********************************************** diff --git a/infra/polling_manager/test/gtest_polling_manager_main.cpp b/infra/polling_manager/test/gtest_polling_manager_main.cpp index 148f0f4..c212934 100644 --- a/infra/polling_manager/test/gtest_polling_manager_main.cpp +++ b/infra/polling_manager/test/gtest_polling_manager_main.cpp @@ -38,11 +38,13 @@ TEST(polling_manager, basic_polling_module) { struct mq_schema *mq_schema=mq_schema_new(); - FILE *fp = fmemopen((void *)gtest_mock_spec_toml, strlen(gtest_mock_spec_toml), "r"); - EXPECT_TRUE(fp!=NULL); + 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_with_file(fp, 10, mq_schema); - fclose(fp); + struct stellar_module_manager *mod_mgr=stellar_module_manager_new(toml_template, 10, mq_schema); EXPECT_TRUE(mod_mgr!=NULL); struct stellar_polling_manager *polling_mgr=stellar_module_get_polling_manager(mod_mgr); @@ -71,6 +73,8 @@ TEST(polling_manager, basic_polling_module) { EXPECT_EQ(env.polling_count, env.N_round+env.polling_active_count); + unlink(toml_template); + } /********************************************** |
