Skip to content

Commit 1bcb766

Browse files
committed
use assert on socket functions
1 parent de2d1c6 commit 1bcb766

File tree

1 file changed

+16
-33
lines changed

1 file changed

+16
-33
lines changed

test/tcp_client_server.lua

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,13 @@ test('TCP client-server communication', function()
77
local update_client
88

99
-- Server setup
10-
local info = socket.find_first_address_info("*", port, {"passive"}, "inet", "stream", "tcp")
11-
ok(info ~= nil, "should find address info for server")
10+
local info = assert(socket.find_first_address_info("*", port, {"passive"}, "inet", "stream", "tcp"))
11+
local server = assert(socket.create(info.family, info.socket_type, info.protocol))
1212

13-
local server = socket.create(info.family, info.socket_type, info.protocol)
14-
ok(server ~= nil, "should create server socket")
15-
16-
server:set_option("reuseaddr", 1)
17-
local blocking_set = server:set_blocking(false)
18-
ok(blocking_set, "should set server to non-blocking")
19-
20-
local bound = server:bind(info)
21-
ok(bound, "should bind server to port")
22-
23-
local listening = server:listen()
24-
ok(listening, "should start listening")
13+
assert(server:set_option("reuseaddr", 1))
14+
assert(server:set_blocking(false))
15+
assert(server:bind(info))
16+
assert(server:listen())
2517

2618
local current_client = nil
2719

@@ -30,8 +22,7 @@ test('TCP client-server communication', function()
3022

3123
if client then
3224
current_client = client
33-
local blocking_set = client:set_blocking(false)
34-
ok(blocking_set, "should set client socket to non-blocking")
25+
assert(client:set_blocking(false))
3526
elseif err ~= "timeout" then
3627
ok(false, "server accept error: " .. tostring(err))
3728
end
@@ -40,45 +31,37 @@ test('TCP client-server communication', function()
4031
local str, err = current_client:receive()
4132
if str then
4233
ok(str == "hello", "server should receive 'hello' message")
43-
local sent = current_client:send(str)
44-
ok(sent, "server should send echo message")
34+
assert(current_client:send(str))
4535
elseif err == "closed" then
46-
local closed = current_client:close()
47-
ok(closed, "should close client connection")
36+
assert(current_client:close())
4837
current_client = nil
4938
return true
5039
elseif err ~= "timeout" then
51-
ok(false, "server receive error: " .. tostring(err))
40+
error("server receive error: " .. tostring(err))
5241
end
5342
end
5443
end
5544

5645
-- Client setup
57-
local client = socket.create("inet", "stream", "tcp")
58-
ok(client ~= nil, "should create client socket")
59-
60-
local connected = client:connect("localhost", port)
61-
ok(connected, "client should connect to server")
62-
63-
local blocking_set = client:set_blocking(false)
64-
ok(blocking_set, "should set client to non-blocking")
46+
local client = assert(socket.create("inet", "stream", "tcp"))
47+
assert(client:connect("localhost", port))
48+
assert(client:set_blocking(false))
6549

6650
local sent_message = false
6751

6852
function update_client()
6953
if client:is_connected() then
7054
if not sent_message then
71-
local sent = client:send("hello")
72-
ok(sent, "client should send 'hello' message")
55+
assert(client:send("hello"))
7356
sent_message = true
7457
else
7558
local str, err = client:receive()
7659

7760
if str then
7861
ok(str == "hello", "client should receive echo 'hello'")
79-
client:close()
62+
assert(client:close())
8063
elseif err ~= "timeout" then
81-
ok(false, "client receive error: " .. tostring(err))
64+
error("client receive error: " .. tostring(err))
8265
end
8366
end
8467
end

0 commit comments

Comments
 (0)