|
| 1 | +# vim:set ft= ts=4 sw=4 et fdm=marker: |
| 2 | + |
| 3 | +our $SkipReason; |
| 4 | + |
| 5 | +BEGIN { |
| 6 | + if ($^O ne 'linux') { |
| 7 | + $SkipReason = "SO_REUSEPORT is only supported on Linux"; |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | +use Test::Nginx::Socket::Lua::Stream $SkipReason ? (skip_all => $SkipReason) : (); |
| 12 | + |
| 13 | +repeat_each(2); |
| 14 | + |
| 15 | +plan tests => blocks() * (repeat_each() * 3); |
| 16 | + |
| 17 | +run_tests(); |
| 18 | + |
| 19 | +__DATA__ |
| 20 | + |
| 21 | +=== TEST 1: udp socket setoption reuseport sanity |
| 22 | +--- stream_config |
| 23 | +server { |
| 24 | + listen 127.0.0.1:2986 udp; |
| 25 | + content_by_lua_block { |
| 26 | + ngx.log(ngx.INFO, "udp reuseport test remote: " .. ngx.var.remote_addr) |
| 27 | + } |
| 28 | +} |
| 29 | +--- stream_server_config |
| 30 | + content_by_lua_block { |
| 31 | + local ip = "127.0.0.1" |
| 32 | + local port = 2986 |
| 33 | + local sock = ngx.socket.udp() |
| 34 | + |
| 35 | + local ok, err = sock:setoption("reuseport", true) |
| 36 | + if not ok then |
| 37 | + ngx.log(ngx.ERR, "failed to set reuseport: ", err) |
| 38 | + return |
| 39 | + end |
| 40 | + |
| 41 | + local ok, err = sock:setpeername(ip, port) |
| 42 | + if not ok then |
| 43 | + ngx.log(ngx.ERR, "failed to setpeername: ", err) |
| 44 | + return |
| 45 | + end |
| 46 | + |
| 47 | + local ok, err = sock:send("trigger") |
| 48 | + if not ok then |
| 49 | + ngx.log(ngx.ERR, "failed to send: ", err) |
| 50 | + end |
| 51 | + } |
| 52 | + |
| 53 | +--- no_error_log |
| 54 | +[error] |
| 55 | +--- error_log eval |
| 56 | +["stream lua set UDP upstream with REUSEPORT"] |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +=== TEST 2: udp socket setoption reuseport false |
| 61 | +--- stream_config |
| 62 | +server { |
| 63 | + listen 127.0.0.1:2986 udp; |
| 64 | + content_by_lua_block { |
| 65 | + ngx.log(ngx.INFO, "udp reuseport test remote: " .. ngx.var.remote_addr) |
| 66 | + } |
| 67 | +} |
| 68 | +--- stream_server_config |
| 69 | + content_by_lua_block { |
| 70 | + local ip = "127.0.0.1" |
| 71 | + local port = 2986 |
| 72 | + local sock = ngx.socket.udp() |
| 73 | + |
| 74 | + local ok, err = sock:setoption("reuseport", false) |
| 75 | + if not ok then |
| 76 | + ngx.log(ngx.ERR, "failed to set reuseport: ", err) |
| 77 | + return |
| 78 | + end |
| 79 | + |
| 80 | + local ok, err = sock:setpeername(ip, port) |
| 81 | + if not ok then |
| 82 | + ngx.log(ngx.ERR, "failed to setpeername: ", err) |
| 83 | + return |
| 84 | + end |
| 85 | + |
| 86 | + local ok, err = sock:send("trigger") |
| 87 | + if not ok then |
| 88 | + ngx.log(ngx.ERR, "failed to send: ", err) |
| 89 | + end |
| 90 | + } |
| 91 | + |
| 92 | +--- no_error_log |
| 93 | +[error] |
| 94 | +--- no_error_log eval |
| 95 | +["stream lua set UDP upstream with REUSEPORT"] |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +=== TEST 3: udp socket setoption reuseport with bind |
| 100 | +--- stream_config |
| 101 | +server { |
| 102 | + listen 127.0.0.1:2986 udp; |
| 103 | + content_by_lua_block { |
| 104 | + ngx.log(ngx.INFO, "udp reuseport test remote: " .. ngx.var.remote_addr) |
| 105 | + } |
| 106 | +} |
| 107 | +--- stream_server_config |
| 108 | + content_by_lua_block { |
| 109 | + local ip = "127.0.0.1" |
| 110 | + local port = 2986 |
| 111 | + local sock = ngx.socket.udp() |
| 112 | + |
| 113 | + local ok, err = sock:bind(ip) |
| 114 | + if not ok then |
| 115 | + ngx.log(ngx.ERR, "failed to bind: ", err) |
| 116 | + return |
| 117 | + end |
| 118 | + |
| 119 | + local ok, err = sock:setoption("reuseport", true) |
| 120 | + if not ok then |
| 121 | + ngx.log(ngx.ERR, "failed to set reuseport: ", err) |
| 122 | + return |
| 123 | + end |
| 124 | + |
| 125 | + local ok, err = sock:setpeername("127.0.0.1", port) |
| 126 | + if not ok then |
| 127 | + ngx.log(ngx.ERR, "failed to setpeername: ", err) |
| 128 | + return |
| 129 | + end |
| 130 | + |
| 131 | + local ok, err = sock:send("trigger") |
| 132 | + if not ok then |
| 133 | + ngx.log(ngx.ERR, "failed to send: ", err) |
| 134 | + end |
| 135 | + } |
| 136 | + |
| 137 | +--- no_error_log |
| 138 | +[error] |
| 139 | +--- error_log eval |
| 140 | +["lua udp socket bind ip: 127.0.0.1", |
| 141 | +"stream lua set UDP upstream with REUSEPORT"] |
0 commit comments