|
| 1 | +use t::APISIX_NGINX 'no_plan'; |
| 2 | + |
| 3 | + |
| 4 | +add_block_preprocessor(sub { |
| 5 | + my ($block) = @_; |
| 6 | + |
| 7 | + if (!$block->request) { |
| 8 | + $block->set_value("request", "GET /t"); |
| 9 | + } |
| 10 | +}); |
| 11 | + |
| 12 | +run_tests; |
| 13 | + |
| 14 | +__DATA__ |
| 15 | +
|
| 16 | +=== TEST 1: check pipe spawn arguments |
| 17 | +--- config |
| 18 | + location = /t { |
| 19 | + content_by_lua_block { |
| 20 | + local ngx_pipe = require "ngx.pipe" |
| 21 | +
|
| 22 | + local function check_error(...) |
| 23 | + local data, err = pcall(...) |
| 24 | + if not data then |
| 25 | + ngx.say(err) |
| 26 | + else |
| 27 | + ngx.say('ok') |
| 28 | + end |
| 29 | + end |
| 30 | +
|
| 31 | + check_error(ngx_pipe.spawn, nil) |
| 32 | + check_error(ngx_pipe.spawn, {}) |
| 33 | + check_error(ngx_pipe.spawn, {"ls"}, {buffer_size = 0}) |
| 34 | + check_error(ngx_pipe.spawn, {"ls"}, {buffer_size = 0.5}) |
| 35 | + check_error(ngx_pipe.spawn, {"ls"}, {buffer_size = "1"}) |
| 36 | + check_error(ngx_pipe.spawn, {"ls"}, {buffer_size = true}) |
| 37 | + } |
| 38 | + } |
| 39 | +--- request |
| 40 | +--- response_body |
| 41 | +bad args arg: table expected, got nil |
| 42 | +bad args arg: non-empty table expected |
| 43 | +bad buffer_size option |
| 44 | +bad buffer_size option |
| 45 | +ok |
| 46 | +bad buffer_size option |
| 47 | +
|
| 48 | +
|
| 49 | +
|
| 50 | +=== TEST 2: spawn process, with buffer_size option |
| 51 | +--- config |
| 52 | + location = /t { |
| 53 | + content_by_lua_block { |
| 54 | + local ngx_pipe = require "ngx.pipe" |
| 55 | + local proc, err = ngx_pipe.spawn({"ls"}, {buffer_size = 256}) |
| 56 | + if not proc then |
| 57 | + ngx.say(err) |
| 58 | + else |
| 59 | + ngx.say('ok') |
| 60 | + end |
| 61 | + } |
| 62 | + } |
| 63 | +--- response_body |
| 64 | +ok |
| 65 | +--- error_log eval |
| 66 | +qr/lua pipe spawn process:[0-9A-F]+ pid:\d+ merge_stderr:0 buffer_size:256/ |
| 67 | +--- no_error_log |
| 68 | +[error] |
| 69 | +
|
| 70 | +
|
| 71 | +
|
| 72 | +=== TEST 3: ensure process is destroyed in GC |
| 73 | +--- config |
| 74 | + location = /t { |
| 75 | + content_by_lua_block { |
| 76 | + local ngx_pipe = require "ngx.pipe" |
| 77 | + do |
| 78 | + local proc, err = ngx_pipe.spawn({"ls", "-l"}) |
| 79 | + if not proc then |
| 80 | + ngx.say(err) |
| 81 | + return |
| 82 | + end |
| 83 | + end |
| 84 | +
|
| 85 | + collectgarbage() |
| 86 | + ngx.say("ok") |
| 87 | + } |
| 88 | + } |
| 89 | +--- response_body |
| 90 | +ok |
| 91 | +--- no_error_log |
| 92 | +[error] |
| 93 | +--- error_log |
| 94 | +lua pipe destroy process: |
| 95 | +
|
| 96 | +
|
| 97 | +
|
| 98 | +=== TEST 4: check phase for process wait |
| 99 | +--- config |
| 100 | + location = /t { |
| 101 | + content_by_lua_block { |
| 102 | + local ngx_pipe = require "ngx.pipe" |
| 103 | + local proc, err = ngx_pipe.spawn({"sleep", 0.1}) |
| 104 | + if not proc then |
| 105 | + ngx.say(err) |
| 106 | + return |
| 107 | + end |
| 108 | +
|
| 109 | + package.loaded.proc = proc |
| 110 | + } |
| 111 | +
|
| 112 | + log_by_lua_block { |
| 113 | + package.loaded.proc:wait() |
| 114 | + } |
| 115 | + } |
| 116 | +--- error_log |
| 117 | +API disabled in the context of log_by_lua |
| 118 | +
|
| 119 | +
|
| 120 | +
|
| 121 | +=== TEST 5: check process wait arguments |
| 122 | +--- config |
| 123 | + location = /t { |
| 124 | + content_by_lua_block { |
| 125 | + local ngx_pipe = require "ngx.pipe" |
| 126 | + local proc, err = ngx_pipe.spawn({"sleep", 0.1}) |
| 127 | + proc.wait() |
| 128 | + } |
| 129 | + } |
| 130 | +--- error_code: 500 |
| 131 | +--- ignore_response_body |
| 132 | +--- error_log eval |
| 133 | +qr/\[error\] .*? runtime error: content_by_lua\(nginx\.conf\:\d+\):\d+: not a process instance/ |
| 134 | +--- no_error_log |
| 135 | +[crit] |
| 136 | +
|
| 137 | +
|
| 138 | +
|
| 139 | +=== TEST 6: wait an already waited process |
| 140 | +--- config |
| 141 | + location = /t { |
| 142 | + content_by_lua_block { |
| 143 | + local ngx_pipe = require "ngx.pipe" |
| 144 | + local proc, err = ngx_pipe.spawn({"ls"}) |
| 145 | + if not proc then |
| 146 | + ngx.say(err) |
| 147 | + return |
| 148 | + end |
| 149 | +
|
| 150 | + local ok, err = proc:wait() |
| 151 | + if not ok then |
| 152 | + ngx.say(err) |
| 153 | + return |
| 154 | + end |
| 155 | +
|
| 156 | + local ok, err = proc:wait() |
| 157 | + if not ok then |
| 158 | + ngx.say(err) |
| 159 | + end |
| 160 | + } |
| 161 | + } |
| 162 | +--- response_body |
| 163 | +exited |
| 164 | +
|
| 165 | +
|
| 166 | +
|
| 167 | +=== TEST 7: more than one coroutines wait a process |
| 168 | +--- config |
| 169 | + location = /t { |
| 170 | + content_by_lua_block { |
| 171 | + local ngx_pipe = require "ngx.pipe" |
| 172 | + local proc, err = ngx_pipe.spawn({"sleep", 0.1}) |
| 173 | + if not proc then |
| 174 | + ngx.say(err) |
| 175 | + return |
| 176 | + end |
| 177 | +
|
| 178 | + local function wait() |
| 179 | + local ok, err = proc:wait() |
| 180 | + if not ok then |
| 181 | + ngx.say(err) |
| 182 | + end |
| 183 | + end |
| 184 | +
|
| 185 | + local th1 = ngx.thread.spawn(wait) |
| 186 | + local th2 = ngx.thread.spawn(wait) |
| 187 | + ngx.thread.wait(th1) |
| 188 | + ngx.thread.wait(th2) |
| 189 | + ngx.thread.spawn(wait) |
| 190 | + } |
| 191 | + } |
| 192 | +--- response_body |
| 193 | +pipe busy waiting |
| 194 | +exited |
| 195 | +
|
| 196 | +
|
| 197 | +
|
| 198 | +=== TEST 8: kill living sub-process during Lua VM destruction. |
| 199 | +--- config |
| 200 | + location = /t { |
| 201 | + content_by_lua_block { |
| 202 | + local ngx_pipe = require "ngx.pipe" |
| 203 | + local proc, err = ngx_pipe.spawn({"sleep", 3600}) |
| 204 | + if not proc then |
| 205 | + ngx.say(err) |
| 206 | + return |
| 207 | + end |
| 208 | + ngx.say("ok") |
| 209 | + } |
| 210 | + } |
| 211 | +--- response_body |
| 212 | +ok |
| 213 | +--- shutdown_error_log |
| 214 | +lua pipe destroy process: |
| 215 | +lua pipe kill process: |
0 commit comments