diff options
| author | Caleb Maclennan <caleb@alerque.com> | 2026-08-31 11:00:36 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-08-31 11:00:36 +0300 |
| commit | b836466de5a78e9b4f29ca6c85a2bdecd7c69da9 (patch) | |
| tree | 465b3b3a8121b1625b7ee3c9fba2e1f3cdce8a3c /test | |
| parent | 8f18ce95bb38c7f5c4bef5b3684cf1b0df1fc266 (diff) | |
| parent | 75d638ac9cc613f39ec4513d3dc2e43a6f909da1 (diff) | |
| download | luasocket-b836466de5a78e9b4f29ca6c85a2bdecd7c69da9.tar.gz luasocket-b836466de5a78e9b4f29ca6c85a2bdecd7c69da9.tar.bz2 luasocket-b836466de5a78e9b4f29ca6c85a2bdecd7c69da9.zip | |
Merge pull request #471 from lunarmodules/fix/zero-read
fix(receive): a receive 0 should immediately return, not block
Diffstat (limited to 'test')
| -rw-r--r-- | test/test_tcp_receive_zero.lua | 36 | ||||
| -rw-r--r-- | test/test_udp_receive_zero.lua | 267 | ||||
| -rw-r--r-- | test/test_unixdgram_receive_zero.lua | 200 |
3 files changed, 503 insertions, 0 deletions
diff --git a/test/test_tcp_receive_zero.lua b/test/test_tcp_receive_zero.lua new file mode 100644 index 0000000..2259910 --- /dev/null +++ b/test/test_tcp_receive_zero.lua | |||
| @@ -0,0 +1,36 @@ | |||
| 1 | -- a TCP receive(0) must never block: requesting zero bytes is trivially | ||
| 2 | -- satisfied without touching the transport layer, POSIX recv(fd, buf, 0, 0) | ||
| 3 | -- returns immediately regardless of whether data is available. | ||
| 4 | local socket = require "socket" | ||
| 5 | |||
| 6 | local host, port = "127.0.0.1", "5464" | ||
| 7 | |||
| 8 | local server = assert(socket.bind(host, port)) | ||
| 9 | local client = assert(socket.connect(host, port)) | ||
| 10 | local peer = assert(server:accept()) | ||
| 11 | |||
| 12 | client:settimeout(2) | ||
| 13 | |||
| 14 | -- no data has been sent by the peer: the read buffer is empty, so if | ||
| 15 | -- receive(0) touches the network it will block until the timeout expires | ||
| 16 | local t0 = socket.gettime() | ||
| 17 | local data, err = client:receive(0) | ||
| 18 | local elapsed = socket.gettime() - t0 | ||
| 19 | |||
| 20 | assert(data == "", "receive(0) on empty buffer returned " .. tostring(data)) | ||
| 21 | assert(err == nil, "receive(0) on empty buffer returned error " .. tostring(err)) | ||
| 22 | assert(elapsed < 1, "receive(0) on empty buffer blocked for " .. elapsed .. "s") | ||
| 23 | |||
| 24 | -- receive(0) must also not consume any bytes when data *is* available | ||
| 25 | assert(peer:send("hello")) | ||
| 26 | data, err = client:receive(0) | ||
| 27 | assert(data == "", "receive(0) with data pending returned " .. tostring(data)) | ||
| 28 | assert(err == nil, "receive(0) with data pending returned error " .. tostring(err)) | ||
| 29 | data = assert(client:receive(5)) | ||
| 30 | assert(data == "hello", "receive(0) consumed bytes meant for receive(5)") | ||
| 31 | |||
| 32 | client:close() | ||
| 33 | peer:close() | ||
| 34 | server:close() | ||
| 35 | |||
| 36 | print("done!") | ||
diff --git a/test/test_udp_receive_zero.lua b/test/test_udp_receive_zero.lua new file mode 100644 index 0000000..f3ca090 --- /dev/null +++ b/test/test_udp_receive_zero.lua | |||
| @@ -0,0 +1,267 @@ | |||
| 1 | #!/usr/bin/env lua | ||
| 2 | |||
| 3 | -- "receive 0 bytes" on a UDP socket is really two different requests that | ||
| 4 | -- happen to share one call shape: | ||
| 5 | -- | ||
| 6 | -- (a) "give me a side-effect-free readiness probe" -- don't touch | ||
| 7 | -- anything, just tell me instantly whether I'd have blocked. | ||
| 8 | -- (b) "my protocol's messages are always empty-payload signals (UDP | ||
| 9 | -- explicitly allows a zero-length datagram on the wire -- see | ||
| 10 | -- udp-zero-length-send-recv in this same directory); receive one | ||
| 11 | -- like any other, I just don't need a payload back." | ||
| 12 | -- | ||
| 13 | -- These two conflict on at least one real platform. A Darwin/BSD kernel's | ||
| 14 | -- recvfrom() returns 0 bytes immediately for a zero-length request | ||
| 15 | -- whether or not anything was actually queued -- so byte count alone can | ||
| 16 | -- never distinguish (a) from (b) there. Linux and (per MSDN's | ||
| 17 | -- recv()/recvfrom() docs -- no exception is carved out for SOCK_DGRAM the | ||
| 18 | -- way there is for "byte stream-style sockets" and WSAEINVAL, though this | ||
| 19 | -- is not independently confirmed against a live Windows kernel) Windows | ||
| 20 | -- both block until something real arrives, so they don't have this | ||
| 21 | -- ambiguity at all. | ||
| 22 | -- | ||
| 23 | -- platform | nothing pending | datagram pending | ||
| 24 | -- ------------+------------------------------+--------------------------- | ||
| 25 | -- Linux | blocks until one arrives | consumed & reported | ||
| 26 | -- Windows | blocks until one arrives * | consumed & reported * | ||
| 27 | -- Darwin/BSD | returns 0 immediately, | consumed & reported | ||
| 28 | -- | never blocks | | ||
| 29 | -- | ||
| 30 | -- What breaks the tie: receivefrom() also reports the sender's address, | ||
| 31 | -- and that address comes back unpopulated when nothing was queued and | ||
| 32 | -- correctly populated when a real datagram was consumed -- on every | ||
| 33 | -- platform, Darwin included. So while byte count can't disambiguate (a) | ||
| 34 | -- from (b) on Darwin, the address can. receivefrom(0) is therefore left | ||
| 35 | -- to make the real recvfrom() call: it correctly serves use case (b) | ||
| 36 | -- everywhere, and on Darwin specifically it gives an honest "nil address" | ||
| 37 | -- for (a) instead of the alternative this test suite used to ship -- a | ||
| 38 | -- crash out of getnameinfo() on a garbage/unpopulated sockaddr. | ||
| 39 | -- | ||
| 40 | -- receive() has no such disambiguator (no address field to check), so | ||
| 41 | -- unguarding it would only buy back blocking-until-signal on Linux/ | ||
| 42 | -- Windows while remaining just as ambiguous as before on Darwin. It's | ||
| 43 | -- left as the deterministic no-op it already was (LuaSocket's TCP | ||
| 44 | -- receive(0) fix, inherited via the shared socket_recv()) -- and that | ||
| 45 | -- happens to be exactly what Python's socket.recv(0) already does | ||
| 46 | -- (CPython's sock_recv_guts), for the same reason. | ||
| 47 | -- | ||
| 48 | -- How other cross-platform standard libraries handle this, checked at the | ||
| 49 | -- source level rather than the docs: | ||
| 50 | -- | ||
| 51 | -- language | receive()-equivalent | receivefrom()-equivalent | ||
| 52 | -- ---------+-------------------------+--------------------------------------- | ||
| 53 | -- Go | UDPConn.Read: guarded, | UDPConn.ReadFrom: unguarded, and built | ||
| 54 | -- | deterministic no-op | the same way LuaSocket's own | ||
| 55 | -- | (internal/poll. | socket_recvfrom() is: try the syscall | ||
| 56 | -- | FD.Read) | first (internal/poll.FD.ReadFrom calls | ||
| 57 | -- | | syscall.Recvfrom directly), only wait | ||
| 58 | -- | | after EAGAIN. Almost certainly shares | ||
| 59 | -- | | the Darwin quirk above as a result; | ||
| 60 | -- | | not confirmed whether Go's sockaddr | ||
| 61 | -- | | conversion crashes or silently zeroes | ||
| 62 | -- | | on an address the kernel never filled | ||
| 63 | -- | | in. | ||
| 64 | -- Python | socket.recv(): | socket.recvfrom(): sock_recvfrom_guts | ||
| 65 | -- | guarded, determin- | has no len==0 guard either, but does | ||
| 66 | -- | istic no-op | NOT hit the Darwin quirk in practice -- | ||
| 67 | -- | (sock_recv_guts) | it blocks for the full timeout with | ||
| 68 | -- | | nothing pending, same as Linux would. | ||
| 69 | -- | | CPython's sock_call_ex runs a real | ||
| 70 | -- | | select()/poll() readiness gate | ||
| 71 | -- | | (internal_select()) before ever | ||
| 72 | -- | | calling recvfrom(), for any operation | ||
| 73 | -- | | with a timeout. That gate reports | ||
| 74 | -- | | "not readable" honestly regardless of | ||
| 75 | -- | | the requested length, so the quirk | ||
| 76 | -- | | never gets a chance to fire. It's a | ||
| 77 | -- | | side effect of Python's general | ||
| 78 | -- | | blocking-with-timeout architecture, | ||
| 79 | -- | | not anything specific to recvfrom() | ||
| 80 | -- | | or to len==0. | ||
| 81 | -- Rust | UdpSocket::recv: | UdpSocket::recv_from: unguarded, | ||
| 82 | -- | unguarded, platform- | platform-dependent -- straight to | ||
| 83 | -- | dependent | libc::recvfrom, no select-gate, no | ||
| 84 | -- | | len==0 guard anywhere. | ||
| 85 | -- | ||
| 86 | -- So "platform-dependent" isn't actually the right label for every | ||
| 87 | -- unguarded implementation. Go shares LuaSocket's original exposure to | ||
| 88 | -- the Darwin quirk because it shares the same try-syscall-first shape; | ||
| 89 | -- Python avoids the quirk entirely, but via unrelated select-gating | ||
| 90 | -- machinery, not via anything resembling the address-population check | ||
| 91 | -- this fix adds. None of the three has that specific fix; LuaSocket adds | ||
| 92 | -- it here. | ||
| 93 | |||
| 94 | local socket = require "socket" | ||
| 95 | |||
| 96 | local host = "127.0.0.1" | ||
| 97 | |||
| 98 | local function new_pair(port) | ||
| 99 | local server = assert(socket.udp()) | ||
| 100 | assert(server:setsockname(host, port)) | ||
| 101 | local client = assert(socket.udp()) | ||
| 102 | assert(client:setpeername(host, port)) | ||
| 103 | server:settimeout(1) | ||
| 104 | return server, client | ||
| 105 | end | ||
| 106 | |||
| 107 | -- Detects which of the three documented recvfrom(0)-with-nothing-pending | ||
| 108 | -- behaviors applies here, so the test below can assert the right one by | ||
| 109 | -- name instead of accepting either shape. "unknown" is the honest answer | ||
| 110 | -- when uname isn't available to tell Linux and Darwin/BSD apart (e.g. a | ||
| 111 | -- locked-down sandbox); the test falls back to accepting either | ||
| 112 | -- documented shape in that case, rather than guessing. | ||
| 113 | local function detect_platform() | ||
| 114 | if package.config:sub(1, 1) == "\\" then | ||
| 115 | return "windows" | ||
| 116 | end | ||
| 117 | local handle = io.popen("uname -s 2>/dev/null") | ||
| 118 | if not handle then | ||
| 119 | return "unknown" | ||
| 120 | end | ||
| 121 | local name = handle:read("*l") | ||
| 122 | handle:close() | ||
| 123 | if not name then | ||
| 124 | return "unknown" | ||
| 125 | end | ||
| 126 | name = name:lower() | ||
| 127 | if name:find("linux") then | ||
| 128 | return "linux" | ||
| 129 | elseif name:find("darwin") or name:find("bsd") then | ||
| 130 | return "darwin" | ||
| 131 | end | ||
| 132 | return "unknown" | ||
| 133 | end | ||
| 134 | |||
| 135 | local platform = detect_platform() | ||
| 136 | |||
| 137 | -- === receive(0): deterministic no-op, nothing pending === | ||
| 138 | do | ||
| 139 | local server, client = new_pair("5465") | ||
| 140 | |||
| 141 | local t0 = socket.gettime() | ||
| 142 | local data, err = server:receive(0) | ||
| 143 | local elapsed = socket.gettime() - t0 | ||
| 144 | assert(data == "" and err == nil, | ||
| 145 | "receive(0) with nothing pending returned " .. tostring(data) .. ", " .. tostring(err)) | ||
| 146 | assert(elapsed < 0.5, | ||
| 147 | "receive(0) with nothing pending blocked for " .. elapsed .. "s") | ||
| 148 | |||
| 149 | client:close() | ||
| 150 | server:close() | ||
| 151 | end | ||
| 152 | |||
| 153 | -- === receive(0): deterministic no-op, a datagram pending must be left untouched === | ||
| 154 | do | ||
| 155 | local server, client = new_pair("5466") | ||
| 156 | |||
| 157 | assert(client:send("world")) | ||
| 158 | socket.sleep(0.1) | ||
| 159 | |||
| 160 | local data, err = server:receive(0) | ||
| 161 | assert(data == "" and err == nil, | ||
| 162 | "receive(0) with data pending returned " .. tostring(data) .. ", " .. tostring(err)) | ||
| 163 | |||
| 164 | data = assert(server:receive()) | ||
| 165 | assert(data == "world", | ||
| 166 | "receive(0) consumed or corrupted the pending datagram: got " .. tostring(data)) | ||
| 167 | |||
| 168 | client:close() | ||
| 169 | server:close() | ||
| 170 | end | ||
| 171 | |||
| 172 | -- === receivefrom(0): nothing pending -- the outcome is platform-specific | ||
| 173 | -- (see the comment block at the top of this file), asserted explicitly | ||
| 174 | -- per platform so it's clear which behavior is expected where === | ||
| 175 | do | ||
| 176 | local server, client = new_pair("5467") | ||
| 177 | |||
| 178 | local data, ip, port = server:receivefrom(0) | ||
| 179 | |||
| 180 | if platform == "linux" or platform == "windows" then | ||
| 181 | -- both block until a real datagram arrives; nothing does, so the | ||
| 182 | -- call must time out, not fabricate a result | ||
| 183 | assert(data == nil and ip == "timeout", | ||
| 184 | "receivefrom(0) with nothing pending on " .. platform .. | ||
| 185 | " should time out, got " .. tostring(data) .. ", " .. tostring(ip)) | ||
| 186 | |||
| 187 | elseif platform == "darwin" then | ||
| 188 | -- the kernel's zero-length recvfrom() is always immediately | ||
| 189 | -- satisfiable, so this must return right away, with no address | ||
| 190 | assert(data == "" and ip == nil and port == nil, | ||
| 191 | "receivefrom(0) with nothing pending on darwin should return " .. | ||
| 192 | "immediately with no address, got " .. | ||
| 193 | tostring(data) .. ", " .. tostring(ip) .. ", " .. tostring(port)) | ||
| 194 | |||
| 195 | else | ||
| 196 | -- platform could not be determined: accept either documented | ||
| 197 | -- shape rather than assert one and risk a false failure | ||
| 198 | local timed_out_like_linux_or_windows = data == nil and ip == "timeout" | ||
| 199 | local returned_immediately_like_darwin = data == "" and ip == nil and port == nil | ||
| 200 | assert(timed_out_like_linux_or_windows or returned_immediately_like_darwin, | ||
| 201 | "receivefrom(0) with nothing pending returned an unexpected shape: " .. | ||
| 202 | tostring(data) .. ", " .. tostring(ip) .. ", " .. tostring(port)) | ||
| 203 | end | ||
| 204 | |||
| 205 | client:close() | ||
| 206 | server:close() | ||
| 207 | end | ||
| 208 | |||
| 209 | -- === receivefrom(0): a datagram is pending -- must be consumed and its | ||
| 210 | -- sender correctly reported, on every platform === | ||
| 211 | do | ||
| 212 | local server, client = new_pair("5468") | ||
| 213 | |||
| 214 | assert(client:send("hello")) | ||
| 215 | socket.sleep(0.1) | ||
| 216 | |||
| 217 | local data, ip, port = server:receivefrom(0) | ||
| 218 | assert(data == "" and ip == "127.0.0.1" and type(port) == "number", | ||
| 219 | "receivefrom(0) with data pending returned " .. | ||
| 220 | tostring(data) .. ", " .. tostring(ip) .. ", " .. tostring(port)) | ||
| 221 | |||
| 222 | -- the payload is gone (all that a 0-byte request can ever return), but | ||
| 223 | -- unlike a crash or a phantom no-op, the caller correctly learned that | ||
| 224 | -- something arrived and who sent it | ||
| 225 | server:settimeout(0.3) | ||
| 226 | data = server:receive() | ||
| 227 | assert(data == nil, | ||
| 228 | "receivefrom(0) did not actually consume the pending datagram: receive() still got " .. | ||
| 229 | tostring(data)) | ||
| 230 | |||
| 231 | client:close() | ||
| 232 | server:close() | ||
| 233 | end | ||
| 234 | |||
| 235 | -- === the real-world case this whole file is about: a protocol whose | ||
| 236 | -- messages are always empty-payload signals. A 0-length UDP datagram is | ||
| 237 | -- a completely legitimate thing to put on the wire (see | ||
| 238 | -- udp-zero-length-send-recv), and receivefrom(0) must work as a normal | ||
| 239 | -- receive path for it -- reporting the (empty) payload and the sender -- | ||
| 240 | -- not just as a probe for some other, larger message. === | ||
| 241 | do | ||
| 242 | local server, client = new_pair("5469") | ||
| 243 | |||
| 244 | -- sanity check: a genuine zero-length datagram over the wire, read | ||
| 245 | -- with a normal-sized buffer, already works (untouched by any of this) | ||
| 246 | assert(client:send("")) | ||
| 247 | socket.sleep(0.1) | ||
| 248 | local data, ip, port = server:receivefrom() | ||
| 249 | assert(data == "" and ip == "127.0.0.1" and type(port) == "number", | ||
| 250 | "a real zero-length datagram, read normally, returned " .. | ||
| 251 | tostring(data) .. ", " .. tostring(ip) .. ", " .. tostring(port)) | ||
| 252 | |||
| 253 | -- the actual point: a receiver that knows every message on this | ||
| 254 | -- socket is an empty signal can use receivefrom(0) as its normal | ||
| 255 | -- receive call, and still learn who signaled it | ||
| 256 | assert(client:send("")) | ||
| 257 | socket.sleep(0.1) | ||
| 258 | data, ip, port = server:receivefrom(0) | ||
| 259 | assert(data == "" and ip == "127.0.0.1" and type(port) == "number", | ||
| 260 | "receivefrom(0) as a normal receive path for a signal datagram returned " .. | ||
| 261 | tostring(data) .. ", " .. tostring(ip) .. ", " .. tostring(port)) | ||
| 262 | |||
| 263 | client:close() | ||
| 264 | server:close() | ||
| 265 | end | ||
| 266 | |||
| 267 | print("done!") | ||
diff --git a/test/test_unixdgram_receive_zero.lua b/test/test_unixdgram_receive_zero.lua new file mode 100644 index 0000000..c91c3cd --- /dev/null +++ b/test/test_unixdgram_receive_zero.lua | |||
| @@ -0,0 +1,200 @@ | |||
| 1 | #!/usr/bin/env lua | ||
| 2 | |||
| 3 | -- Same reasoning as test_udp_receive_zero.lua: "receive 0 bytes" on a | ||
| 4 | -- datagram socket is really two different requests sharing one call | ||
| 5 | -- shape -- (a) a side-effect-free readiness probe, and (b) "my protocol's | ||
| 6 | -- messages are always empty-payload signals, receive one normally, I | ||
| 7 | -- just don't need a payload back" (a zero-length datagram is a | ||
| 8 | -- completely legitimate thing to put on the wire). | ||
| 9 | -- | ||
| 10 | -- receive() has no sender-address field to disambiguate (a) from (b), so | ||
| 11 | -- it stays the deterministic no-op it already was via the shared | ||
| 12 | -- socket_recv() guard (unaffected here, same as TCP). receivefrom() does | ||
| 13 | -- have that field: unlike inet UDP, unixdgram.c already handled an | ||
| 14 | -- unpopulated sender path gracefully (it pre-zeroes sun_path and just | ||
| 15 | -- returns whatever's there -- see the "may be empty when client sent | ||
| 16 | -- without bind" comment in unixdgram.c), so receivefrom(0) here was | ||
| 17 | -- never at risk of the getnameinfo crash inet UDP had. It's left making | ||
| 18 | -- the real recvfrom() call: nothing pending is platform-dependent | ||
| 19 | -- (blocks on Linux/Windows, returns immediately on Darwin/BSD -- see the | ||
| 20 | -- platform table in test_udp_receive_zero.lua), but a pending datagram is | ||
| 21 | -- always correctly consumed and its sender always correctly reported. | ||
| 22 | |||
| 23 | local socket = require "socket" | ||
| 24 | local unix = require "socket.unix" | ||
| 25 | |||
| 26 | local function new_pair(server_path, client_path) | ||
| 27 | os.remove(server_path) | ||
| 28 | os.remove(client_path) | ||
| 29 | local server = assert(unix.dgram()) | ||
| 30 | assert(server:bind(server_path)) | ||
| 31 | local client = assert(unix.dgram()) | ||
| 32 | assert(client:bind(client_path)) | ||
| 33 | assert(client:connect(server_path)) | ||
| 34 | server:settimeout(1) | ||
| 35 | return server, client | ||
| 36 | end | ||
| 37 | |||
| 38 | local function cleanup(server, client, server_path, client_path) | ||
| 39 | client:close() | ||
| 40 | server:close() | ||
| 41 | os.remove(server_path) | ||
| 42 | os.remove(client_path) | ||
| 43 | end | ||
| 44 | |||
| 45 | -- Detects which of the three documented recvfrom(0)-with-nothing-pending | ||
| 46 | -- behaviors applies here, so the test below can assert the right one by | ||
| 47 | -- name instead of accepting either shape. "unknown" is the honest answer | ||
| 48 | -- when uname isn't available to tell Linux and Darwin/BSD apart (e.g. a | ||
| 49 | -- locked-down sandbox); the test falls back to accepting either | ||
| 50 | -- documented shape in that case, rather than guessing. | ||
| 51 | local function detect_platform() | ||
| 52 | if package.config:sub(1, 1) == "\\" then | ||
| 53 | return "windows" | ||
| 54 | end | ||
| 55 | local handle = io.popen("uname -s 2>/dev/null") | ||
| 56 | if not handle then | ||
| 57 | return "unknown" | ||
| 58 | end | ||
| 59 | local name = handle:read("*l") | ||
| 60 | handle:close() | ||
| 61 | if not name then | ||
| 62 | return "unknown" | ||
| 63 | end | ||
| 64 | name = name:lower() | ||
| 65 | if name:find("linux") then | ||
| 66 | return "linux" | ||
| 67 | elseif name:find("darwin") or name:find("bsd") then | ||
| 68 | return "darwin" | ||
| 69 | end | ||
| 70 | return "unknown" | ||
| 71 | end | ||
| 72 | |||
| 73 | local platform = detect_platform() | ||
| 74 | |||
| 75 | -- === receive(0): deterministic no-op, nothing pending === | ||
| 76 | do | ||
| 77 | local spath, cpath = "/tmp/luasocket-test-udgram-1-srv.sock", "/tmp/luasocket-test-udgram-1-clt.sock" | ||
| 78 | local server, client = new_pair(spath, cpath) | ||
| 79 | |||
| 80 | local t0 = socket.gettime() | ||
| 81 | local rdata, rerr = server:receive(0) | ||
| 82 | local elapsed = socket.gettime() - t0 | ||
| 83 | assert(rdata == "" and rerr == nil, | ||
| 84 | "receive(0) with nothing pending returned " .. tostring(rdata) .. ", " .. tostring(rerr)) | ||
| 85 | assert(elapsed < 0.5, | ||
| 86 | "receive(0) with nothing pending blocked for " .. elapsed .. "s") | ||
| 87 | |||
| 88 | cleanup(server, client, spath, cpath) | ||
| 89 | end | ||
| 90 | |||
| 91 | -- === receive(0): deterministic no-op, a datagram pending must be left untouched === | ||
| 92 | do | ||
| 93 | local spath, cpath = "/tmp/luasocket-test-udgram-2-srv.sock", "/tmp/luasocket-test-udgram-2-clt.sock" | ||
| 94 | local server, client = new_pair(spath, cpath) | ||
| 95 | |||
| 96 | assert(client:send("world")) | ||
| 97 | socket.sleep(0.1) | ||
| 98 | |||
| 99 | local data, err = server:receive(0) | ||
| 100 | assert(data == "" and err == nil, | ||
| 101 | "receive(0) with data pending returned " .. tostring(data) .. ", " .. tostring(err)) | ||
| 102 | |||
| 103 | data, err = assert(server:receive()) | ||
| 104 | assert(data == "world", | ||
| 105 | "receive(0) consumed or corrupted the pending datagram: got " .. tostring(data)) | ||
| 106 | |||
| 107 | cleanup(server, client, spath, cpath) | ||
| 108 | end | ||
| 109 | |||
| 110 | -- === receivefrom(0): nothing pending -- the outcome is platform-specific | ||
| 111 | -- (see the comment block at the top of test_udp_receive_zero.lua), | ||
| 112 | -- asserted explicitly per platform so it's clear which behavior is | ||
| 113 | -- expected where === | ||
| 114 | do | ||
| 115 | local spath, cpath = "/tmp/luasocket-test-udgram-3-srv.sock", "/tmp/luasocket-test-udgram-3-clt.sock" | ||
| 116 | local server, client = new_pair(spath, cpath) | ||
| 117 | |||
| 118 | local data, addr = server:receivefrom(0) | ||
| 119 | |||
| 120 | if platform == "linux" or platform == "windows" then | ||
| 121 | -- both block until a real datagram arrives; nothing does, so the | ||
| 122 | -- call must time out, not fabricate a result | ||
| 123 | assert(data == nil and addr == "timeout", | ||
| 124 | "receivefrom(0) with nothing pending on " .. platform .. | ||
| 125 | " should time out, got " .. tostring(data) .. ", " .. tostring(addr)) | ||
| 126 | |||
| 127 | elseif platform == "darwin" then | ||
| 128 | -- the kernel's zero-length recvfrom() is always immediately | ||
| 129 | -- satisfiable, so this must return right away, with no sender path | ||
| 130 | assert(data == "" and addr == "", | ||
| 131 | "receivefrom(0) with nothing pending on darwin should return " .. | ||
| 132 | "immediately with no sender path, got " .. | ||
| 133 | tostring(data) .. ", " .. tostring(addr)) | ||
| 134 | |||
| 135 | else | ||
| 136 | -- platform could not be determined: accept either documented | ||
| 137 | -- shape rather than assert one and risk a false failure | ||
| 138 | local timed_out_like_linux_or_windows = data == nil and addr == "timeout" | ||
| 139 | local returned_immediately_like_darwin = data == "" and addr == "" | ||
| 140 | assert(timed_out_like_linux_or_windows or returned_immediately_like_darwin, | ||
| 141 | "receivefrom(0) with nothing pending returned an unexpected shape: " .. | ||
| 142 | tostring(data) .. ", " .. tostring(addr)) | ||
| 143 | end | ||
| 144 | |||
| 145 | cleanup(server, client, spath, cpath) | ||
| 146 | end | ||
| 147 | |||
| 148 | -- === receivefrom(0): a datagram is pending -- must be consumed and its | ||
| 149 | -- sender correctly reported, on every platform === | ||
| 150 | do | ||
| 151 | local spath, cpath = "/tmp/luasocket-test-udgram-4-srv.sock", "/tmp/luasocket-test-udgram-4-clt.sock" | ||
| 152 | local server, client = new_pair(spath, cpath) | ||
| 153 | |||
| 154 | assert(client:send("hello")) | ||
| 155 | socket.sleep(0.1) | ||
| 156 | |||
| 157 | local data, addr = server:receivefrom(0) | ||
| 158 | assert(data == "" and addr == cpath, | ||
| 159 | "receivefrom(0) with data pending returned " .. tostring(data) .. ", " .. tostring(addr)) | ||
| 160 | |||
| 161 | server:settimeout(0.3) | ||
| 162 | data = server:receive() | ||
| 163 | assert(data == nil, | ||
| 164 | "receivefrom(0) did not actually consume the pending datagram: receive() still got " .. | ||
| 165 | tostring(data)) | ||
| 166 | |||
| 167 | cleanup(server, client, spath, cpath) | ||
| 168 | end | ||
| 169 | |||
| 170 | -- === the real-world case this whole file is about: a protocol whose | ||
| 171 | -- messages are always empty-payload signals. receivefrom(0) must work as | ||
| 172 | -- a normal receive path for it -- reporting the (empty) payload and the | ||
| 173 | -- sender -- not just as a probe for some other, larger message. === | ||
| 174 | do | ||
| 175 | local spath, cpath = "/tmp/luasocket-test-udgram-5-srv.sock", "/tmp/luasocket-test-udgram-5-clt.sock" | ||
| 176 | local server, client = new_pair(spath, cpath) | ||
| 177 | |||
| 178 | -- sanity check: a genuine zero-length datagram, read with a | ||
| 179 | -- normal-sized buffer, already works (untouched by any of this) | ||
| 180 | assert(client:send("")) | ||
| 181 | socket.sleep(0.1) | ||
| 182 | local data, addr = server:receivefrom() | ||
| 183 | assert(data == "" and addr == cpath, | ||
| 184 | "a real zero-length datagram, read normally, returned " .. | ||
| 185 | tostring(data) .. ", " .. tostring(addr)) | ||
| 186 | |||
| 187 | -- the actual point: a receiver that knows every message on this | ||
| 188 | -- socket is an empty signal can use receivefrom(0) as its normal | ||
| 189 | -- receive call, and still learn who signaled it | ||
| 190 | assert(client:send("")) | ||
| 191 | socket.sleep(0.1) | ||
| 192 | data, addr = server:receivefrom(0) | ||
| 193 | assert(data == "" and addr == cpath, | ||
| 194 | "receivefrom(0) as a normal receive path for a signal datagram returned " .. | ||
| 195 | tostring(data) .. ", " .. tostring(addr)) | ||
| 196 | |||
| 197 | cleanup(server, client, spath, cpath) | ||
| 198 | end | ||
| 199 | |||
| 200 | print("done!") | ||
