Add lb_tcpsocket test

This commit is contained in:
lumi 2025-06-28 17:21:28 +10:00
parent 85ed0d8318
commit cea9bc0813
Signed by: luaneko
GPG Key ID: 406809B8763FF07A

View File

@ -26,13 +26,50 @@ describe("tcp", function()
describe("socket", function()
test("bind", function()
local socket = net.bind_tcp("127.0.0.1")
-- binds to the correct port
assert(tostring(socket:local_addr():ip()) == "127.0.0.1")
assert(socket:local_addr():port() ~= 0)
-- should not be able to rebind socket
assert(not pcall(socket.bind, socket, net.socketaddr("127.0.0.1")))
end)
test("options", function()
local socket = net.tcp()
-- keepalive
socket:set_keepalive(true)
assert(socket:keepalive() == true)
socket:set_keepalive(false)
assert(socket:keepalive() == false)
-- reuseaddr
socket:set_reuseaddr(true)
assert(socket:reuseaddr() == true)
socket:set_reuseaddr(false)
assert(socket:reuseaddr() == false)
-- reuseport not always supported on all platforms
-- sendbuf
socket:set_sendbuf(4096)
assert(socket:sendbuf() >= 4096)
-- recvbuf
socket:set_recvbuf(4096)
assert(socket:recvbuf() >= 4096)
-- linger
socket:set_linger(0)
assert(socket:linger() == 0)
socket:set_linger(2)
assert(math.abs(socket:linger() - 2) < 0.1)
-- nodelay
socket:set_nodelay(true)
assert(socket:nodelay() == true)
socket:set_nodelay(false)
assert(socket:nodelay() == false)
end)
test("can't use socket after conversion", function()
local socket = net.tcp()
socket:bind(net.socketaddr("127.0.0.1"))
socket:listen(10) -- convert to listener
assert(not pcall(socket.listen, socket, 10)) -- socket consumed
assert(not pcall(socket.local_addr, socket))
end)
end)
end)