From 4df12ebbec2f5b1f9989a1f37925f8ef13d73749 Mon Sep 17 00:00:00 2001 From: weli Date: Tue, 8 Mar 2022 20:08:50 +0800 Subject: [PATCH] add coroutine --- conf/local_macos/test_co.conf | 13 +++++++++++++ cp_scripts_local_linux.sh | 0 cp_scripts_local_macos.sh | 4 ++++ test_scripts/co.lua | 13 +++++++++++++ 4 files changed, 30 insertions(+) create mode 100644 conf/local_macos/test_co.conf mode change 100644 => 100755 cp_scripts_local_linux.sh create mode 100644 test_scripts/co.lua diff --git a/conf/local_macos/test_co.conf b/conf/local_macos/test_co.conf new file mode 100644 index 0000000..8bd1797 --- /dev/null +++ b/conf/local_macos/test_co.conf @@ -0,0 +1,13 @@ +server { +# https://serverfault.com/questions/798734/use-variable-for-server-name-in-nginx# +# server_name $servername; + server_name "test.localhost"; + listen 8088; + + set $resty_loc "/usr/local/opt/openresty"; + + location /api/ngx/test/co { + content_by_lua_file $resty_loc/nginx/scripts/tests/co.lua; + } +} + diff --git a/cp_scripts_local_linux.sh b/cp_scripts_local_linux.sh old mode 100644 new mode 100755 diff --git a/cp_scripts_local_macos.sh b/cp_scripts_local_macos.sh index 74fba66..87ff731 100755 --- a/cp_scripts_local_macos.sh +++ b/cp_scripts_local_macos.sh @@ -22,6 +22,10 @@ echo $openresty_lua_scripts_dir mkdir -p $openresty_lua_scripts_dir # -- Copy scripts file cp scripts/*.lua $openresty_lua_scripts_dir +# -- Copy tests +mkdir -p $openresty_lua_scripts_dir/tests/ +cp test_scripts/*.lua $openresty_lua_scripts_dir/tests/ + # -- Replace scripts path in file sed -i -e "s||$openresty_lua_scripts_dir|" \ "$openresty_lua_scripts_dir"/combine.lua \ diff --git a/test_scripts/co.lua b/test_scripts/co.lua new file mode 100644 index 0000000..8bfa594 --- /dev/null +++ b/test_scripts/co.lua @@ -0,0 +1,13 @@ +local function sleep(n) + os.execute("sleep " .. tonumber(n)) +end + +local co = coroutine.create(function(x) + sleep(3) + print("co -> ", x) + end) + +coroutine.resume(co, 42) + +ngx.say("main") +