Skip to content

Commit 3aab708

Browse files
committed
rework tests
1 parent 055852c commit 3aab708

3 files changed

Lines changed: 27 additions & 40 deletions

File tree

test/dns_lookup_test.lua

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
require("luacov")
2-
local lunit = require("lunit")
31
local socket = require("ljsocket")
42

5-
module("dns_lookup_test", lunit.testcase, package.seeall)
6-
7-
function test_lookup_google()
3+
do -- lookup google
84
results = socket.get_address_info({host = "www.google.com"})
9-
lunit.assert_not_nil(results)
10-
lunit.assert_not_equal(0, #results)
5+
assert(type(results) ~= nil)
6+
assert(#results ~= 0)
117

128
for i, info in ipairs(results) do
13-
lunit.assert_equal("www.google.com", info.host)
14-
lunit.assert_not_nil(info.family)
15-
lunit.assert_not_nil(info.protocol)
9+
assert(info.host == "www.google.com")
10+
assert(info.family)
11+
assert(info.protocol)
1612
end
17-
end
18-
19-
lunit.main(...)
13+
end

test/run.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require("test.dns_lookup_test")
2+
require("test.tcp_client_test")

test/tcp_client_test.lua

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
require("luacov")
2-
local lunit = require("lunit")
31
local socket = require("ljsocket")
42

5-
module("tcp_test", lunit.testcase, package.seeall)
6-
7-
function tcp_client_blocking_test()
3+
do -- tcp client blocking test
84
local host = "www.freebsd.no"
9-
local socket = socket.create("inet", "stream", "tcp")
10-
lunit.assert(socket)
11-
lunit.assert(socket:connect(host, "http"))
12-
lunit.assert(socket:send(
5+
local socket = assert(socket.create("inet", "stream", "tcp"))
6+
assert(socket:connect(host, "http"))
7+
assert(socket:send(
138
"GET / HTTP/1.1\r\n"..
149
"Host: " .. host .. "\r\n"..
1510
"User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0\r\n"..
@@ -25,8 +20,7 @@ function tcp_client_blocking_test()
2520
local str = ""
2621

2722
while true do
28-
local chunk = socket:receive()
29-
lunit.assert(chunk)
23+
local chunk = assert(socket:receive())
3024

3125
if not chunk then
3226
break
@@ -44,24 +38,23 @@ function tcp_client_blocking_test()
4438
end
4539
end
4640

47-
lunit.assert_true(total_length > 1024)
48-
lunit.assert_true(string.find(str, "HTTP/1.1 200 OK") > 0)
49-
lunit.assert_true(string.find(str, "</html>") > 0)
41+
assert(total_length > 1024)
42+
assert(string.find(str, "HTTP/1.1 200 OK", nil, true) > 0)
43+
assert(string.find(str, "</html>", nil, true) > 0)
5044
end
5145

52-
function tcp_client_blocking_test()
46+
do -- tcp client non-blocking test
5347
local host = "www.freebsd.no"
54-
local socket = socket.create("inet", "stream", "tcp")
55-
lunit.assert(socket)
56-
lunit.assert(socket:connect(host, "http"))
57-
lunit.assert(socket:set_blocking(false))
48+
local socket = assert(socket.create("inet", "stream", "tcp"))
49+
assert(socket:connect(host, "http"))
50+
assert(socket:set_blocking(false))
5851

5952
local str = ""
6053
local total_length
6154

6255
while true do
6356
if socket:is_connected() then
64-
lunit.assert(socket:send(
57+
assert(socket:send(
6558
"GET / HTTP/1.1\r\n"..
6659
"Host: "..host.."\r\n"..
6760
"User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0\r\n"..
@@ -87,7 +80,7 @@ function tcp_client_blocking_test()
8780
if #str >= total_length then
8881
return
8982
end
90-
elseif num ~= 11 then
83+
elseif err ~= "timeout" then
9184
error(err)
9285
end
9386
end
@@ -96,9 +89,7 @@ function tcp_client_blocking_test()
9689
end
9790
end
9891

99-
lunit.assert_true(total_length > 1024)
100-
lunit.assert_true(string.find(str, "HTTP/1.1 200 OK") > 0)
101-
lunit.assert_true(string.find(str, "</html>") > 0)
102-
end
103-
104-
lunit.main(...)
92+
assert(total_length > 1024)
93+
assert(string.find(str, "HTTP/1.1 200 OK", nil, true) > 0)
94+
assert(string.find(str, "</html>", nil, true) > 0)
95+
end

0 commit comments

Comments
 (0)