Add basic tcp socket test
This commit is contained in:
parent
4f548bf9e9
commit
0c4639c3e9
@ -124,7 +124,11 @@ impl lb_netlib {
|
|||||||
}
|
}
|
||||||
|
|
||||||
extern "Lua-C" fn __parse_skaddr(&self, addr: &str) -> Result<lb_socketaddr> {
|
extern "Lua-C" fn __parse_skaddr(&self, addr: &str) -> Result<lb_socketaddr> {
|
||||||
Ok(addr.parse()?)
|
Ok(if let Ok(addr) = addr.parse() {
|
||||||
|
SocketAddr::new(addr, 0).into() // default port 0
|
||||||
|
} else {
|
||||||
|
addr.parse::<SocketAddr>()?.into()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new TCP socket configured for IPv4.
|
/// Creates a new TCP socket configured for IPv4.
|
||||||
|
17
crates/lb/tests/net.lua
Normal file
17
crates/lb/tests/net.lua
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
local ok, net = pcall(require, "lb:net")
|
||||||
|
if not ok then return end
|
||||||
|
|
||||||
|
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)
|
||||||
|
end)
|
||||||
|
end)
|
Loading…
x
Reference in New Issue
Block a user