🧪 test(module manager): add test case

This commit is contained in:
yangwei
2024-09-14 16:00:24 +08:00
parent bf65690cdf
commit 5150a03512
6 changed files with 256 additions and 72 deletions

View File

@@ -2,25 +2,32 @@
#include "stellar/module_manager.h"
#include "stellar/utils.h"
#include "toml/toml.h"
#include <dlfcn.h>
#include <stdbool.h>
UT_icd module_specs_icd = {sizeof(struct module_specific), NULL, NULL, NULL};
static struct module_specific *module_specs_load(const char *toml_conf_path, int *mod_num)
/*******************************************
* module manager internal API *
*******************************************/
toml_table_t *toml_parse_file_path(const char *toml_conf_path)
{
*mod_num = 0;
if(toml_conf_path==NULL)return NULL;
FILE* fp = fopen(toml_conf_path, "r");
if(fp==NULL)return NULL;
char errbuf[256];
toml_table_t* conf = toml_parse_file(fp, errbuf, sizeof(errbuf));
fclose(fp);
if (!conf) {
fprintf(stderr, "Error parsing toml: %s\n", errbuf);
return NULL;
}
if(conf==NULL) fprintf(stderr, "Error parsing toml: %s\n", errbuf);
return conf;
}
struct module_specific *module_specs_load(toml_table_t* conf, int *mod_num)
{
if(conf==NULL|| mod_num==NULL) return NULL;
*mod_num = 0;
struct module_specific* mod_spec=NULL;
toml_array_t* mod_array = toml_array_in(conf, "module");
if(mod_array==NULL)goto MODULE_SPEC_LOAD_ERROR;
@@ -47,62 +54,62 @@ static struct module_specific *module_specs_load(const char *toml_conf_path, int
goto MODULE_SPEC_LOAD_ERROR;
}
mod_spec[i].load_cb = (module_on_init_func *) dlsym(handle, init_func_name);
if (!mod_spec[i].load_cb) {
mod_spec[i].on_init_cb = (module_on_init_func *) dlsym(handle, init_func_name);
if (!mod_spec[i].on_init_cb) {
fprintf(stderr, "Could not load init function %s: %s\n", init_func_name, dlerror());
}
mod_spec[i].unload_cb = (module_on_exit_func *) dlsym(handle, exit_func_name);
if (!mod_spec[i].unload_cb) {
mod_spec[i].on_exit_cb = (module_on_exit_func *) dlsym(handle, exit_func_name);
if (!mod_spec[i].on_exit_cb) {
fprintf(stderr, "Could not load exit function %s: %s\n", exit_func_name, dlerror());
}
FREE(path);
FREE(init_func_name);
FREE(exit_func_name);
}
toml_free(conf);
return mod_spec;
MODULE_SPEC_LOAD_ERROR:
toml_free(conf);
if(mod_spec)FREE(mod_spec);
*mod_num=0;
return NULL;
}
struct stellar_module_manager *stellar_module_manager_new_with_toml(toml_table_t *conf, int max_thread_num, struct mq_schema *mq_schema)
{
int spec_num = 0;
struct module_specific *specs = module_specs_load(conf, &spec_num);
struct stellar_module_manager *mod_mgr = CALLOC(struct stellar_module_manager, 1);
if(spec_num > 0)
{
utarray_new(mod_mgr->schema.module_specs_array,&module_specs_icd);
utarray_reserve(mod_mgr->schema.module_specs_array, spec_num);
}
mod_mgr->schema.max_thread_num=max_thread_num;
mod_mgr->schema.mq_schema=mq_schema;
// TODO: store module specific data in hash
for(int i = 0; i < spec_num; i++)
{
if (specs[i].on_init_cb != NULL)
{
//TODO: duplicate check mod_name
specs[i].mod=specs[i].on_init_cb(mod_mgr);
utarray_push_back(mod_mgr->schema.module_specs_array, &specs[i]);
}
}
FREE(specs);
return mod_mgr;
}
/*******************************************
* stellar module manager API *
*******************************************/
struct stellar_module_manager *stellar_module_manager_new(const char *module_spec_toml_path, int max_thread_num, struct mq_schema *mq_schema)
{
int spec_num;
struct module_specific *specs = module_specs_load(module_spec_toml_path, &spec_num);
if(spec_num < 0)
{
return NULL;
}
struct stellar_module_manager *mod_mgr = CALLOC(struct stellar_module_manager, 1);
if(spec_num > 0)
{
utarray_new(mod_mgr->schema.module_specs_array,&module_specs_icd);
utarray_reserve(mod_mgr->schema.module_specs_array, spec_num);
}
mod_mgr->schema.max_thread_num=max_thread_num;
mod_mgr->schema.mq_schema=mq_schema;
// TODO: store module specific data in hash
for(int i = 0; i < spec_num; i++)
{
if (specs[i].load_cb != NULL)
{
//TODO: duplicate check mod_name
specs[i].mod=specs[i].load_cb(mod_mgr);
utarray_push_back(mod_mgr->schema.module_specs_array, &specs[i]);
}
}
FREE(specs);
toml_table_t *conf = toml_parse_file_path(module_spec_toml_path);
struct stellar_module_manager *mod_mgr=stellar_module_manager_new_with_toml(conf, max_thread_num, mq_schema);
if(conf)toml_free(conf);
return mod_mgr;
}
@@ -114,8 +121,8 @@ void stellar_module_manager_free(struct stellar_module_manager *mod_mgr)
{
while ((p = (struct module_specific *)utarray_next(mod_mgr->schema.module_specs_array, p)))
{
if (p->unload_cb)
p->unload_cb(mod_mgr, p->mod);
if (p->on_exit_cb)
p->on_exit_cb(mod_mgr, p->mod);
}
utarray_free(mod_mgr->schema.module_specs_array);
}
@@ -180,17 +187,20 @@ void stellar_module_manager_register_thread(struct stellar_module_manager* mod_m
struct stellar_module *stellar_module_manager_get_module(struct stellar_module_manager *mod_mgr, const char *module_name)
{
if(mod_mgr==NULL)return NULL;
if(mod_mgr==NULL || module_name == NULL)return NULL;
struct module_specific *p=NULL;
if (mod_mgr->schema.module_specs_array)
{
while ((p = (struct module_specific *)utarray_next(mod_mgr->schema.module_specs_array, p)))
{
if(strcmp(p->mod->name, module_name)==0)
{
return p->mod;
}
}
if (p->mod)
{
if (strcmp(p->mod->name, module_name) == 0)
{
return p->mod;
}
}
}
}
return NULL;
}
@@ -232,3 +242,10 @@ const char *stellar_module_get_name(struct stellar_module* mod)
if(mod==NULL)return NULL;
return mod->name;
}
void stellar_module_set_name(struct stellar_module* mod, const char *name)
{
if(mod==NULL)return;
strncpy(mod->name, name, NAME_MAX);
return;
}