diff options
| author | 彭宣正 <[email protected]> | 2022-09-05 19:03:03 +0800 |
|---|---|---|
| committer | 彭宣正 <[email protected]> | 2022-09-05 19:03:03 +0800 |
| commit | 2a0f0af98db078dc159a138ef8d25048807222ea (patch) | |
| tree | e92eb30a921a28e8fa50bfa23f78510e7ae50629 /gtest/gtest_lua_timeout.cpp | |
| parent | fb53526b29c8092aed85ec73f17b470df379e95a (diff) | |
✨ feat(TSG-11870): 修改测试用例
Diffstat (limited to 'gtest/gtest_lua_timeout.cpp')
| -rw-r--r-- | gtest/gtest_lua_timeout.cpp | 105 |
1 files changed, 60 insertions, 45 deletions
diff --git a/gtest/gtest_lua_timeout.cpp b/gtest/gtest_lua_timeout.cpp index 88dcd78..a5ecf9c 100644 --- a/gtest/gtest_lua_timeout.cpp +++ b/gtest/gtest_lua_timeout.cpp @@ -1,6 +1,6 @@ #include <gtest/gtest.h> #include <time.h> -#include "tsg_lua_interface.h" +#include "elua.h" static long mstime() { @@ -9,7 +9,7 @@ static long mstime() return now.tv_sec * 1000 + now.tv_nsec/1000000; } -static int count_set(tsg_lua_handle L) +static int count_set(elua_vm *L) { int num = 0; for (int i = 0; i < 10000; i++) @@ -19,91 +19,106 @@ static int count_set(tsg_lua_handle L) num++; } } - int *count = (int *)lua_get_userdata(L); + int *count = (int *)elua_get_execute_userdata(L); *count = num; return 0; } -TEST(lua_time_out, lua) +TEST(elua_time_out, lua) { - tsg_lua_handle L = tsg_lua_vm_create_with_name("TEST"); + elua_vm *L = elua_create_vm("TEST"); const char *ud = "hello world."; - const char *script = "./script/lua_time_out.lua"; - lua_data_t in; - in.data = (char *)"This is a test"; - in.len = strlen(in.data); - lua_arg_t out; + const char *script = "./script/elua_time_out.lua"; + + char *buff= (char *)"This is a test"; + int len = strlen(buff); + elua_data out; out.type = STRING; - out.len = 1024; - out.str = (char *)calloc(1, 1024); + out.integer = 1024; + out.string = (char *)calloc(1, 1024); long tt, elapsed; + int ret = 0; + + struct elua_script *escript = elua_cache_script_file(L, script, 10); - int ret = lua_exec_file(L, script, in, (void *)ud, NULL, 10, &out); - EXPECT_EQ(ERR_SCRIPT_TIMEOUT, ret); - bzero(out.str, 1024); + ret = elua_execute_script(escript, buff, len, (void *)ud, NULL, &out); + EXPECT_EQ(ret, -1); + EXPECT_STREQ(elua_get_last_error_string(L), "[elua_call_script:1074] Lua script killed by time out.."); + bzero(out.string, 1024); + struct elua_script *escript0 = elua_cache_script_file(L, script, 0); tt = mstime(); - ret = lua_exec_file(L, script, in, (void *)ud, NULL, 0, &out); + ret = elua_execute_script(escript0, buff, len, (void *)ud, NULL, &out); elapsed = mstime() - tt; EXPECT_EQ(0, ret); EXPECT_LE(10, elapsed); - EXPECT_EQ(in.len, out.len); + EXPECT_EQ(len, out.len); EXPECT_EQ(STRING, out.type); - EXPECT_STREQ(in.data, out.str); - bzero(out.str, 1024); + EXPECT_STREQ(buff, out.string); + bzero(out.string, 1024); - ret = lua_exec_file(L, script, in, (void *)ud, NULL, 10, &out); - EXPECT_EQ(ERR_SCRIPT_TIMEOUT, ret); - bzero(out.str, 1024); + ret = elua_execute_script(escript, buff, len, (void *)ud, NULL, &out); + EXPECT_EQ(ret, -1); + EXPECT_STREQ(elua_get_last_error_string(L), "[elua_call_script:1074] Lua script killed by time out.."); + bzero(out.string, 1024); - tsg_destory_lua(L); - free(out.str); + elua_cleanup_script(escript); + elua_cleanup_script(escript0); + elua_destroy_vm(L); + free(out.string); } -TEST(lua_time_out, c) +TEST(elua_time_out, c) { - tsg_lua_handle L = tsg_lua_vm_create_with_name("TEST"); + elua_vm *L = elua_create_vm("TEST"); int count = 10; const char *script = "./script/c_time_out.lua"; - lua_data_t in; - in.data = (char *)"This is a test"; - in.len = strlen(in.data); - lua_arg_t out; + + char *buff= (char *)"This is a test"; + int len = strlen(buff); + + struct elua_data out; out.type = STRING; - out.len = 1024; - out.str = (char *)calloc(1, 1024); + out.integer = 1024; + out.string = (char *)calloc(1, 1024); long tt, elapsed; - lua_register_function(L, NULL, "set_count", count_set); + elua_register_cbinding(L, NULL, "set_count", count_set); + struct elua_script *escript = elua_cache_script_file(L, script, 10); tt = mstime(); - int ret = lua_exec_file(L, script, in, (void *)&count, NULL, 10, &out); + int ret = elua_execute_script(escript, buff, len, (void *)&count, NULL, &out); elapsed = mstime() - tt; - EXPECT_EQ(ERR_SCRIPT_TIMEOUT, ret); + EXPECT_EQ(-1, ret); + EXPECT_STREQ(elua_get_last_error_string(L), "[elua_call_script:1074] Lua script killed by time out.."); EXPECT_LE(10, elapsed); EXPECT_EQ(10000000, count); - bzero(out.str, 1024); + bzero(out.string, 1024); + struct elua_script *escript0 = elua_cache_script_file(L, script, 0); tt = mstime(); - ret = lua_exec_file(L, script, in, (void *)&count, NULL, 0, &out); + ret = elua_execute_script(escript0, buff, len, (void *)&count, NULL, &out); elapsed = mstime() - tt; EXPECT_EQ(0, ret); EXPECT_LE(10, elapsed); - EXPECT_EQ(in.len, out.len); + EXPECT_EQ(len, out.len); EXPECT_EQ(STRING, out.type); - EXPECT_STREQ(in.data, out.str); + EXPECT_STREQ(buff, out.string); EXPECT_EQ(10000000, count); - bzero(out.str, 1024); + bzero(out.string, 1024); tt = mstime(); - ret = lua_exec_file(L, script, in, (void *)&count, NULL, 10, &out); + ret = elua_execute_script(escript, buff, len, (void *)&count, NULL, &out); elapsed = mstime() - tt; - EXPECT_EQ(ERR_SCRIPT_TIMEOUT, ret); + EXPECT_EQ(-1, ret); + EXPECT_STREQ(elua_get_last_error_string(L), "[elua_call_script:1074] Lua script killed by time out.."); EXPECT_LE(10, elapsed); EXPECT_EQ(10000000, count); - bzero(out.str, 1024); + bzero(out.string, 1024); - tsg_destory_lua(L); - free(out.str); + elua_cleanup_script(escript0); + elua_cleanup_script(escript); + elua_destroy_vm(L); + free(out.string); }
\ No newline at end of file |
