【修改】更新单元测试

This commit is contained in:
niubinghui
2024-09-02 16:10:51 +08:00
parent 0988578c6a
commit e063af614e
10 changed files with 347 additions and 244 deletions

View File

@@ -1,10 +1,15 @@
# config.toml
[[plugin]]
path = "./plugin/example_plugin-1.lua"
path = "./plugin/example_plugin_regist.lua"
init = "plugin_load"
exit = "plugin_unload"
# [[plugin]]
# path = "./plugin/example_plugin-2.lua"
# init = "plugin_load"
# exit = "plugin_unload"
[[plugin]]
path = "./plugin/example_plugin_topic.lua"
init = "plugin_load"
exit = "plugin_unload"
[[plugin]]
path = "./plugin/example_plugin_message.lua"
init = "plugin_load"
exit = "plugin_unload"

View File

@@ -7,75 +7,159 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <getopt.h>
#include <time.h>
#define PLUGIN_CONFIG_PATH "./conf/plugin_manage.toml"
#define LUA_CONFIG_PATH "./conf/lua_plugin_manage.toml"
// #define DEBUG_PLUGIN_SCHEMA
static struct lua_config_specific *config_load(const char *config_file_name, int *specific_num);
#ifdef DEBUG_PLUGIN_SCHEMA
static void debug_plugin_manage_schema(struct plugin_manager_schema *schema);
#endif
#define PACKET_COUNT 10
#define PACKET_COUNT 3
#define SESSION_COUNT 10
int main()
struct arg_config
{
struct stellar st;
memset(&st, 0, sizeof(st));
int session_count;
int packet_count;
int wait_time;
int thread_num;
unsigned char lua_mod;
unsigned char session_mod;
unsigned char print_mod;
unsigned char time_mod;
char *config_path;
};
int num = 0;
struct lua_config_specific *specific = config_load(LUA_CONFIG_PATH, &num);
struct plugin_manager_schema *plug_mgr = plugin_manager_init(&st, PLUGIN_CONFIG_PATH);
/* 初始化lua插件 */
struct lua_plugin_manage_schema *lua_schema = lua_plugin_manage_init(&st, num, specific);
st.lua_plug_mgr = lua_schema;
struct arg_config global_arg_config = {SESSION_COUNT, PACKET_COUNT, 10, 1, 1, 1, 1, 1, LUA_CONFIG_PATH};
struct lua_config_specific add_specific = {NULL, NULL, NULL};
add_specific.config_specific_file = "./plugin/example_plugin-2.lua";
add_specific.config_specific_load_func = "plugin_load";
add_specific.config_specific_unload_func = "plugin_unload";
static struct lua_config_specific *config_load(const char *config_file_name, int *specific_num);
static void parse_args(int argc, char *argv[]);
// #ifdef DEBUG_PLUGIN_SCHEMA
static void debug_plugin_manage_specifics(struct lua_config_specific *specifics, int num);
static void debug_plugin_manage_schema(struct plugin_manager_schema *schema);
// #endif
lua_plugin_manage_load_one_specific(st.lua_plug_mgr, &add_specific);
#ifdef DEBUG_PLUGIN_SCHEMA
debug_plugin_manage_schema(plug_mgr);
#endif
unsigned char ip_proto=6;
struct packet pkt={&st, TCP, ip_proto};
struct session sess[SESSION_COUNT];
memset(&sess, 0, sizeof(sess));
for(int i=0; i < SESSION_COUNT; i++)
{
sess[i].plug_mgr_rt=plugin_manager_session_runtime_new(plug_mgr, &sess[i]);
sess[i].type=SESSION_TYPE_TCP;
}
for (int j = 0; j < PACKET_COUNT; j++)
int main(int argc, char *argv[])
{
parse_args(argc, argv);
if (global_arg_config.print_mod)
{
// plugin_manager_on_packet_ingress(plug_mgr, &pkt);
for (int i = 0; i < SESSION_COUNT; i++)
{
sess[i].sess_pkt_cnt+=1;
plugin_manager_on_session_ingress(&sess[i], &pkt);
plugin_manager_on_session_egress(&sess[i], &pkt);
}
// plugin_manager_on_packet_egress(plug_mgr, &pkt);
printf("***** ***** ***** ***** ***** *****\n");
printf("load example config:\n");
printf("session count is %d\n", global_arg_config.session_count);
printf("packet count is %d\n", global_arg_config.packet_count);
printf("thread num is %d\n", global_arg_config.thread_num);
printf("load config from path %s\n", global_arg_config.config_path);
printf("***** ***** ***** ***** ***** *****\n\n");
}
for(int i=0; i < SESSION_COUNT; i++)
{
plugin_manager_on_session_closing(&sess[i]);
plugin_manager_session_runtime_free(sess[i].plug_mgr_rt);
}
clock_t start_clock, end_clock;
plugin_manager_exit(plug_mgr);
struct stellar st;
memset(&st, 0, sizeof(st));
st.thread_num = global_arg_config.thread_num;
/* ***** ***** 进行初始化 ***** ***** */
struct plugin_manager_schema *plug_mgr = plugin_manager_init(&st, PLUGIN_CONFIG_PATH);
assert(plug_mgr);
/* 初始化lua插件 */
printf("now wait time to compute memory ... ...\n");
sleep(global_arg_config.wait_time);
struct lua_plugin_manage_schema *lua_schema = NULL;
if (global_arg_config.lua_mod)
{
int num = 0;
struct lua_config_specific *specific = config_load(LUA_CONFIG_PATH, &num);
assert(specific);
if (global_arg_config.print_mod)
debug_plugin_manage_specifics(specific, num);
lua_schema = lua_plugin_manage_init(&st, num, specific);
st.lua_plug_mgr = lua_schema;
assert(lua_schema);
if (specific)
free(specific);
/* 测试单个插件加载函数 */
struct lua_config_specific add_specific = {NULL, NULL, NULL};
add_specific.config_specific_file = "./plugin/example_plugin_load.lua";
add_specific.config_specific_load_func = "plugin_load";
add_specific.config_specific_unload_func = "plugin_unload";
lua_plugin_manage_load_one_specific(st.lua_plug_mgr, &add_specific);
if (global_arg_config.print_mod)
debug_lua_plugin_manage_schema(lua_schema);
printf("now wait time to compute memory ... ...\n");
sleep(global_arg_config.wait_time);
}
// #ifdef DEBUG_PLUGIN_SCHEMA
if (global_arg_config.print_mod)
debug_plugin_manage_schema(plug_mgr);
// #endif
/* ***** ***** 初始化完成 ***** ***** */
/* ***** ***** 会话相关测试 ***** ***** */
if (global_arg_config.session_mod)
{
unsigned char ip_proto = 6;
struct packet pkt = {&st, TCP, ip_proto};
struct session *sess = (struct session *)calloc(global_arg_config.session_count, sizeof(struct session));
memset(sess, 0, global_arg_config.session_count * sizeof(struct session));
/* 创建session */
for (int i = 0; i < global_arg_config.session_count; i++)
{
sess[i].plug_mgr_rt = plugin_manager_session_runtime_new(plug_mgr, &sess[i]);
sess[i].type = SESSION_TYPE_TCP;
}
clock_t total_clock = 0;
for (int j = 0; j < global_arg_config.packet_count; j++)
{
plugin_manager_on_packet_ingress(plug_mgr, &pkt);
for (int i = 0; i < global_arg_config.session_count; i++)
{
sess[i].sess_pkt_cnt += 1;
if (global_arg_config.time_mod)
start_clock = clock();
plugin_manager_on_session_ingress(&sess[i], &pkt);
plugin_manager_on_session_egress(&sess[i], &pkt);
if (global_arg_config.time_mod)
{
end_clock = clock();
total_clock += (end_clock - start_clock);
}
}
if (global_arg_config.time_mod)
{
printf("for session count %d, total time is %ld\n", global_arg_config.session_count, total_clock);
printf("trans to second, time is %f\n", ((double)total_clock / CLOCKS_PER_SEC));
}
plugin_manager_on_packet_egress(plug_mgr, &pkt);
}
/* 释放session */
for (int i = 0; i < global_arg_config.session_count; i++)
{
plugin_manager_on_session_closing(&sess[i]);
plugin_manager_session_runtime_free(sess[i].plug_mgr_rt);
}
}
/* ***** ***** 会话相关完成 ***** ***** */
/* ***** ***** 释放 ***** ***** */
lua_plugin_manage_exit(lua_schema);
plugin_manager_exit(plug_mgr);
/* ***** ***** 释放 ***** ***** */
return 0;
}
@@ -132,15 +216,83 @@ static struct lua_config_specific *config_load(const char *config_file_name, int
return new_spec;
}
#ifdef DEBUG_PLUGIN_SCHEMA
static void parse_args(int argc, char *argv[])
{
int opt;
static const char *shortopts = "s:p:w:c:t:lnde";
static struct option longopts[] = {
{"session_count", required_argument, NULL, 's'},
{"packet_count", required_argument, NULL, 'p'},
{"wait_time", required_argument, NULL, 'w'},
{"config_path", required_argument, NULL, 'c'},
{"thread_num", required_argument, NULL, 't'},
{"disable_lua", no_argument, NULL, 'l'},
{"disable_session", no_argument, NULL, 'n'},
{"print_debug", no_argument, NULL, 'd'},
{"disable_time", no_argument, NULL, 'e'},
{0, 0, 0, 0},
};
while ((opt = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1)
{
switch (opt)
{
case 's':
global_arg_config.session_count = atoi(optarg);
break;
case 'p':
global_arg_config.packet_count = atoi(optarg);
break;
case 'w':
global_arg_config.wait_time = atoi(optarg);
break;
case 'c':
global_arg_config.config_path = strdup(optarg);
break;
case 't':
global_arg_config.thread_num = atoi(optarg);
break;
case 'l':
global_arg_config.lua_mod = 0;
break;
case 'n':
global_arg_config.session_mod = 0;
break;
case 'd':
global_arg_config.print_mod = 1;
break;
case 'e':
global_arg_config.time_mod = 0;
break;
default:
break;
}
}
}
// #ifdef DEBUG_PLUGIN_SCHEMA
static void debug_plugin_manage_specifics(struct lua_config_specific *specifics, int num)
{
printf("***** ***** ***** ***** ***** *****\n");
printf("load config specifics count is %d\n", num);
for (int i = 0; i < num; ++i)
{
printf("[%d]file path: %s\n", i, specifics[i].config_specific_file);
printf("[%d]load func: %s\n", i, specifics[i].config_specific_load_func);
printf("[%d]unload func: %s\n", i, specifics[i].config_specific_unload_func);
}
printf("***** ***** ***** ***** ***** *****\n\n");
}
static void debug_plugin_manage_schema(struct plugin_manager_schema *schema)
{
struct registered_session_plugin_schema * plugin = NULL;
for (int i = 0; i < (int)utarray_len(schema->registered_session_plugin_array); ++i) {
struct registered_session_plugin_schema *plugin = NULL;
for (int i = 0; i < (int)utarray_len(schema->registered_session_plugin_array); ++i)
{
plugin = (struct registered_session_plugin_schema *)utarray_eltptr(schema->registered_session_plugin_array, (unsigned int)i);
printf("plugin[%d]: new func %p, free func %p, env %p\n", i, plugin->on_ctx_new, plugin->on_ctx_free, plugin->plugin_env);
}
printf("\n");
return;
}
#endif
// #endif

View File

@@ -14,6 +14,7 @@ struct stellar
{
struct plugin_manager_schema *plug_mgr;
struct lua_plugin_manage_schema * lua_plug_mgr;
int thread_num;
};
struct packet
@@ -40,8 +41,6 @@ enum session_type session_get_type(struct session *sess)
return sess->type;
}
int session_get_current_plugin_id(struct session *sess)
{
return sess->plug_mgr_rt->current_session_plugin_id;
@@ -67,7 +66,7 @@ int session_get_current_thread_id(struct session *sess)
int stellar_get_worker_thread_num(struct stellar *st)
{
return 16;
return st->thread_num;
}
int stellar_get_current_thread_id(struct stellar *st)

View File

@@ -1,26 +0,0 @@
function plugin_ctx_new(sess, plug_env, sess_context)
print("now begin to create new ctx context example-1")
print(plug_env.data)
sess_context.id = 100
end
function plugin_ctx_free(sess, sess_context, plug_env)
print("now begin to free ctx context example-1")
print(sess_context.id)
end
function plugin_load(stellar, plug_env)
print("now begin to load plugin example-1")
plug_env.data = "my example-1 plugin env"
plug_env.newid = 1000
plug_env.id = plugin_manage.register(stellar, plugin_ctx_new, plugin_ctx_free, plug_env)
plug_env.messid = 100
end
function plugin_unload(plug_env)
print("now running unload plugin example-1 function")
-- print(plug_env.__penv_pointer)
-- print(plug_env.data)
-- print(plug_env.newid)
-- print(plug_env.messid)
end

View File

@@ -1,55 +0,0 @@
function plugin_ctx_new(sess, plug_env, sess_context)
print("now create new ctx example-2, plugin id ", plug_env.id)
local sesstype = session.gettype(sess)
sess_context.id = 200
sess_context.called = 0
print("session type is ", sesstype)
-- session.setid(sess, 50000)
end
function plugin_ctx_free(sess, sess_context, plug_env)
print(plug_env.id, sess_context.id, sess_context.called, plug_env.called)
print("now begin to free ctx context example-2")
end
function on_message(sess, topic_id, msg, sess_context, env)
-- print(sess, topic_id, msg, sess_context, env)
sess_context.called = sess_context.called + 1
env.calledcount = env.calledcount + 1
print("call on message", env.id, env.calledcount, sess_context.called)
end
function free_message()
print("free message")
end
function plugin_load(stellar, plug_env)
print("now begin to load plugin example-2")
plug_env.id = plugin_manage.register(stellar, plugin_ctx_new, plugin_ctx_free, plug_env)
topic_id = message.gettopicid(stellar, "TCP")
print("get topic id is ", topic_id)
message.subscribetopic(stellar, topic_id, on_message, plug_env.id)
-- print("subscribetopic result", bool)
-- mq_topic_id = message.gettopicid(stellar, "SESSION_MQ_TOPIC")
-- print("get session mq topic is", mq_topic_id)
-- message.subscribetopic(stellar, mq_topic_id, on_message, plug_env.id)
plug_env.calledcount = 0
--[[
create_id = message.gettopicid(stellar, "TOPIC_LUA_SESSION_TEST")
if (create_id < 0)
then
print("no topic, create new one")
create_table = {}
create_id = message.createtopic(stellar, "TOPIC_LUA_SESSION_TEST", free_message, create_table)
print("create topic is ", create_id)
else
print("has created, id is ", create_id)
end
--]]
end
function plugin_unload(plug_env)
print("now running unload plugin example-2 function, plugin id is ", plug_env.id)
end

View File

@@ -0,0 +1,11 @@
function plugin_load(stellar, plug_env)
print("now load lua plugin example load")
print("this example aims to test load function")
plug_env.data = "load plugin env"
print("plugin env data is", plug_env.data)
end
function plugin_unload(plug_env)
print("now unload lua plugin example load")
print("plugin env data is", plug_env.data)
end

View File

@@ -0,0 +1,30 @@
function plugin_ctx_new(sess, plug_env, sess_context)
-- print("now create new ctx example message, plugin id", plug_env.id)
end
function plugin_ctx_free(sess, sess_context, plug_env)
-- print("now begin to free ctx context example message")
end
function on_message(sess, topic_id, msg, sess_context, env)
-- print("message call on message function, id", topic_id)
-- print("message is", msg.data)
end
function plugin_load(stellar, plug_env)
print("now begin to load plugin example messaga")
print("this example aims to test topic functions")
plug_env.st = stellar
plug_env.id = plugin_manage.register(stellar, plugin_ctx_new, plugin_ctx_free, plug_env)
plug_env.data = "this is message example plug env data"
--[[ 订阅TOPIC_SESSION_STAT, 并完成函数注册 ]]
test_topic_id = message.gettopicid(stellar, "TOPIC_SESSION_STAT")
message.subscribetopic(stellar, test_topic_id, on_message, plug_env.id)
end
function plugin_unload(plug_env)
print("now unload lua plugin example message")
print("plugin env data is", plug_env.data)
end

View File

@@ -0,0 +1,25 @@
function plugin_ctx_new(sess, plug_env, sess_context)
print("now begin to create new ctx context example regist")
sess_context.data = "this is example regist sess context data"
print("session data is", sess_context.data)
end
function plugin_ctx_free(sess, sess_context, plug_env)
print("now begin to free ctx context example regist")
print("session data is", sess_context.data)
end
function plugin_load(stellar, plug_env)
print("now load lua plugin example regist")
print("this example aims to test regist function")
plug_env.data = "my example regist plugin env"
--[[ 由于未订阅消息, 注册函数应该永远不会触发 ]]
plug_env.id = plugin_manage.register(stellar, plugin_ctx_new, plugin_ctx_free, plug_env)
print("plugin env data is", plug_env.data)
print("after regist, plugin id is", plug_env.id)
end
function plugin_unload(plug_env)
print("now unload lua plugin example regist")
print("plugin env data is", plug_env.data)
end

View File

@@ -0,0 +1,57 @@
function plugin_ctx_new(sess, plug_env, sess_context)
-- print("now create new ctx example topic, plugin id", plug_env.id)
msg = {}
msg.data = "this is message"
message.publishmessage(sess, plug_env.topic_id, msg)
end
function plugin_ctx_free(sess, sess_context, plug_env)
-- print("now begin to free ctx context example topic")
end
function on_message(sess, topic_id, msg, sess_context, env)
-- print("topic call on message function, id", topic_id)
end
function free_message(sess, msg, private_env)
-- print("message need to free is", msg.data)
-- print("env id is ", private_env.id)
end
function plugin_load(stellar, plug_env)
print("now begin to load plugin example topic")
print("this example aims to test topic functions")
plug_env.st = stellar
plug_env.id = plugin_manage.register(stellar, plugin_ctx_new, plugin_ctx_free, plug_env)
plug_env.data = "this is topic example plug env data"
--[[ 获取TCP topic, 并完成订阅 ]]
tcp_topic_id = message.gettopicid(stellar, "TCP")
print("get TCP topic id is", tcp_topic_id)
message.subscribetopic(stellar, tcp_topic_id, on_message, plug_env.id)
--[[ 创建TOPIC_SESSION_STAT, 并完成函数注册 ]]
msg_private_table = {}
msg_private_table.data = "this is example topic msg private data"
test_topic_id = message.gettopicid(stellar, "TOPIC_SESSION_STAT")
if (test_topic_id < 0)
then
--[[ 该消息未创建, 创建该topic ]]
test_topic_id = message.createtopic(stellar, "TOPIC_SESSION_STAT", free_message, msg_private_table)
print("create topic is", test_topic_id)
else
--[[ 如果该消息已经注册, 更新其注册函数 ]]
message.updatetopic(stellar, test_topic_id, free_message, msg_private_table)
print("topic already created, id is", test_topic_id)
end
plug_env.tcp_topic_id = tcp_topic_id
plug_env.topic_id = test_topic_id
end
function plugin_unload(plug_env)
print("now unload lua plugin example topic")
print("plugin env data is", plug_env.data)
print("now destory topic id is", plug_env.topic_id)
message.destorytopic(plug_env.st, plug_env.topic_id)
end

View File

@@ -303,12 +303,13 @@ void simple_session_packet_plugin_exit(void *plugin_env)
static void simple_plugin_sub_session_stat_on_msg(struct session *sess, int topic_id, const void *data, void *plugin_ctx , void *plugin_env)
{
// printf("get message, %d\n", topic_id);
struct simple_stellar_plugin_env *env = (struct simple_stellar_plugin_env *)plugin_env;
if(topic_id == env->egress_topic_id)
{
session_mq_ignore_message(sess, topic_id, env->session_plugin_id);
}
#if 0
if (topic_id == env->stat_topic_id)
{
struct mq_message_stat *stat = (struct mq_message_stat *)data;
@@ -321,6 +322,7 @@ static void simple_plugin_sub_session_stat_on_msg(struct session *sess, int topi
// print_session_stat(sess, stat, env->plugin_id, __FUNCTION__);
session_mq_unignore_message(sess, env->egress_topic_id, env->session_plugin_id);
}
#endif
return;
}
@@ -357,101 +359,4 @@ void simple_plugin_sub_session_stat_exit(void *plugin_env)
{
if(plugin_env)FREE(plugin_env);
return;
}
struct session_plugin_env
{
struct plugin_manager_schema *plug_mgr;
int N_session;
int N_per_session_pkt_cnt;
int intrinsc_tcp_topic_id;
int intrinsc_egress_topic_id;
int basic_exdata_idx;
int basic_exdata_free_called;
int basic_on_session_ingress_called;
int basic_on_session_egress_called;
int basic_ctx_new_called;
int basic_ctx_free_called;
int test_mq_pub_plugin_id;
int test_mq_sub_plugin_id;
int test_mq_pub_called;
int test_mq_sub_called;
int test_mq_free_called;
int test_mq_topic_id;
int plugin_id_1;
int plugin_id_2;
int plugin_id_1_called;
int plugin_id_2_called;
};
struct test_basic_ctx
{
int called;
};
static void *test_dettach_session_ctx_new(struct session *sess, void *plugin_env)
{
printf("now running ctx new\n");
struct test_basic_ctx *ctx=(struct test_basic_ctx *)calloc(1, sizeof(struct test_basic_ctx));
struct session_plugin_env *env = (struct session_plugin_env *)plugin_env;
session_mq_publish_message(sess, env->test_mq_topic_id, plugin_env);// publish success, but won't be delivered to currnet plugin
stellar_session_plugin_dettach_current_session(sess);
ctx->called+=1;
session_mq_publish_message(sess, env->test_mq_topic_id, plugin_env);// publish success, but won't be delivered to currnet plugin
return ctx;
}
static void test_dettach_session_ctx_free(struct session *sess, void *session_ctx, void *plugin_env)
{
struct session_plugin_env *env = (struct session_plugin_env *)plugin_env;
env->basic_ctx_free_called+=1;
struct test_basic_ctx *ctx=(struct test_basic_ctx *)session_ctx;
printf("now free ctx, called is %d\n", ctx->called);
session_mq_publish_message(sess, env->test_mq_topic_id, plugin_env);// publish success, but won't be delivered to currnet plugin
free(ctx);
}
static void test_dettach_on_session(struct session *sess, int topic_id, const void *msg , void *per_session_ctx, void *plugin_env)
{
struct test_basic_ctx *ctx=(struct test_basic_ctx *)per_session_ctx;
// struct session_plugin_env *env = (struct session_plugin_env *)plugin_env;
ctx->called+=1;
}
static void test_dettach_msg_free(struct session *sess, void *msg, void *msg_free_arg)
{
struct session_plugin_env *env = (struct session_plugin_env *)msg_free_arg;
env->test_mq_free_called+=1;
return;
}
void * test_plugin_init(struct stellar * st)
{
struct session_plugin_env * env = CALLOC(struct session_plugin_env, 1);
// env.plug_mgr=plug_mgr;
// env->N_per_session_pkt_cnt=10;
// env->N_session=10
/* 先插入一个C插件 */
int plugin_id=stellar_session_plugin_register(st, test_dettach_session_ctx_new, test_dettach_session_ctx_free, env);
printf("plugin regist id is %d\n", plugin_id);
env->intrinsc_tcp_topic_id=stellar_session_mq_get_topic_id(st, TOPIC_TCP);
printf("TCP topic id is %d\n", env->intrinsc_egress_topic_id);
stellar_session_mq_subscribe(st, env->intrinsc_tcp_topic_id, test_dettach_on_session, plugin_id);
env->test_mq_topic_id=stellar_session_mq_create_topic(st, "SESSION_MQ_TOPIC", test_dettach_msg_free, &env);
stellar_session_mq_subscribe(st, env->test_mq_topic_id, test_dettach_on_session, plugin_id);
return env;
}
void test_plugin_free(void * plug_env)
{
struct session_plugin_env * env = (struct session_plugin_env *)plug_env;
if (env) free(env);
return;
}