From b128b931b594d70fe142a9fedeaf1c767b5aee8e Mon Sep 17 00:00:00 2001 From: findstr Date: Thu, 14 Mar 2024 19:44:00 +0800 Subject: [PATCH] reimplement startup mechanism and env module --- Makefile | 3 +- README.md | 17 +-- examples/patch.lua | 2 +- examples/socket.lua | 2 +- examples/start.sh | 4 +- examples/websocket.lua | 30 ++-- lualib-src/lualib-env.c | 114 ++++++++++++-- silly-src/main.c | 313 +++++++++++++-------------------------- silly-src/silly.h | 17 ++- silly-src/silly_conf.h | 7 + silly-src/silly_env.c | 48 ------ silly-src/silly_env.h | 10 -- silly-src/silly_run.c | 1 - silly-src/silly_worker.c | 35 ++++- silly-src/silly_worker.h | 2 + test/test.conf | 6 +- test/test.lua | 33 ++--- test/testaux.lua | 1 - test/testchannel.lua | 45 +++--- test/testcrypto.lua | 144 +++++++++--------- test/testdns.lua | 70 +++++---- test/testdom.lua | 26 ++-- test/testexit.lua | 29 ++-- test/testhpack.lua | 56 ++++--- test/testhttp.lua | 93 ++++++------ test/testhttp2.lua | 132 ++++++++--------- test/testjson.lua | 26 ++-- test/testmulticast.lua | 75 +++++----- test/testmutex.lua | 16 +- test/testmysql.lua | 20 +-- test/testnetpacket.lua | 25 ++-- test/testnetstream.lua | 68 +++++---- test/testpatch.lua | 16 +- test/testredis.lua | 52 ++++--- test/testrpc.lua | 11 +- test/testssl.lua | 27 ++-- test/testtcp.lua | 51 ++++--- test/testtimer.lua | 10 +- test/testudp.lua | 22 ++- test/testwaitgroup.lua | 24 ++- test/testwakeup.lua | 45 +++--- test/testwebsocket.lua | 13 +- 42 files changed, 821 insertions(+), 920 deletions(-) delete mode 100644 silly-src/silly_env.c delete mode 100644 silly-src/silly_env.h diff --git a/Makefile b/Makefile index 22d6c209..897a739f 100644 --- a/Makefile +++ b/Makefile @@ -65,7 +65,6 @@ SRC_FILE = \ silly_timer.c \ silly_run.c \ silly_daemon.c \ - silly_env.c \ silly_malloc.c \ silly_log.c \ silly_trace.c \ @@ -120,7 +119,7 @@ $(LUACLIB_PATH)/test.so: $(LIB_PATH)/lualib-test.c | $(LUACLIB_PATH) test: CCFLAG += -fsanitize=address -fno-omit-frame-pointer -DSILLY_TEST test: LDFLAG += -fsanitize=address -fno-omit-frame-pointer test: $(PLATS) - ./$(TARGET) test/test.conf + ./$(TARGET) test/test.lua --lualib_path="test/?.lua" clean: -rm $(SRC:.c=.o) *.so $(TARGET) diff --git a/README.md b/README.md index 3bcab4bc..f2f65ac7 100644 --- a/README.md +++ b/README.md @@ -15,23 +15,16 @@ - `make` - `make TLS=off` (disable TLS function) -## Configuration - -- `daemon`, 1 --> run as daemon, 0 --> normal -- `bootstrap`, lua entry file -- `lualib_path`, will append the package.path (in luaVM) -- `lualib_cpath`, will append the package.cpath (int luaVM) -- `logpath`, if running in daemon mode, all print messages will be written to the `[logpath]/silly-[pid].log` file -- `loglevel`, is used to control the log level limits, it can be set to `debug`, `info`, `warn`, or `error`, default is `info` -- `pidfile`, if running in daemon mode, `pidfile` will used by run only once in a system - ## Running - ./silly + ./silly [options] + +## Options + ./silly -h ## Test - Tests are in the test folder -- Run `./silly test/test.conf` to test all modules +- Run `./silly test/test.lua --lualib_path="test/?.lua` to test all modules ## Examples diff --git a/examples/patch.lua b/examples/patch.lua index 2f7a3f31..ffe651bf 100644 --- a/examples/patch.lua +++ b/examples/patch.lua @@ -1,5 +1,5 @@ local console = require "sys.console" -local run = require "patch.run" +local run = require "examples.patch.run" console { addr = "127.0.0.1:2345", diff --git a/examples/socket.lua b/examples/socket.lua index 4f8aa7b2..3242c6c5 100644 --- a/examples/socket.lua +++ b/examples/socket.lua @@ -1,6 +1,6 @@ local core = require "sys.core" local crypto = require "sys.crypto" -local socket = require "sys.socket" +local socket = require "sys.net.tcp" socket.listen("127.0.0.1:9999", function(fd, addr) print("accept", fd, addr) diff --git a/examples/start.sh b/examples/start.sh index be9ca39f..357f0388 100755 --- a/examples/start.sh +++ b/examples/start.sh @@ -1,14 +1,14 @@ #!/bin/bash module() { - ./silly --lualib_path="lualib/?.lua;examples/?.lua" --lualib_cpath="luaclib/?.so" --bootstrap="examples/$1.lua" + ./silly "examples/$1.lua" } all() { array=($(ls examples/*.lua)) for i in "${array[@]}"; do echo "---------------$i-------------------" - timeout 5 ./silly --lualib_path="lualib/?.lua;examples/?.lua" --lualib_cpath="luaclib/?.so" --bootstrap="$i" + timeout 5 ./silly "$i" done exit 0 } diff --git a/examples/websocket.lua b/examples/websocket.lua index 9b2f6e8d..caa646f1 100644 --- a/examples/websocket.lua +++ b/examples/websocket.lua @@ -2,17 +2,29 @@ local core = require "sys.core" local crypto = require "sys.crypto" local websocket = require "http.websocket" +local handler = function(sock) + local dat, typ = sock:read() + print("server read:", dat, typ) + sock:write(dat, "pong") + sock:close() +end + + websocket.listen { port = "127.0.0.1:9999", - tls_port = "127.0.0.1:8888", - tls_cert = "test/cert.pem", - tls_key = "test/key.pem", - handler = function(sock) - local dat, typ = sock:read() - print("server read:", dat, typ) - sock:write(dat, "pong") - sock:close() - end + handler = handler +} + +websocket.listen { + tls = true, + port = "127.0.0.1:8888", + certs = { + { + cert = "test/cert.pem", + cert_key = "test/key.pem", + } + }, + handler = handler, } core.start(function() diff --git a/lualib-src/lualib-env.c b/lualib-src/lualib-env.c index 8bc7531e..81e32df0 100644 --- a/lualib-src/lualib-env.c +++ b/lualib-src/lualib-env.c @@ -12,43 +12,131 @@ #include "silly.h" #include "compiler.h" -#include "silly_env.h" +#include "silly_log.h" +#include "silly_worker.h" +static const char *load_config = "\ + local env, file = ...\ + local config = {}\ + local function eval(parent, tbl)\ + for k, v in pairs(tbl) do\ + if #parent > 0 then\ + k = parent .. '.' .. tostring(k)\ + end\ + if type(v) == 'table' then\ + eval(k, v)\ + elseif not env[k] then\ + env[k] = v\ + end\ + end\ + return t\ + end\ + local function include(name)\ + local f = io.open(name, 'r')\ + if not f then\ + error('open config error of file:' .. name)\ + end\ + local code = f:read('a')\ + if not code then\ + error('read config error of file:' .. name)\ + end\ + f:close()\ + assert(load(code, name, 't', config))()\ + return \ + end\ + config.include = include\ + config.ENV = os.getenv\ + include(file)\ + config.include = nil\ + config.ENV = nil\ + eval('', config)\ + "; + +static const char * +skipcode(const char *str) +{ + while (*str && *str++ != ']') + ; + if (*str) + str += 3; + return str; +} static int -lget(lua_State *L) +lload(lua_State *L) { - const char *key = luaL_checkstring(L, 1); - const char *value = silly_env_get(key); - if (value) - lua_pushstring(L, value); - else + int err; + err = luaL_loadstring(L, load_config); + assert(err == LUA_OK); + lua_pushvalue(L, lua_upvalueindex(1)); //env_table + lua_pushvalue(L, 1); + err = lua_pcall(L, 2, 0, 0); + if (err != LUA_OK) { + const char *err = lua_tostring(L, -1); + err = skipcode(err); + lua_pushstring(L, err); + lua_replace(L, -2); + } else { lua_pushnil(L); + } + return 1; +} +static int +lget(lua_State *L) +{ + lua_pushvalue(L, lua_upvalueindex(1)); //env_table + lua_pushvalue(L, 1); + lua_gettable(L, -2); return 1; } static int lset(lua_State *L) { - const char *key = luaL_checkstring(L, 1); - const char *value = luaL_checkstring(L, 2); - silly_env_set(key, value); + lua_pushvalue(L, lua_upvalueindex(1)); //env_table + lua_pushvalue(L, 1); + lua_pushvalue(L, 2); + lua_settable(L, -3); return 0; } +static void +load_args(lua_State *L) +{ + int i, argc; + char **argv; + argv = silly_worker_args(&argc); + for (i = 0; i < argc; i++) { + const char *k, *v; + char *str = argv[i]; + if (strlen(str) <= 2 || str[0] != '-' || str[1] != '-') { + continue; + } + k = strtok(&str[2], "="); + v = strtok(NULL, "="); + if (k != NULL && v != NULL) { + lua_pushstring(L, k); + lua_pushstring(L, v); + lua_settable(L, -3); + } + } +} + int luaopen_sys_env(lua_State *L) { luaL_Reg tbl[] = { + {"load", lload}, {"get", lget}, {"set", lset}, //end {NULL, NULL}, }; - luaL_checkversion(L); - luaL_newlib(L, tbl); + luaL_newlibtable(L,tbl); + lua_newtable(L); + load_args(L); + luaL_setfuncs(L,tbl,1); return 1; } - diff --git a/silly-src/main.c b/silly-src/main.c index f3109d9f..23272a5d 100644 --- a/silly-src/main.c +++ b/silly-src/main.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -9,187 +10,50 @@ #include "silly.h" #include "silly_trace.h" #include "silly_log.h" -#include "silly_env.h" #include "silly_timer.h" #include "silly_run.h" #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -static inline int -optint(const char *key, int v) -{ - int n; - int len; - char *end; - const char *val; - val = silly_env_get(key); - if (val == NULL) - return v; - len = strlen(val); - errno = 0; - n = strtol(val, &end, 0); - if (errno != 0 || end != (val + len)) { - const char *fmt = "[config] incorrect value of '%s' %s\n"; - silly_log_error(fmt, key); - exit(-1); - } - return n; -} - -static inline const char * -optstr(const char *key, size_t *sz, const char *v) -{ - const char *str; - str = silly_env_get(key); - if (str == NULL) - str = v; - *sz = strlen(str); - return str; -} - static void -enveach(lua_State *L, char *first, char *curr, char *end) +print_help(const char *selfname) { - lua_pushnil(L); - while (lua_next(L, -2) != 0) { - size_t sz; - const char *k; - int type = lua_type(L, -2); - if (type != LUA_TSTRING && type != LUA_TNUMBER) { - silly_log_error("[checktype] key expecte string/number" - " but got %s\n", lua_typename(L, type)); - exit(-1); - } - lua_pushvalue(L, -2); - k = lua_tolstring(L, -1, &sz); - assert(curr <= end); - if (sz >= (size_t)(end - curr)) { - silly_log_error("[enveach] buff is too short\n"); - exit(-1); - } - lua_pop(L, 1); - memcpy(curr, k, sz); - if (lua_type(L, -1) == LUA_TTABLE) { - curr[sz] = '.'; - enveach(L, first, &curr[sz + 1], end); - } else { - int type = lua_type(L, -1); - if (type != LUA_TSTRING && type != LUA_TNUMBER) { - const char *fmt = "[enveach]" - "%s expect string/number bug got:%s\n"; - silly_log_error(fmt, lua_typename(L, type)); - } - const char *value = lua_tostring(L, -1); - curr[sz] = '\0'; - silly_env_set(first, value); - } - lua_pop(L, 1); - } - return ; -} - -static const char *load_config = "\ - local config = {}\ - local function include(name)\ - local f = io.open(name, 'r')\ - if not f then\ - error('open config error of file:' .. name)\ - end\ - local code = f:read('a')\ - if not code then\ - error('read config error of file:' .. name)\ - end\ - f:close()\ - assert(load(code, name, 't', config))()\ - return \ - end\ - config.include = include\ - include(...)\ - config.include = nil\ - return config\ - "; - -static const char * -skipcode(const char *str) -{ - while (*str && *str++ != ']') - ; - if (*str) - str += 3; - return str; -} - -static void -initenv(const char *self, const char *file) -{ - int err; - char name[256] = {0}; - lua_State *L = luaL_newstate(); - luaL_openlibs(L); - err = luaL_loadstring(L, load_config); - lua_pushstring(L, file); - assert(err == LUA_OK); - err = lua_pcall(L, 1, 1, 0); - if (err != LUA_OK) { - const char *err = lua_tostring(L, -1); - err = skipcode(err); - silly_log_error("%s parse config file:%s fail,%s\n", - self, file, err); - lua_close(L); - exit(-1); - } - enveach(L, name, name, &name[256]); - lua_close(L); - return ; + printf("Usage: %s main.lua [options]\n", selfname); + printf("Options:\n"); + printf(" -h, --help Show help\n"); + printf(" -v, --version Show version\n"); + printf(" -d, --daemon Run as a daemon\n"); + printf(" -p, --logpath PATH Specify the path for the log file\n"); + printf(" -l, --loglevel LEVEL Set the logging level (e.g. debug, info, warn, error)\n"); + printf(" -f, --pidfile FILE Specify the path for the PID file\n"); + printf(" -L, --lualib_path PATH Specify the path for Lua libraries\n"); + printf(" -C, --lualib_cpath PATH Specify the path for C Lua libraries\n"); + printf(" -S, --socket_cpu_affinity CPU Set CPU affinity for socket thread\n"); + printf(" -W, --worker_cpu_affinity CPU Set CPU affinity for worker threads\n"); + printf(" -T, --timer_cpu_affinity CPU Set CPU affinity for timer thread\n"); } static void -applyconfig(struct silly_config *config) +parse_args(struct silly_config *config, int argc, char *argv[]) { - int slash; - char *p; - size_t i, sz, n; - const char *str; - config->daemon = optint("daemon", 0); - //bootstrap - str = optstr("bootstrap", &sz, ""); - if (sz >= ARRAY_SIZE(config->bootstrap)) { - silly_log_error("[config] bootstrap is too long\n"); - exit(-1); - } - if (sz == 0) { - silly_log_error("[config] bootstrap can't be empty\n"); - exit(-1); - } - memcpy(config->bootstrap, str, sz + 1); - //lualib_path - str = optstr("lualib_path", &sz, ""); - if (sz >= ARRAY_SIZE(config->lualib_path)) { - silly_log_error("[config] lualib_path is too long\n"); - exit(-1); - } - memcpy(config->lualib_path, str, sz + 1); - //lualib_cpath - str = optstr("lualib_cpath", &sz, ""); - if (sz >= ARRAY_SIZE(config->lualib_cpath)) { - silly_log_error("[config] lualib_cpath is too long\n"); - exit(-1); - } - memcpy(config->lualib_cpath, str, sz + 1); - //logpath - str = optstr("logpath", &sz, ""); - slash = sz > 0 ? str[sz - 1] : '/'; - p = config->logpath; - n = ARRAY_SIZE(config->logpath); - if (slash == '/') - sz = snprintf(p, n, "%s%s.log", str, config->selfname); - else - sz = snprintf(p, n, "%s", str); - if (sz >= n) { - silly_log_error("[config] logpath is too long\n"); - exit(-1); - } - //log level + int c; + unsigned int i; + optind = 2; + opterr = 0; + struct option long_options[] = { + {"help", no_argument, 0, 'h'}, + {"version", no_argument, 0, 'v'}, + {"daemon", no_argument, 0, 'd'}, + {"logpath", required_argument, 0, 'p'}, + {"loglevel", required_argument, 0, 'l'}, + {"pidfile", required_argument, 0, 'f'}, + {"lualib_path", required_argument, 0, 'L'}, + {"lualib_cpath", required_argument, 0, 'C'}, + {"socket_cpu_affinity", required_argument, 0, 'S'}, + {"worker_cpu_affinity", required_argument, 0, 'W'}, + {"timer_cpu_affinity", required_argument, 0, 'T'}, + {0, 0, 0, 0} + }; struct { const char *name; enum silly_log_level level; @@ -199,25 +63,70 @@ applyconfig(struct silly_config *config) {"warn", SILLY_LOG_WARN}, {"error", SILLY_LOG_ERROR}, }; - str = optstr("loglevel", &sz, ""); - for (i = 0; i < ARRAY_SIZE(loglevels); i++) { - if (strcmp(loglevels[i].name, str) == 0) { - silly_log_setlevel(loglevels[i].level); + for (;;) { + c = getopt_long(argc, argv, "hvdp:l:f:L:C:S:W:T:", long_options, NULL); + if (c == -1) + break; + switch (c) { + case 'h': + print_help(config->selfname); + exit(0); + break; + case 'v': + printf("%s\n", SILLY_VERSION); + exit(0); + break; + case 'd': + config->daemon = 1; + break; + case 'p': + if (strlen(optarg) >= ARRAY_SIZE(config->logpath)) { + silly_log_error("[option] logpath is too long\n"); + } + strncpy(config->logpath, optarg, ARRAY_SIZE(config->logpath) - 1); + break; + case 'l': + for (i = 0; i < ARRAY_SIZE(loglevels); i++) { + if (strcmp(loglevels[i].name, optarg) == 0) { + silly_log_setlevel(loglevels[i].level); + break; + } + } + if (i == ARRAY_SIZE(loglevels)) { + silly_log_error("[option] unknown loglevel:%s\n", optarg); + } + break; + case 'f': + if (strlen(optarg) >= ARRAY_SIZE(config->pidfile)) { + silly_log_error("[option] pidfile is too long\n"); + } + strncpy(config->pidfile, optarg, ARRAY_SIZE(config->pidfile) - 1); + break; + case 'L': + if (strlen(optarg) >= ARRAY_SIZE(config->lualib_path)) { + silly_log_error("[option] lualib_path is too long\n"); + } + strncpy(config->lualib_path, optarg, ARRAY_SIZE(config->lualib_path) - 1); + break; + case 'C': + if (strlen(optarg) >= ARRAY_SIZE(config->lualib_cpath)) { + silly_log_error("[option] lualib_cpath is too long\n"); + } + strncpy(config->lualib_cpath, optarg, ARRAY_SIZE(config->lualib_cpath) - 1); + break; + case 'S': + config->socketaffinity = atoi(optarg); + break; + case 'W': + config->workeraffinity = atoi(optarg); + break; + case 'T': + config->timeraffinity = atoi(optarg); + break; + case '?': break; } } - //pidfile - str = optstr("pidfile", &sz, ""); - if ((sz + 1) >= ARRAY_SIZE(config->pidfile)) { - silly_log_error("[config] pidfile is too long\n"); - exit(-1); - } - memcpy(config->pidfile, str, sz + 1); - //cpu affinity - config->socketaffinity= optint("socket_cpu_affinity", -1); - config->workeraffinity = optint("worker_cpu_affinity", -1); - config->timeraffinity = optint("timer_cpu_affinity", -1); - return; } static const char * @@ -234,39 +143,21 @@ selfname(const char *path) int main(int argc, char *argv[]) { - int i, status; + int status; struct silly_config config; + config.argc = argc; + config.argv = argv; + config.selfname = selfname(argv[0]); if (argc < 2) { - printf("USAGE:%s --parameters\n", argv[0]); + print_help(config.selfname); return -1; } silly_trace_init(); - silly_env_init(); silly_log_init(); silly_timer_init(); - config.selfname = selfname(argv[0]); - i = 1; - //first argument is t he config file name? - if (argv[i][0] != '-' || argv[i][1] != '-') - initenv(config.selfname, argv[i++]); - while (i < argc) { - if (argv[i][0] != '-' || argv[i][1] != '-') { - silly_log_error("[config] skip argument '%s'\n", argv[i]); - } else { - const char *k, *v; - char *str = argv[i]; - k = strtok(&str[2], "="); - v = strtok(NULL, "="); - if (k == NULL || v == NULL) - silly_log_error("[config] skip argument '%s'\n", str); - else - silly_env_set(k, v); - } - ++i; - } - applyconfig(&config); + strncpy(config.bootstrap, argv[1], ARRAY_SIZE(config.bootstrap) - 1); + parse_args(&config, argc, argv); status = silly_run(&config); - silly_env_exit(); silly_log_info("%s exit, leak memory size:%zu\n", argv[0], silly_memused()); silly_log_flush(); diff --git a/silly-src/silly.h b/silly-src/silly.h index 961f14a0..5b247581 100644 --- a/silly-src/silly.h +++ b/silly-src/silly.h @@ -2,10 +2,15 @@ #define _SILLY_H #include #include +#include #include "silly_conf.h" #include "silly_malloc.h" #include "silly_socket.h" +#ifndef PATH_MAX +#define PATH_MAX 256 +#endif + #ifndef SILLY_GIT_SHA1 #define SILLY_GIT_SHA1 0 #endif @@ -29,12 +34,14 @@ struct silly_config { int socketaffinity; int workeraffinity; int timeraffinity; + int argc; + char **argv; const char *selfname; - char bootstrap[128]; - char lualib_path[256]; - char lualib_cpath[256]; - char logpath[256]; - char pidfile[256]; + char bootstrap[PATH_MAX]; + char lualib_path[PATH_MAX]; + char lualib_cpath[PATH_MAX]; + char logpath[PATH_MAX]; + char pidfile[PATH_MAX]; }; diff --git a/silly-src/silly_conf.h b/silly-src/silly_conf.h index 8ff8052d..3d7b8c29 100644 --- a/silly-src/silly_conf.h +++ b/silly-src/silly_conf.h @@ -21,6 +21,13 @@ #define LUA_GC_STEP (100) //KiB #endif +#ifndef DEFAULT_LUA_PATH +#define DEFAULT_LUA_PATH "lualib/?.lua" +#endif + +#ifndef DEFAULT_LUA_CPATH +#define DEFAULT_LUA_CPATH "luaclib/?.so" +#endif //(1 << 16) = 65536 diff --git a/silly-src/silly_env.c b/silly-src/silly_env.c deleted file mode 100644 index 8eb8f4e2..00000000 --- a/silly-src/silly_env.c +++ /dev/null @@ -1,48 +0,0 @@ -#include -#include - -#include "silly_malloc.h" - -#include "silly_env.h" - -struct silly_env { - lua_State *L; -}; - -static struct silly_env *E; - -const char *silly_env_get(const char *key) -{ - const char *value; - lua_State *L = E->L; - lua_getglobal(L, key); - value = lua_tostring(L, -1); - lua_pop(L, 1); - return value; -} - -void silly_env_set(const char *key, const char *value) -{ - lua_State *L = E->L; - lua_pushstring(L, value); - lua_setglobal(L, key); - return ; -} - - -int -silly_env_init() -{ - E = (struct silly_env *)silly_malloc(sizeof(*E)); - E->L = luaL_newstate(); - return 0; -} - -void -silly_env_exit() -{ - lua_close(E->L); - silly_free(E); - return ; -} - diff --git a/silly-src/silly_env.h b/silly-src/silly_env.h deleted file mode 100644 index e3c91299..00000000 --- a/silly-src/silly_env.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef _SILLY_ENV_H -#define _SILLY_ENV_H - -int silly_env_init(); -const char *silly_env_get(const char *key); -void silly_env_set(const char *key, const char *value); -void silly_env_exit(); - -#endif - diff --git a/silly-src/silly_run.c b/silly-src/silly_run.c index a4d62731..3e74514f 100644 --- a/silly-src/silly_run.c +++ b/silly-src/silly_run.c @@ -11,7 +11,6 @@ #include "compiler.h" #include "silly_trace.h" #include "silly_log.h" -#include "silly_env.h" #include "silly_malloc.h" #include "silly_timer.h" #include "silly_socket.h" diff --git a/silly-src/silly_worker.c b/silly-src/silly_worker.c index f898f6fe..fe8d6d46 100644 --- a/silly-src/silly_worker.c +++ b/silly-src/silly_worker.c @@ -17,6 +17,8 @@ #define WARNING_THRESHOLD (64) struct silly_worker { + int argc; + char **argv; lua_State *L; uint32_t id; size_t maxmsg; @@ -144,17 +146,34 @@ ltraceback(lua_State *L) return 1; } +static void +fetch_core_start(lua_State *L) +{ + lua_getglobal(L, "require"); + lua_pushstring(L, "sys.core"); + if (lua_pcall(L, 1, 1, 0) != LUA_OK) { + silly_log_error("[worker] require sys.core fail,%s\n", + lua_tostring(L, -1)); + exit(-1); + } + lua_getfield(L, -1, "start"); + lua_remove(L, -2); +} + void silly_worker_start(const struct silly_config *config) { int err; lua_State *L = lua_newstate(lua_alloc, NULL); luaL_openlibs(L); + W->argc = config->argc; + W->argv = config->argv; #if LUA_GC_MODE == LUA_GC_INC lua_gc(L, LUA_GCINC, 0, 0, 0); #else lua_gc(L, LUA_GCGEN, 0, 0); #endif + setlibpath(L, DEFAULT_LUA_PATH, DEFAULT_LUA_CPATH); err = setlibpath(L, config->lualib_path, config->lualib_cpath); if (unlikely(err != 0)) { silly_log_error("[worker] set lua libpath fail,%s\n", @@ -163,8 +182,15 @@ silly_worker_start(const struct silly_config *config) exit(-1); } lua_pushcfunction(L, ltraceback); + fetch_core_start(L); err = luaL_loadfile(L, config->bootstrap); - if (unlikely(err) || unlikely(lua_pcall(L, 0, 0, 1))) { + if (unlikely(err)) { + silly_log_error("[worker] load %s %s\n", + config->bootstrap, lua_tostring(L, -1)); + lua_close(L); + exit(-1); + } + if (unlikely(lua_pcall(L, 1, 0, 1))) { silly_log_error("[worker] call %s %s\n", config->bootstrap, lua_tostring(L, -1)); lua_close(L); @@ -184,6 +210,13 @@ silly_worker_init() return ; } +char ** +silly_worker_args(int *argc) +{ + *argc = W->argc; + return W->argv; +} + void silly_worker_exit() { diff --git a/silly-src/silly_worker.h b/silly-src/silly_worker.h index 4dc5eb21..235117f1 100644 --- a/silly-src/silly_worker.h +++ b/silly-src/silly_worker.h @@ -15,6 +15,8 @@ void silly_worker_dispatch(); uint32_t silly_worker_genid(); size_t silly_worker_msgsize(); +char **silly_worker_args(int *argc); + void silly_worker_callback(void (*callback)(struct lua_State *L, struct silly_message *msg)); #endif diff --git a/test/test.conf b/test/test.conf index b9315397..74716f32 100644 --- a/test/test.conf +++ b/test/test.conf @@ -1,6 +1,2 @@ -daemon = 0 -bootstrap = "test/test.lua" -lualib_path = "test/?.lua;lualib/?.lua" -lualib_cpath = "luaclib/?.so" log_level="debug" -hello={{"world"}} +hello={{"world"}} \ No newline at end of file diff --git a/test/test.lua b/test/test.lua index 619ef60f..f6c4930c 100644 --- a/test/test.lua +++ b/test/test.lua @@ -1,6 +1,6 @@ local core = require "sys.core" local env = require "sys.env" -local testaux = require "testaux" +local testaux = require "test.testaux" local modules = { "testjson", @@ -33,33 +33,20 @@ local gprint = print local function print(...) gprint(M, ...) end - +assert(not env.load("test/test.conf")) assert(env.get("hello.1.1") == "world") env.set("hello.1.1", "hello") assert(env.get("hello.1.1") == "hello") _ENV.print = print - -local ok, res = pcall(core.wait) -assert(not ok) - -core.start(function() - local entry = {} - for k, v in pairs(modules) do - entry[k] = require(v) - end - for k, v in ipairs(modules) do - M = v .. ":" - print("=========start=========") - testaux.module(v) - local ok, err = pcall(entry[k]) - if not ok then - print(err) - core.exit(1) - end - print("======success==========") - end -end) +core.sleep(1000) +for k, v in ipairs(modules) do + M = v .. ":" + print("=========start=========") + testaux.module(v) + dofile("test/" .. v .. ".lua") + print("======success==========") +end diff --git a/test/testaux.lua b/test/testaux.lua index 38e74b58..16dc5430 100644 --- a/test/testaux.lua +++ b/test/testaux.lua @@ -1,4 +1,3 @@ -local core = require "sys.core" local time = require "sys.time" local json = require "sys.json" local c = require "test.aux.c" diff --git a/test/testchannel.lua b/test/testchannel.lua index 3be31a17..62915596 100644 --- a/test/testchannel.lua +++ b/test/testchannel.lua @@ -1,6 +1,6 @@ local core = require "sys.core" local channel = require "sys.sync.channel" -local testaux = require "testaux" +local testaux = require "test.testaux" local c1 = channel.channel() local queue = {} @@ -16,28 +16,27 @@ local function test() core.sleep(1) end +------------- + core.fork(test) -return function() - local a, b, c, d - core.sleep(10) - for i = 1, 3 do - a = "hello" .. i - b = nil - c = "world" .. i - d = nil - table.insert(queue, table.pack(a, b, c, d)) - c1:push2(a, b, c, d) - end - core.sleep(1000) - for i = 4, 6 do - a = "hello" .. i - b = nil - c = "world" .. i - d = nil - table.insert(queue, table.pack(a, b, c, d)) - c1:push2(a, b, c, d) - core.sleep(1) - end +local a, b, c, d +core.sleep(10) +for i = 1, 3 do + a = "hello" .. i + b = nil + c = "world" .. i + d = nil + table.insert(queue, table.pack(a, b, c, d)) + c1:push2(a, b, c, d) end - +core.sleep(1000) +for i = 4, 6 do + a = "hello" .. i + b = nil + c = "world" .. i + d = nil + table.insert(queue, table.pack(a, b, c, d)) + c1:push2(a, b, c, d) + core.sleep(1) +end \ No newline at end of file diff --git a/test/testcrypto.lua b/test/testcrypto.lua index 00912ef7..41e1b6fc 100644 --- a/test/testcrypto.lua +++ b/test/testcrypto.lua @@ -1,50 +1,49 @@ local crypto = require "sys.crypto" -local testaux = require "testaux" +local testaux = require "test.testaux" local P = require "print" -return function() - ---------------------test hamc - local hmac_key = "test" - local hmac_body = "helloworld" - local res = crypto.hmac(hmac_key, hmac_body) - print(string.format("hmac key:%s body:%s, res:", hmac_key, hmac_body)) - P.hex(hmac_body) - P.hex(res) - ---------------------test aes - local aes1_key = "lilei" - local aes1_body = "hello" - local aes2_key = "hanmeimei" - local aes2_body = "1234567891011121314151612345678910111213141516" - local aes1_encode = crypto.aesencode(aes1_key, aes1_body) - local aes2_encode = crypto.aesencode(aes2_key, aes2_body) - local aes1_decode = crypto.aesdecode(aes1_key, aes1_encode) - local aes2_decode = crypto.aesdecode(aes2_key, aes2_encode) - testaux.asserteq(aes1_decode, aes1_body, "aes test decode success") - testaux.asserteq(aes2_decode, aes2_body, "aes test decode success") - local incorrect = crypto.aesdecode(aes2_key, aes1_encode) - testaux.assertneq(incorrect, aes1_body, "aes test decode fail") - local incorrect = crypto.aesdecode("adsafdsafdsafds", aes1_encode) - testaux.assertneq(incorrect, aes1_body, "aes test decode fail") - local incorrect = crypto.aesdecode("fdsafdsafdsafdadfsa", aes2_encode) - testaux.assertneq(incorrect, aes2_body, "aes test decode fail") - --test dirty data defend - crypto.aesdecode("fdsafdsafdsafdsafda", aes2_body) - ----------------------test base64 - local plain = "aGVsbG8sIG15IGZyaWVuZCwgeW91IGtvbncgaSBkb24ndCBnb29kIGF0IGNy\xff\xef" - local e1 = crypto.base64encode(plain) - local e2 = crypto.base64encode(plain, "url") - testaux.assertneq(e1, e2, "base64 encode normal and url safe mode") - local d1 = crypto.base64decode(e1) - local d2 = crypto.base64decode(e2) - testaux.asserteq(d1, plain, "base64 normal test encode/decode success") - testaux.asserteq(d2, plain, "base64 url safe test encode/decode success") - local d3 = crypto.base64decode("====") - testaux.asserteq(d3, "", "base64 decode empty success") - ---------------------test sha256 - local x = crypto.sha256("aGVsbG8sIG15IGZyaWVuZCwgeW91IGtvbncgaSBkb24ndCBnb29kIGF0IGNy") - testaux.asserteq(x, "a3f0f2484b434eb7e3b7dbf89a3b2192c5577a3d51bb65d766a1abedb57aea8c","sha256 hash") - if crypto.digestsign then - local rsa_pub = +---------------------test hamc +local hmac_key = "test" +local hmac_body = "helloworld" +local res = crypto.hmac(hmac_key, hmac_body) +print(string.format("hmac key:%s body:%s, res:", hmac_key, hmac_body)) +P.hex(hmac_body) +P.hex(res) +---------------------test aes +local aes1_key = "lilei" +local aes1_body = "hello" +local aes2_key = "hanmeimei" +local aes2_body = "1234567891011121314151612345678910111213141516" +local aes1_encode = crypto.aesencode(aes1_key, aes1_body) +local aes2_encode = crypto.aesencode(aes2_key, aes2_body) +local aes1_decode = crypto.aesdecode(aes1_key, aes1_encode) +local aes2_decode = crypto.aesdecode(aes2_key, aes2_encode) +testaux.asserteq(aes1_decode, aes1_body, "aes test decode success") +testaux.asserteq(aes2_decode, aes2_body, "aes test decode success") +local incorrect = crypto.aesdecode(aes2_key, aes1_encode) +testaux.assertneq(incorrect, aes1_body, "aes test decode fail") +local incorrect = crypto.aesdecode("adsafdsafdsafds", aes1_encode) +testaux.assertneq(incorrect, aes1_body, "aes test decode fail") +local incorrect = crypto.aesdecode("fdsafdsafdsafdadfsa", aes2_encode) +testaux.assertneq(incorrect, aes2_body, "aes test decode fail") +--test dirty data defend +crypto.aesdecode("fdsafdsafdsafdsafda", aes2_body) +----------------------test base64 +local plain = "aGVsbG8sIG15IGZyaWVuZCwgeW91IGtvbncgaSBkb24ndCBnb29kIGF0IGNy\xff\xef" +local e1 = crypto.base64encode(plain) +local e2 = crypto.base64encode(plain, "url") +testaux.assertneq(e1, e2, "base64 encode normal and url safe mode") +local d1 = crypto.base64decode(e1) +local d2 = crypto.base64decode(e2) +testaux.asserteq(d1, plain, "base64 normal test encode/decode success") +testaux.asserteq(d2, plain, "base64 url safe test encode/decode success") +local d3 = crypto.base64decode("====") +testaux.asserteq(d3, "", "base64 decode empty success") +---------------------test sha256 +local x = crypto.sha256("aGVsbG8sIG15IGZyaWVuZCwgeW91IGtvbncgaSBkb24ndCBnb29kIGF0IGNy") +testaux.asserteq(x, "a3f0f2484b434eb7e3b7dbf89a3b2192c5577a3d51bb65d766a1abedb57aea8c","sha256 hash") +if crypto.digestsign then + local rsa_pub = '-----BEGIN PUBLIC KEY-----\n\z MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2LewXe+8fkojFjTQ+pRq\n\z e29jKHJXcoqEc4D2GDNNdq32GjloPFidVvnlDhOForS5fH5jlqzvcG0mvzAGkmU3\n\z @@ -54,7 +53,7 @@ KBeLP8QuuBd68nRMLZMseMyUz+foVv22FvZ/SdqIeG8IZOSzoN2WJOPy+kYRxdS7\n\z h/2sfHCcNJhxDw4G8tB1oiX+uZclwBMSuOJiu6dD1/IBt4FwzvLZghhgHW7ziJoA\n\z dQIDAQAB\n\z -----END PUBLIC KEY-----' - local rsa_pri = + local rsa_pri = '-----BEGIN RSA PRIVATE KEY-----\n\z MIIEpQIBAAKCAQEA2LewXe+8fkojFjTQ+pRqe29jKHJXcoqEc4D2GDNNdq32Gjlo\n\z PFidVvnlDhOForS5fH5jlqzvcG0mvzAGkmU3Zz5fkrRkiKa+/EyHkiHk+h/VZgCP\n\z @@ -82,43 +81,42 @@ p6sn54cCgYEAi0g89uogqIYsBnTHi9jWDaordsSJdcZyRzq7wlb8sTeBpJEHOdzo\n\z whjjqZDjAW0a+58OPaKcDbTriqug9XvsIs25+7htJysO/yzTOIzTGb1pqJ1ZkeNs\n\z w0W5t0qWE/d60ztwcVCUSINIb680yZexrobYH+tlpsVIdXxjcHPU2o4=\n\z -----END RSA PRIVATE KEY-----' - local ec_secp256k1_pri = + local ec_secp256k1_pri = '-----BEGIN EC PRIVATE KEY-----\n\z MHQCAQEEIHmXrEOL6We4OA9zJ8FOCe/Ed2efH+bi6bfn/0OYGzSqoAcGBSuBBAAK\n\z oUQDQgAESYuWA8FXcHPPPuLj2uAuZRWQwRKEcayXWwXR47rGwMaQzNIhIHxkwbdZ\n\z bd9rXq9Xrhow8xvLeInIGibcx27f1w==\n\z -----END EC PRIVATE KEY-----' - local ec_secp256k1_pub = + local ec_secp256k1_pub = '-----BEGIN PUBLIC KEY-----\n\z MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAESYuWA8FXcHPPPuLj2uAuZRWQwRKEcayX\n\z WwXR47rGwMaQzNIhIHxkwbdZbd9rXq9Xrhow8xvLeInIGibcx27f1w==\n\z -----END PUBLIC KEY-----' - local sig = crypto.digestsign(rsa_pri, "hello", "SHA256") - local ver = crypto.digestverify(rsa_pub, "hello", sig, "SHA256") - testaux.asserteq(testaux.hextostr(sig), - '7169cb0696fc074feb26296b9801833277cd7fce836\z - 6ccdb01f228fd0ee058e063374856d3316df9628774d\z - fbb5aab1b378304423a5f872b8aa0efcd86f08dc05fe\z - b74d4fb885242ef925f3ca79d3925dd19f143d8caf2d\z - ee3eec28ed632491b5aae85b8cc2ebab4682a7f0adbe\z - 33383e1d12a8b9d4ab7d0a005d14c0bc5b3608558211\z - 91271a43e60347be0d791cfaef174e8692aacb0b695e\z - e3c3ed7a2371661c432d7ff0ceb6c457b034829998d9\z - 369f85e72cbfe93705bd2de73b27f06bae5bd9998256\z - bc838a4141149e731a78a96e0b82f413fdb5f41bf45d\z - dcb157e254a6e959275fce64e7e7de25aa5017590e49\z - e06588b8ca903bea4fa4862df0846', "SHA256withRSA sign success") - local ok = crypto.digestverify(rsa_pub, "hello", sig, "sha256") - testaux.asserteq(ok, true, "SHA256withRSA verify success") - local ok = crypto.digestverify(rsa_pub, "hellox", sig, "sha256") - testaux.asserteq(ok, false, "SHA256withRSA verify invalid") - local sig = crypto.digestsign(ec_secp256k1_pri, "hello", "sha256") - local ok = crypto.digestverify(ec_secp256k1_pub, "hello", sig, "sha256") - testaux.asserteq(ok, true, "SHA256withECDSA verify success") - local ok = crypto.digestverify(ec_secp256k1_pub, "hellox", sig, "sha256") - testaux.asserteq(ok, false, "SHA256withECDSA verify invalid") - end - + local sig = crypto.digestsign(rsa_pri, "hello", "SHA256") + local ver = crypto.digestverify(rsa_pub, "hello", sig, "SHA256") + testaux.asserteq(testaux.hextostr(sig), + '7169cb0696fc074feb26296b9801833277cd7fce836\z + 6ccdb01f228fd0ee058e063374856d3316df9628774d\z + fbb5aab1b378304423a5f872b8aa0efcd86f08dc05fe\z + b74d4fb885242ef925f3ca79d3925dd19f143d8caf2d\z + ee3eec28ed632491b5aae85b8cc2ebab4682a7f0adbe\z + 33383e1d12a8b9d4ab7d0a005d14c0bc5b3608558211\z + 91271a43e60347be0d791cfaef174e8692aacb0b695e\z + e3c3ed7a2371661c432d7ff0ceb6c457b034829998d9\z + 369f85e72cbfe93705bd2de73b27f06bae5bd9998256\z + bc838a4141149e731a78a96e0b82f413fdb5f41bf45d\z + dcb157e254a6e959275fce64e7e7de25aa5017590e49\z + e06588b8ca903bea4fa4862df0846', "SHA256withRSA sign success") + local ok = crypto.digestverify(rsa_pub, "hello", sig, "sha256") + testaux.asserteq(ok, true, "SHA256withRSA verify success") + local ok = crypto.digestverify(rsa_pub, "hellox", sig, "sha256") + testaux.asserteq(ok, false, "SHA256withRSA verify invalid") + local sig = crypto.digestsign(ec_secp256k1_pri, "hello", "sha256") + local ok = crypto.digestverify(ec_secp256k1_pub, "hello", sig, "sha256") + testaux.asserteq(ok, true, "SHA256withECDSA verify success") + local ok = crypto.digestverify(ec_secp256k1_pub, "hellox", sig, "sha256") + testaux.asserteq(ok, false, "SHA256withECDSA verify invalid") end + diff --git a/test/testdns.lua b/test/testdns.lua index 8c17bdd2..b265e6c0 100644 --- a/test/testdns.lua +++ b/test/testdns.lua @@ -1,40 +1,38 @@ local core = require "sys.core" local dns = require "sys.dns" -local testaux = require "testaux" +local testaux = require "test.testaux" -return function() - print("testA") - local ip = dns.resolve("test.silly.gotocoding.com", dns.A) - table.sort(ip, function(a, b) - return a < b - end) - testaux.asserteq(#ip, 2, "multi ip") - testaux.asserteq(ip[1], "127.0.0.1", "ip1") - testaux.asserteq(ip[2], "127.0.0.2", "ip2") - print("testAAAA") - local ip = dns.resolve("test.silly.gotocoding.com", dns.AAAA) - table.sort(ip, function(a, b) - return a < b - end) - testaux.asserteq(#ip, 2, "multi ip") - testaux.asserteq(ip[1], "00:00:00:00:00:00:00:01", "ip1") - testaux.asserteq(ip[2], "00:00:00:00:00:00:00:02", "ip2") - print("testSRV") - local ip = dns.resolve("_rpc._tcp.gotocoding.com", dns.SRV) - testaux.asserteq(#ip, 2, "multi srv") - table.sort(ip, function(a, b) - return a.priority < b.priority - end) - testaux.asserteq(ip[1].priority, 0, "srv1") - testaux.asserteq(ip[1].weight, 5, "srv1") - testaux.asserteq(ip[1].port, 5060, "srv1") - testaux.asserteq(ip[1].target, "test2.silly.gotocoding.com", "srv1") - testaux.asserteq(ip[2].priority, 1, "srv1") - testaux.asserteq(ip[2].weight, 6, "srv1") - testaux.asserteq(ip[2].port, 5061, "srv1") - testaux.asserteq(ip[2].target, "test1.silly.gotocoding.com", "srv1") - print("test guess") - testaux.asserteq(dns.isname("wwww.gotocoding.com"), true, "name") - testaux.asserteq(dns.isname("127.0.0.1"), false, "name") -end +print("testA") +local ip = dns.resolve("test.silly.gotocoding.com", dns.A) +table.sort(ip, function(a, b) + return a < b +end) +testaux.asserteq(#ip, 2, "multi ip") +testaux.asserteq(ip[1], "127.0.0.1", "ip1") +testaux.asserteq(ip[2], "127.0.0.2", "ip2") +print("testAAAA") +local ip = dns.resolve("test.silly.gotocoding.com", dns.AAAA) +table.sort(ip, function(a, b) + return a < b +end) +testaux.asserteq(#ip, 2, "multi ip") +testaux.asserteq(ip[1], "00:00:00:00:00:00:00:01", "ip1") +testaux.asserteq(ip[2], "00:00:00:00:00:00:00:02", "ip2") +print("testSRV") +local ip = dns.resolve("_rpc._tcp.gotocoding.com", dns.SRV) +testaux.asserteq(#ip, 2, "multi srv") +table.sort(ip, function(a, b) + return a.priority < b.priority +end) +testaux.asserteq(ip[1].priority, 0, "srv1") +testaux.asserteq(ip[1].weight, 5, "srv1") +testaux.asserteq(ip[1].port, 5060, "srv1") +testaux.asserteq(ip[1].target, "test2.silly.gotocoding.com", "srv1") +testaux.asserteq(ip[2].priority, 1, "srv1") +testaux.asserteq(ip[2].weight, 6, "srv1") +testaux.asserteq(ip[2].port, 5061, "srv1") +testaux.asserteq(ip[2].target, "test1.silly.gotocoding.com", "srv1") +print("test guess") +testaux.asserteq(dns.isname("wwww.gotocoding.com"), true, "name") +testaux.asserteq(dns.isname("127.0.0.1"), false, "name") diff --git a/test/testdom.lua b/test/testdom.lua index 67ae3943..b1d82791 100644 --- a/test/testdom.lua +++ b/test/testdom.lua @@ -1,16 +1,14 @@ local dom = require "http.dom" -local testaux = require "testaux" -return function() - local tree = dom.parse [[ - - 你好世界<>&" - - ]] - testaux.asserteq(tree.name, "hello", "name") - testaux.asserteq(tree.child[1], '你好世界<>&"', 'child') - testaux.asserteq(tree.class[1], "c1", "class c1") - testaux.asserteq(tree.class[2], "c2", "class c2") - testaux.asserteq(tree.attr.foo, "bar", "attr.foo") - testaux.asserteq(tree.attr.class, "c1 c2", "attr.class") -end +local testaux = require "test.testaux" +local tree = dom.parse [[ + + 你好世界<>&" + +]] +testaux.asserteq(tree.name, "hello", "name") +testaux.asserteq(tree.child[1], '你好世界<>&"', 'child') +testaux.asserteq(tree.class[1], "c1", "class c1") +testaux.asserteq(tree.class[2], "c2", "class c2") +testaux.asserteq(tree.attr.foo, "bar", "attr.foo") +testaux.asserteq(tree.attr.class, "c1 c2", "attr.class") diff --git a/test/testexit.lua b/test/testexit.lua index 5ac160b0..ee9cd6d3 100644 --- a/test/testexit.lua +++ b/test/testexit.lua @@ -1,17 +1,16 @@ local core = require "sys.core" -return function() - core.fork(function() - print("-------1") - core.exit(0) - print("exit") - end) - core.fork(function() - print("-------2") - core.exit(1) - end) - core.timeout(0, function() - print("-------3") - core.exit(1) - end) -end + +core.fork(function() + print("-------1") + core.exit(0) + print("exit") +end) +core.fork(function() + print("-------2") + core.exit(1) +end) +core.timeout(0, function() + print("-------3") + core.exit(1) +end) diff --git a/test/testhpack.lua b/test/testhpack.lua index 20dfaabe..82fce091 100644 --- a/test/testhpack.lua +++ b/test/testhpack.lua @@ -1,37 +1,35 @@ local crypto = require "sys.crypto" local hpack = require "http2.hpack" -local testaux = require "testaux" +local testaux = require "test.testaux" local send_hpack = hpack.new(4096) local prefix = crypto.randomkey(200) local unit = 250+32 -return function() - local idx = 0 - local n = 4096//unit - print("n", n) - local prun_count = 0 - repeat - prun_count = prun_count + n - until prun_count > (prun_count // 2) and prun_count > 64 - local data = {} - for i = 1, prun_count + 32 do - idx = idx + 1 - data[i] = prefix .. string.format("%045d", idx) - end - for i = 1, prun_count do - hpack.pack(send_hpack, nil, ":path", data[i]) - end - local evict_count = hpack.dbg_evictcount(send_hpack) - testaux.asserteq(evict_count, (prun_count // n - 1) * n, "hpack queue is full") - hpack.pack(send_hpack, nil, - ":path", data[prun_count-3], - ":path", data[prun_count-2], - ":path", data[prun_count-1], - ":path", data[prun_count+1]) - local evict_count = hpack.dbg_evictcount(send_hpack) - testaux.asserteq(evict_count, 0, "hpack queue is prune") - local id1 = hpack.dbg_stringid(send_hpack, ":path", data[prun_count]) - local id2 = hpack.dbg_stringid(send_hpack, ":path", data[prun_count+1]) - testaux.asserteq((id1 + 1), id2, "hpack prune") +local idx = 0 +local n = 4096//unit +print("n", n) +local prun_count = 0 +repeat + prun_count = prun_count + n +until prun_count > (prun_count // 2) and prun_count > 64 +local data = {} +for i = 1, prun_count + 32 do + idx = idx + 1 + data[i] = prefix .. string.format("%045d", idx) end +for i = 1, prun_count do + hpack.pack(send_hpack, nil, ":path", data[i]) +end +local evict_count = hpack.dbg_evictcount(send_hpack) +testaux.asserteq(evict_count, (prun_count // n - 1) * n, "hpack queue is full") +hpack.pack(send_hpack, nil, + ":path", data[prun_count-3], + ":path", data[prun_count-2], + ":path", data[prun_count-1], + ":path", data[prun_count+1]) +local evict_count = hpack.dbg_evictcount(send_hpack) +testaux.asserteq(evict_count, 0, "hpack queue is prune") +local id1 = hpack.dbg_stringid(send_hpack, ":path", data[prun_count]) +local id2 = hpack.dbg_stringid(send_hpack, ":path", data[prun_count+1]) +testaux.asserteq((id1 + 1), id2, "hpack prune") diff --git a/test/testhttp.lua b/test/testhttp.lua index 88dab39f..157a646a 100644 --- a/test/testhttp.lua +++ b/test/testhttp.lua @@ -1,7 +1,6 @@ local json = require "sys.json" -local tcp = require "sys.net.tcp" local http = require "http" -local testaux = require "testaux" +local testaux = require "test.testaux" local dispatch = {} dispatch["/"] = function(stream) @@ -43,53 +42,51 @@ dispatch["/upload"] = function(stream) stream:close(body) end -return function() - local handler = function(stream) - local header = stream.header - local body = stream:readall() - print("handler", stream.path, json.encode(header), body, stream.form and json.encode(stream.form)) - local c = dispatch[stream.path] - if c then - c(stream) - else - local txt = "404 Page Not Found" - stream:respond(200, { - ["Content-Type"] = "text/plain", - ['content-length'] = #txt, - }) - stream:close(txt) - end +local handler = function(stream) + local header = stream.header + local body = stream:readall() + print("handler", stream.path, json.encode(header), body, stream.form and json.encode(stream.form)) + local c = dispatch[stream.path] + if c then + c(stream) + else + local txt = "404 Page Not Found" + stream:respond(200, { + ["Content-Type"] = "text/plain", + ['content-length'] = #txt, + }) + stream:close(txt) end - local fd1 = http.listen { - port = ":8080", - handler = handler, - } - assert(fd1, "listen 8080 fail") - local fd2 = http.listen { - tls = true, - port = ":8081", - certs = { - { - cert = "./test/cert.pem", - cert_key = "./test/key.pem", - }, +end +local fd1 = http.listen { + port = ":8080", + handler = handler, +} +assert(fd1, "listen 8080 fail") +local fd2 = http.listen { + tls = true, + port = ":8081", + certs = { + { + cert = "./test/cert.pem", + cert_key = "./test/key.pem", }, - handler = handler, - } - assert(fd2, "listen 8081 fail") - local ack, err = http.POST("http://localhost:8080/upload", - {["Content-Type"] = "application/x-www-form-urlencoded"}, - "Hello=findstr") - if not ack then - print("ERROR", err) - return - end - print(ack.status, json.encode(ack.header), ack.body) - local res = http.GET("https://localhost:8081/download") - print(json.encode(res)) - testaux.asserteq(res.body, "findstr", "http GET data validate") - testaux.asserteq(res.status, 200, "http GET status validate") - local res = http.GET("http://www.baidu.com") - testaux.asserteq(res.status, 200, "http GET status validate") + }, + handler = handler, +} +assert(fd2, "listen 8081 fail") +local ack, err = http.POST("http://localhost:8080/upload", + {["Content-Type"] = "application/x-www-form-urlencoded"}, + "Hello=findstr") +if not ack then + print("ERROR", err) + return end +print(ack.status, json.encode(ack.header), ack.body) +local res = http.GET("https://localhost:8081/download") +print(json.encode(res)) +testaux.asserteq(res.body, "findstr", "http GET data validate") +testaux.asserteq(res.status, 200, "http GET status validate") +local res = http.GET("http://www.baidu.com") +testaux.asserteq(res.status, 200, "http GET status validate") diff --git a/test/testhttp2.lua b/test/testhttp2.lua index 119ef8bd..a3a480ca 100644 --- a/test/testhttp2.lua +++ b/test/testhttp2.lua @@ -3,77 +3,75 @@ local core = require "sys.core" local crypto = require "sys.crypto" local json = require "sys.json" local waitgroup = require "sys.sync.waitgroup" -local testaux = require "testaux" +local testaux = require "test.testaux" local f = io.open("./a.txt", "w") -return function() - if not crypto.digestsign then - print("not enable openssl") - return - end - http.listen { - tls = true, - port = ":8082", - alpnprotos = { - "h2", - }, - certs = { - { - cert = "test/cert.pem", - cert_key = "test/key.pem", - } - }, - handler = function(stream) - core.sleep(math.random(1, 300)) - local status, header = stream:readheader() - testaux.asserteq(stream.method, "POST", "http2.server method") - testaux.asserteq(stream.path, "/test", "http2.server path") - testaux.asserteq(header['hello'], "world", "http2.server header") - local body = stream:readall() - testaux.asserteq(body, "http2", "http2 body") - stream:respond(200, {["foo"] = header['foo']}) - stream:close("http2") - end - } - local n = 0 - print("test http2 client") - --[[ disable test http2 client for temporary - local wg = waitgroup:create() - for i = 1, 2000 do - wg:fork(function() - local key = crypto.randomkey(1028) - local status, header, body = http2.POST("https://http2.golang.org/reqinfo", { - ['hello'] = 'world', - ['foo'] = key, - }, "http2") - n = n + 1 - print("test", n) - testaux.asserteq(status, 200, "http2.client status") - testaux.assertneq(body:find("Foo: " .. key), nil, "http2.header key") - testaux.assertneq(body:find("Hello: world"), nil, "http2.header key") - end) +if not crypto.digestsign then + print("not enable openssl") + return +end +http.listen { + tls = true, + port = ":8082", + alpnprotos = { + "h2", + }, + certs = { + { + cert = "test/cert.pem", + cert_key = "test/key.pem", + } + }, + handler = function(stream) + core.sleep(math.random(1, 300)) + local status, header = stream:readheader() + testaux.asserteq(stream.method, "POST", "http2.server method") + testaux.asserteq(stream.path, "/test", "http2.server path") + testaux.asserteq(header['hello'], "world", "http2.server header") + local body = stream:readall() + testaux.asserteq(body, "http2", "http2 body") + stream:respond(200, {["foo"] = header['foo']}) + stream:close("http2") end - wg:wait() - ]] - local ack, err = http.GET("https://http2cdn.cdnsun.com/") - testaux.asserteq(ack.status, 200, "http2.client status") - testaux.asserteq(ack.body, "Hello\n", "http2.body") +} +local n = 0 +print("test http2 client") +--[[ disable test http2 client for temporary +local wg = waitgroup:create() +for i = 1, 2000 do + wg:fork(function() + local key = crypto.randomkey(1028) + local status, header, body = http2.POST("https://http2.golang.org/reqinfo", { + ['hello'] = 'world', + ['foo'] = key, + }, "http2") + n = n + 1 + print("test", n) + testaux.asserteq(status, 200, "http2.client status") + testaux.assertneq(body:find("Foo: " .. key), nil, "http2.header key") + testaux.assertneq(body:find("Hello: world"), nil, "http2.header key") + end) +end +wg:wait() +]] +local ack, err = http.GET("https://http2cdn.cdnsun.com/") +testaux.asserteq(ack.status, 200, "http2.client status") +testaux.asserteq(ack.body, "Hello\n", "http2.body") - print("test http2 server") - local wg = waitgroup:create() - for i = 1, 2000 do - wg:fork(function() - local key = crypto.randomkey(1028) - local ack, err = http.POST("https://localhost:8082/test", { - ['hello'] = 'world', - ['foo'] = key, - }, "http2") - testaux.asserteq(ack.status, 200, "http2.client status") - testaux.asserteq(ack.header['foo'], key, "http2.client header") - testaux.asserteq(ack.body, 'http2', "http2.client body") - end) - end - wg:wait() +print("test http2 server") +local wg = waitgroup:create() +for i = 1, 2000 do + wg:fork(function() + local key = crypto.randomkey(1028) + local ack, err = http.POST("https://localhost:8082/test", { + ['hello'] = 'world', + ['foo'] = key, + }, "http2") + testaux.asserteq(ack.status, 200, "http2.client status") + testaux.asserteq(ack.header['foo'], key, "http2.client header") + testaux.asserteq(ack.body, 'http2', "http2.client body") + end) end +wg:wait() diff --git a/test/testjson.lua b/test/testjson.lua index 33c82bc9..8eaedf3f 100644 --- a/test/testjson.lua +++ b/test/testjson.lua @@ -1,5 +1,5 @@ local json = require "sys.json" -local testaux = require "testaux" +local testaux = require "test.testaux" local obj = { [1] = { @@ -17,19 +17,17 @@ local obj = { }, } -return function() - local str = json.encode(obj) - print('encode:', str) - local res = json.decode(str) - print('decode:', res) - for i = 1, #obj do - local s = obj[i] - local d = res[i] - for k, v in pairs(s) do - testaux.asserteq(s[k], d[k], - string.format("obj[%s].%s == obj[%s].%s", - i, k, i, k)) - end +local str = json.encode(obj) +print('encode:', str) +local res = json.decode(str) +print('decode:', res) +for i = 1, #obj do + local s = obj[i] + local d = res[i] + for k, v in pairs(s) do + testaux.asserteq(s[k], d[k], + string.format("obj[%s].%s == obj[%s].%s", + i, k, i, k)) end end diff --git a/test/testmulticast.lua b/test/testmulticast.lua index 3c733036..7803b50a 100644 --- a/test/testmulticast.lua +++ b/test/testmulticast.lua @@ -1,7 +1,6 @@ local core = require "sys.core" -local testaux = require "testaux" +local testaux = require "test.testaux" local msg = require "cluster.msg" -local np = require "sys.netpacket" local zproto = require "zproto" local logic = zproto:parse [[ test 0xff { @@ -15,49 +14,47 @@ local accept = {} local client = {} local recv = {} -return function() - server = msg.listen { +server = msg.listen { + proto = logic, + addr = "127.0.0.1:8002", + accept = function(fd, addr) + accept[#accept + 1] = fd + --print("accept", addr) + end, + close = function(fd, errno) + --print("close", fd, errno) + end, + data = function(fd, cmd, obj) + local m, sz = server:multipack(cmd, obj, #accept) + for _, fd in pairs(accept) do + local ok = server:multicast(fd, m, sz) + testaux.assertneq(fd, nil, "multicast test send") + testaux.asserteq(ok, true, "multicast test send") + end + end +} +testaux.asserteq(not not server, true, "multicast test listen") + +local inst +for i = 1, 10 do + inst = msg.connect { proto = logic, addr = "127.0.0.1:8002", - accept = function(fd, addr) - accept[#accept + 1] = fd - --print("accept", addr) + data = function(fd, cmd, obj) + testaux.asserteq(obj.str, "testmulticast", "muticast validate data") + recv[i] = true end, close = function(fd, errno) - --print("close", fd, errno) - end, - data = function(fd, cmd, obj) - local m, sz = server:multipack(cmd, obj, #accept) - for _, fd in pairs(accept) do - local ok = server:multicast(fd, m, sz) - testaux.assertneq(fd, nil, "multicast test send") - testaux.asserteq(ok, true, "multicast test send") - end + end } - testaux.asserteq(not not server, true, "multicast test listen") - - local inst - for i = 1, 10 do - inst = msg.connect { - proto = logic, - addr = "127.0.0.1:8002", - data = function(fd, cmd, obj) - testaux.asserteq(obj.str, "testmulticast", "muticast validate data") - recv[i] = true - end, - close = function(fd, errno) - - end - } - client[i] = inst - end - inst:send("test", {str = "testmulticast"}) - core.sleep(1000) - for k, _ in pairs(client) do - testaux.asserteq(recv[k], true, "multicast recv count validate") - end - server:stop() + client[i] = inst +end +inst:send("test", {str = "testmulticast"}) +core.sleep(1000) +for k, _ in pairs(client) do + testaux.asserteq(recv[k], true, "multicast recv count validate") end +server:stop() diff --git a/test/testmutex.lua b/test/testmutex.lua index 1e39fad4..d0f84546 100644 --- a/test/testmutex.lua +++ b/test/testmutex.lua @@ -1,5 +1,5 @@ local core = require "sys.core" -local testaux = require "testaux" +local testaux = require "test.testaux" local waitgroup = require "sys.sync.waitgroup" local mutex = require "sys.sync.mutex" @@ -156,12 +156,10 @@ end -return function() - testcase1() - testcase2() - testcase3() - testcase4() - testcase5() - testcase6() -end +testcase1() +testcase2() +testcase3() +testcase4() +testcase5() +testcase6() diff --git a/test/testmysql.lua b/test/testmysql.lua index 35650153..4c550731 100644 --- a/test/testmysql.lua +++ b/test/testmysql.lua @@ -1,15 +1,11 @@ -local core = require "sys.core" local mysql = require "sys.db.mysql" -local testaux = require "testaux" -local json = require "sys.json" +local testaux = require "test.testaux" -return function() - local db = mysql.connect{ - addr ="127.0.0.1:3306", - user="root", - password="root", - } - local status, res = db:query("select 0;") - testaux.asserteq(res[1]["0"], 0, "select 0;") -end +local db = mysql.connect{ + addr ="127.0.0.1:3306", + user="root", + password="root", +} +local status, res = db:query("select 0;") +testaux.asserteq(res[1]["0"], 0, "select 0;") diff --git a/test/testnetpacket.lua b/test/testnetpacket.lua index 66a8f558..b2977820 100644 --- a/test/testnetpacket.lua +++ b/test/testnetpacket.lua @@ -1,7 +1,6 @@ local core = require "sys.core" local np = require "sys.netpacket" -local testaux = require "testaux" -local P = require "print" +local testaux = require "test.testaux" local BUFF @@ -145,16 +144,14 @@ local function testexpand() end end -return function() - collectgarbage("collect") - BUFF = np.create() - testhashconflict_part1() - testpacket(justpush) - testpacket(randompush) - testclear() - testexpand() - testhashconflict_part2() - BUFF = nil - collectgarbage("collect") -end +collectgarbage("collect") +BUFF = np.create() +testhashconflict_part1() +testpacket(justpush) +testpacket(randompush) +testclear() +testexpand() +testhashconflict_part2() +BUFF = nil +collectgarbage("collect") diff --git a/test/testnetstream.lua b/test/testnetstream.lua index 8a23a24f..5051434c 100644 --- a/test/testnetstream.lua +++ b/test/testnetstream.lua @@ -1,5 +1,5 @@ local ns = require "sys.netstream" -local testaux = require "testaux" +local testaux = require "test.testaux" local function build(n) local tbl = {} @@ -9,39 +9,37 @@ local function build(n) return table.concat(tbl) end -return function() - local sb - local fd = 3 - sb = ns.new(fd) - print("push", "hello") - ns.tpush(sb, fd, "hello") - print("push", "a") - ns.tpush(sb, fd, "a") - print("push", "\\rworld\\ntail\\r\\n") - ns.tpush(sb, fd, "\rworld\ntail\r\n") - - local data = ns.read(sb, 1) - testaux.asserteq(data, "h", "data == 'a'") - - data = ns.readline(sb, "a\r") - testaux.asserteq(data, "elloa\r", 'ns.readline(sb, "a\r")') - - data = ns.readline(sb, "\n") - testaux.asserteq(data, "world\n", 'ns.readline(sb, "\n")') - - data = ns.readline(sb, "\r\n") - testaux.asserteq(data, "tail\r\n", 'ns.readline(sb, "\r\n")') - - local push = {} - for i = 1, 2 * 1024 * 64 do - local dat = build(math.random(1, 33)) - push[#push + 1] = dat - ns.tpush(sb, fd, dat) - end - local r1 = ns.read(sb, 1) - local r2 = ns.read(sb, math.random(1, 1024)) - local r3 = ns.readall(sb) - testaux.asserteq(table.concat(push), r1 .. r2 .. r3, - "table.concat(push) == r1 .. r2 .. r3") +local sb +local fd = 3 +sb = ns.new(fd) +print("push", "hello") +ns.tpush(sb, fd, "hello") +print("push", "a") +ns.tpush(sb, fd, "a") +print("push", "\\rworld\\ntail\\r\\n") +ns.tpush(sb, fd, "\rworld\ntail\r\n") + +local data = ns.read(sb, 1) +testaux.asserteq(data, "h", "data == 'a'") + +data = ns.readline(sb, "a\r") +testaux.asserteq(data, "elloa\r", 'ns.readline(sb, "a\r")') + +data = ns.readline(sb, "\n") +testaux.asserteq(data, "world\n", 'ns.readline(sb, "\n")') + +data = ns.readline(sb, "\r\n") +testaux.asserteq(data, "tail\r\n", 'ns.readline(sb, "\r\n")') + +local push = {} +for i = 1, 2 * 1024 * 64 do + local dat = build(math.random(1, 33)) + push[#push + 1] = dat + ns.tpush(sb, fd, dat) end +local r1 = ns.read(sb, 1) +local r2 = ns.read(sb, math.random(1, 1024)) +local r3 = ns.readall(sb) +testaux.asserteq(table.concat(push), r1 .. r2 .. r3, + "table.concat(push) == r1 .. r2 .. r3") diff --git a/test/testpatch.lua b/test/testpatch.lua index 728a82d0..fb55bd89 100644 --- a/test/testpatch.lua +++ b/test/testpatch.lua @@ -1,6 +1,6 @@ local core = require "sys.core" local patch = require "sys.patch" -local testaux = require "testaux" +local testaux = require "test.testaux" local function fix(P, ENV, M1, M2, skip) local up1 = P:collectupval(M1) local up2 = P:collectupval(M2) @@ -273,11 +273,9 @@ local function case5(P) testaux.asserteq(_ENV.bar, 4, "global variable") end -return function() - local P = patch:create() - case1(P) - case2(P) - case3(P) - case4(P) - case5(P) -end +local P = patch:create() +case1(P) +case2(P) +case3(P) +case4(P) +case5(P) diff --git a/test/testredis.lua b/test/testredis.lua index f2fe81ec..bf85dc1e 100644 --- a/test/testredis.lua +++ b/test/testredis.lua @@ -1,6 +1,6 @@ local core = require "sys.core" local redis = require "sys.db.redis" -local testaux = require "testaux" +local testaux = require "test.testaux" local function asserteq(cmd, expect_success, expect_value, success, value) if type(value) == "table" then @@ -42,32 +42,30 @@ local function testbasic() asserteq("HGETALL hash", true, "k1", db:hgetall("hash")) end -return function() - local testcount = 1024 - local finish = 0 - local idx = 0 - print("-----test basic-----") - testbasic() - print("-----test cocurrent:", testcount) - db:del("foo") - for i = 1, testcount do - core.fork(function() - idx = idx + 1 - local id = idx - local ok, get = db:incr("foo") - core.sleep(math.random(1, 100)) - testaux.asserteq(ok, true, "INCR foo") - testaux.asserteq(id, get, "INCR foo") - finish = finish + 1 - print("----finish:", finish) - end) - core.sleep(math.random(1, 10)) - end - while true do - if finish == testcount then - break - end - core.sleep(500) +local testcount = 1024 +local finish = 0 +local idx = 0 +print("-----test basic-----") +testbasic() +print("-----test cocurrent:", testcount) +db:del("foo") +for i = 1, testcount do + core.fork(function() + idx = idx + 1 + local id = idx + local ok, get = db:incr("foo") + core.sleep(math.random(1, 100)) + testaux.asserteq(ok, true, "INCR foo") + testaux.asserteq(id, get, "INCR foo") + finish = finish + 1 + print("----finish:", finish) + end) + core.sleep(math.random(1, 10)) +end +while true do + if finish == testcount then + break end + core.sleep(500) end diff --git a/test/testrpc.lua b/test/testrpc.lua index 0c096510..7a5239bd 100644 --- a/test/testrpc.lua +++ b/test/testrpc.lua @@ -2,7 +2,7 @@ local core = require "sys.core" local waitgroup = require "sys.sync.waitgroup" local rpc = require "cluster.rpc" local crypto = require "sys.crypto" -local testaux = require "testaux" +local testaux = require "test.testaux" local zproto = require "zproto" local logic = zproto:parse [[ @@ -120,9 +120,6 @@ local function client_part() print("case three finish") end -return function() - client_part() - client:close() - server:close() -end - +client_part() +client:close() +server:close() \ No newline at end of file diff --git a/test/testssl.lua b/test/testssl.lua index 3b996960..007f02da 100644 --- a/test/testssl.lua +++ b/test/testssl.lua @@ -1,19 +1,16 @@ -local core = require "sys.core" -local ssl = require "sys.tls" - -return function() - local fd = ssl.connect("14.215.177.37:443") - print("connect", fd) - ssl.write(fd, "GET https://www.baidu.com/ HTTP/1.1\r\n" .. - "User-Agent: Fiddler\r\n" .. - "Host: www.baidu.com\r\n\r\n") - local d - while not d do - d = ssl.readline(fd) - print(d) - end - print("testssl ok") +local dns = require "sys.dns" +local ip = dns.lookup("www.baidu.com", dns.A) +local fd = ssl.connect(ip..":443") +print("connect", fd) +ssl.write(fd, "GET https://www.baidu.com/ HTTP/1.1\r\n" .. + "User-Agent: Fiddler\r\n" .. + "Host: www.baidu.com\r\n\r\n") +local d +while not d do + d = ssl.readline(fd) + print(d) end +print("testssl ok") diff --git a/test/testtcp.lua b/test/testtcp.lua index 3ffcddf2..97c2b0ab 100644 --- a/test/testtcp.lua +++ b/test/testtcp.lua @@ -4,7 +4,7 @@ local json = require "sys.json" local tcp = require "sys.net.tcp" local tls = require "sys.tls" local crypto = require "sys.crypto" -local testaux = require "testaux" +local testaux = require "test.testaux" local IO local listen_cb local listenfd = tcp.listen(":10001", function(fd, addr) @@ -441,30 +441,29 @@ local function netstat() } end -return function() - local info1 = netstat() - print(json.encode(info1)) - testaux.module("socet") - test_limit(":10001") - core.sleep(100) - local info2 = netstat() - print(json.encode(info2)) - testaux.asserteq(info1, info2, "check limit clear") - IO = tcp - testaux.module("socet") - test_read(":10001") - test_close(":10001") - core.sleep(100) - local info3 = netstat() - testaux.asserteq(info1, info3, "check tcp clear") +core.sleep(1000) +local info1 = netstat() +print(json.encode(info1)) +testaux.module("socet") +test_limit(":10001") +core.sleep(100) +local info2 = netstat() +print(json.encode(info2)) +testaux.asserteq(info1, info2, "check limit clear") +IO = tcp +testaux.module("socet") +test_read(":10001") +test_close(":10001") +core.sleep(100) +local info3 = netstat() +testaux.asserteq(info1, info3, "check tcp clear") - IO = tls - testaux.module("tls") - IO.limit = function(fd, limit) end - test_read(":10002") - test_close(":10002") - core.sleep(100) - local info4 = netstat() - testaux.asserteq(info1, info4, "check tls clear") -end +IO = tls +testaux.module("tls") +IO.limit = function(fd, limit) end +test_read(":10002") +test_close(":10002") +core.sleep(100) +local info4 = netstat() +testaux.asserteq(info1, info4, "check tls clear") diff --git a/test/testtimer.lua b/test/testtimer.lua index 43b92d34..495aed62 100644 --- a/test/testtimer.lua +++ b/test/testtimer.lua @@ -1,6 +1,6 @@ local core = require "sys.core" local time = require "sys.time" -local testaux = require "testaux" +local testaux = require "test.testaux" local context = {} local total = 30 @@ -53,9 +53,7 @@ local function test_cancel() testaux.assertneq(key, "bar", "test timer cancel") end -return function() - test_timer() - test_userdata() - test_cancel() -end +test_timer() +test_userdata() +test_cancel() diff --git a/test/testudp.lua b/test/testudp.lua index 444dfd38..652004e8 100644 --- a/test/testudp.lua +++ b/test/testudp.lua @@ -1,7 +1,7 @@ local core = require "sys.core" local udp = require "sys.net.udp" local crypto = require "sys.crypto" -local testaux = require "testaux" +local testaux = require "test.testaux" local server_fd local client_fd @@ -18,16 +18,14 @@ local function udp_client(data, addr) testaux.asserteq(data, queue[recvidx], "udp data validate") end -return function() - server_fd = udp.bind(":8989", udp_server) - testaux.asserteq(not server_fd, false, "upd bind") - client_fd = udp.connect("127.0.0.1:8989", udp_client) - testaux.asserteq(not client_fd, false, "udp bridge") - for i = 1, 20 do - local d = crypto.randomkey(8) - queue[i] = d - udp.send(client_fd, d) - core.sleep(150) - end +server_fd = udp.bind(":8989", udp_server) +testaux.asserteq(not server_fd, false, "upd bind") +client_fd = udp.connect("127.0.0.1:8989", udp_client) +testaux.asserteq(not client_fd, false, "udp bridge") +for i = 1, 20 do + local d = crypto.randomkey(8) + queue[i] = d + udp.send(client_fd, d) + core.sleep(150) end diff --git a/test/testwaitgroup.lua b/test/testwaitgroup.lua index 4856b79f..fb4deed6 100644 --- a/test/testwaitgroup.lua +++ b/test/testwaitgroup.lua @@ -1,18 +1,16 @@ local core = require "sys.core" -local testaux = require "testaux" +local testaux = require "test.testaux" local waitgroup = require "sys.sync.waitgroup" -return function() - local wg = waitgroup:create() - local count = 0 - for i = 1, 5 do - wg:fork(function() - core.sleep(500) - count = count + 1 - assert(count ~= 5, "crash the last coroutine") - end) - end - wg:wait() - testaux.asserteq(count, 5, "wait") +local wg = waitgroup:create() +local count = 0 +for i = 1, 5 do + wg:fork(function() + core.sleep(500) + count = count + 1 + assert(count ~= 5, "crash the last coroutine") + end) end +wg:wait() +testaux.asserteq(count, 5, "wait") diff --git a/test/testwakeup.lua b/test/testwakeup.lua index 44682e05..f076b32e 100644 --- a/test/testwakeup.lua +++ b/test/testwakeup.lua @@ -1,8 +1,7 @@ local core = require "sys.core" -local testaux = require "testaux" +local testaux = require "test.testaux" local fork_queue = {} local nxt = 1 -local last = nil local function wrap(str, i) return function(x) @@ -14,27 +13,25 @@ local function wrap(str, i) end end -return function() - for i = 1, 50 do - fork_queue[i] = core.fork(wrap("test" .. i, i)) - end - core.sleep(0) - local wakeup = core.wakeup - for i = 1, 50 do - wakeup(fork_queue[i], i) - local ok, err = pcall(wakeup, fork_queue[i], i) - assert(not ok) - end - core.sleep(100) - for i = 51, 100 do - fork_queue[i] = core.fork(wrap("test" .. i, i)) - end - core.sleep(0) - local wakeup = core.wakeup - for i = 51, 100 do - wakeup(fork_queue[i], i) - end - core.sleep(0) - testaux.asserteq(nxt, 101, "wakeup validate sequence") +for i = 1, 50 do + fork_queue[i] = core.fork(wrap("test" .. i, i)) +end +core.sleep(0) +local wakeup = core.wakeup +for i = 1, 50 do + wakeup(fork_queue[i], i) + local ok, err = pcall(wakeup, fork_queue[i], i) + assert(not ok) +end +core.sleep(100) +for i = 51, 100 do + fork_queue[i] = core.fork(wrap("test" .. i, i)) +end +core.sleep(0) +local wakeup = core.wakeup +for i = 51, 100 do + wakeup(fork_queue[i], i) end +core.sleep(0) +testaux.asserteq(nxt, 101, "wakeup validate sequence") diff --git a/test/testwebsocket.lua b/test/testwebsocket.lua index 159b9086..ef270dc8 100644 --- a/test/testwebsocket.lua +++ b/test/testwebsocket.lua @@ -1,7 +1,6 @@ local core = require "sys.core" local websocket = require "http.websocket" -local testaux = require "testaux" -local listen_cb +local testaux = require "test.testaux" local handler = function(sock) local dat, typ = sock:read() @@ -46,10 +45,8 @@ local function client(scheme, port) testaux.asserteq(ok, false, "close dummy") end -return function() - testaux.module("socket") - client("ws", ":10003") - testaux.module("tls") - client("wss", ":10004") -end +testaux.module("socket") +client("ws", ":10003") +testaux.module("tls") +client("wss", ":10004")