Updated for LuaSocket 1.2.

More tests added.
This commit is contained in:
Diego Nehab 2001-01-25 21:59:59 +00:00
parent 7096b8df82
commit 2bb209ab9e

View File

@ -1,359 +1,428 @@
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- LuaSocket automated test module -- LuaSocket automated test module
-- client.lua -- client.lua
-- This is the client module. It connects with the server module and executes -- This is the client module. It connects with the server module and executes
-- all tests. -- all tests.
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Prints a header to separate the test phases -- Prints a header to separate the test phases
-- Input -- Input
-- test: test phase name -- test: test phase name
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
function new_test(test) function new_test(test)
write("----------------------------------------------\n", write("----------------------------------------------\n",
test, "\n", test, "\n",
"----------------------------------------------\n") "----------------------------------------------\n")
end end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Read command definitions and stablish control connection -- Get host and port from command line
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
new_test("initializing...") HOST = "127.0.0.1"
dofile("command.lua") PORT = 2020
test_debug_mode() if arg then
while control == nil do HOST = arg[1] or HOST
print("client: trying control connection...") PORT = arg[2] or PORT
control, err = connect(HOST, PORT) end
if control then
print("client: control connection stablished!") -----------------------------------------------------------------------------
else -- Read command definitions
sleep(2) -----------------------------------------------------------------------------
end assert(dofile("testcmd.lua"))
end test_debug_mode()
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Make sure server is ready for data transmission -- Start control connection
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
function sync() new_test("initializing...")
send_command(SYNC) while control == nil do
get_command() print("client: trying control connection...")
end control, err = connect(HOST, PORT)
if control then
----------------------------------------------------------------------------- print("client: control connection stablished!")
-- Close and reopen data connection, to get rid of any unread blocks else
----------------------------------------------------------------------------- sleep(2)
function reconnect() end
if data then end
data:close()
send_command(CLOSE) -----------------------------------------------------------------------------
data = nil -- Make sure server is ready for data transmission
end -----------------------------------------------------------------------------
while data == nil do function sync()
send_command(CONNECT) send_command(SYNC)
data = connect(HOST, PORT) get_command()
if not data then end
print("client: waiting for data connection.")
sleep(1) -----------------------------------------------------------------------------
end -- Close and reopen data connection, to get rid of any unread blocks
end -----------------------------------------------------------------------------
sync() function reconnect()
end if data then
close(data)
----------------------------------------------------------------------------- send_command(CLOSE)
-- Tests the command connection data = nil
----------------------------------------------------------------------------- end
function test_command(cmd, par) while data == nil do
local cmd_back, par_back send_command(CONNECT)
reconnect() data = connect(HOST, PORT)
send_command(COMMAND) if not data then
write("testing command ") print("client: waiting for data connection.")
print_command(cmd, par) sleep(1)
send_command(cmd, par) end
cmd_back, par_back = get_command() end
if cmd_back ~= cmd or par_back ~= par then sync()
fail(cmd) end
else
pass() -----------------------------------------------------------------------------
end -- Tests the command connection
end -----------------------------------------------------------------------------
function test_command(cmd, par)
----------------------------------------------------------------------------- local cmd_back, par_back
-- Tests ASCII line transmission reconnect()
-- Input send_command(COMMAND)
-- len: length of line to be tested write("testing command ")
----------------------------------------------------------------------------- print_command(cmd, par)
function test_asciiline(len) send_command(cmd, par)
local str, str10, back, err cmd_back, par_back = get_command()
reconnect() if cmd_back ~= cmd or par_back ~= par then
send_command(ECHO_LINE) fail(cmd)
str = strrep("x", mod(len, 10)) else
str10 = strrep("aZb.c#dAe?", floor(len/10)) pass()
str = str .. str10 end
write("testing ", len, " byte(s) line\n") end
err = data:send(str, "\n")
if err then fail(err) end -----------------------------------------------------------------------------
back, err = data:receive() -- Tests ASCII line transmission
if err then fail(err) end -- Input
if back == str then pass("lines match") -- len: length of line to be tested
else fail("lines don't match") end -----------------------------------------------------------------------------
end function test_asciiline(len)
local str, str10, back, err
----------------------------------------------------------------------------- reconnect()
-- Tests closed connection detection send_command(ECHO_LINE)
----------------------------------------------------------------------------- str = strrep("x", mod(len, 10))
function test_closed() str10 = strrep("aZb.c#dAe?", floor(len/10))
local str = "This is our little test line" str = str .. str10
local len = strlen(str) write("testing ", len, " byte(s) line\n")
local back, err, total err = send(data, str, "\n")
reconnect() if err then fail(err) end
print("testing close while reading line") back, err = receive(data)
send_command(ECHO_BLOCK, len) if err then fail(err) end
data:send(str) if back == str then pass("lines match")
send_command(CLOSE) else fail("lines don't match") end
-- try to get a line end
back, err = data:receive()
if not err then fail("shold have gotten 'closed'.") -----------------------------------------------------------------------------
elseif err ~= "closed" then fail("got '"..err.."' instead of 'closed'.") -- Tests closed connection detection
elseif str ~= back then fail("didn't receive what i should 'closed'.") -----------------------------------------------------------------------------
else pass("rightfull 'closed' received") end function test_closed()
reconnect() local str = "This is our little test line"
print("testing close while reading block") local len = strlen(str)
send_command(ECHO_BLOCK, len) local back, err, total
data:send(str) reconnect()
send_command(CLOSE) print("testing close while reading line")
-- try to get a line send_command(ECHO_BLOCK, len)
back, err = data:receive(2*len) send(data, str)
if not err then fail("shold have gotten 'closed'.") send_command(CLOSE)
elseif err ~= "closed" then fail("got '"..err.."' instead of 'closed'.") -- try to get a line
elseif str ~= back then fail("didn't receive what I should.") back, err = receive(data)
else pass("rightfull 'closed' received") end if not err then fail("shold have gotten 'closed'.")
end elseif err ~= "closed" then fail("got '"..err.."' instead of 'closed'.")
elseif str ~= back then fail("didn't receive what i should 'closed'.")
----------------------------------------------------------------------------- else pass("rightfull 'closed' received") end
-- Tests binary line transmission reconnect()
-- Input print("testing close while reading block")
-- len: length of line to be tested send_command(ECHO_BLOCK, len)
----------------------------------------------------------------------------- send(data, str)
function test_rawline(len) send_command(CLOSE)
local str, str10, back, err -- try to get a line
reconnect() back, err = receive(data, 2*len)
send_command(ECHO_LINE) if not err then fail("shold have gotten 'closed'.")
str = strrep(strchar(47), mod(len, 10)) elseif err ~= "closed" then fail("got '"..err.."' instead of 'closed'.")
str10 = strrep(strchar(120,21,77,4,5,0,7,36,44,100), floor(len/10)) elseif str ~= back then fail("didn't receive what I should.")
str = str .. str10 else pass("rightfull 'closed' received") end
write("testing ", len, " byte(s) line\n") end
err = data:send(str, "\n")
if err then fail(err) end -----------------------------------------------------------------------------
back, err = data:receive() -- Tests binary line transmission
if err then fail(err) end -- Input
if back == str then pass("lines match") -- len: length of line to be tested
else fail("lines don't match") end -----------------------------------------------------------------------------
end function test_rawline(len)
local str, str10, back, err
----------------------------------------------------------------------------- reconnect()
-- Tests block transmission send_command(ECHO_LINE)
-- Input str = strrep(strchar(47), mod(len, 10))
-- len: length of block to be tested str10 = strrep(strchar(120,21,77,4,5,0,7,36,44,100), floor(len/10))
----------------------------------------------------------------------------- str = str .. str10
function test_block(len) write("testing ", len, " byte(s) line\n")
local half = floor(len/2) err = send(data, str, "\n")
local s1, s2, back, err if err then fail(err) end
reconnect() back, err = receive(data)
send_command(ECHO_BLOCK, len) if err then fail(err) end
write("testing ", len, " byte(s) block\n") if back == str then pass("lines match")
s1 = strrep("x", half) else fail("lines don't match") end
err = data:send(s1) end
if err then fail(err) end
sleep(1) -----------------------------------------------------------------------------
s2 = strrep("y", len-half) -- Tests block transmission
err = data:send(s2) -- Input
if err then fail(err) end -- len: length of block to be tested
back, err = data:receive(len) -----------------------------------------------------------------------------
if err then fail(err) end function test_block(len)
if back == s1..s2 then pass("blocks match") local half = floor(len/2)
else fail("blocks don't match") end local s1, s2, back, err
end reconnect()
send_command(ECHO_BLOCK, len)
----------------------------------------------------------------------------- write("testing ", len, " byte(s) block\n")
-- Tests if return-timeout was respected s1 = strrep("x", half)
-- delta: time elapsed during transfer err = send(data, s1)
-- t: timeout value if err then fail(err) end
-- err: error code returned by I/O operation s2 = strrep("y", len-half)
----------------------------------------------------------------------------- err = send(data, s2)
function returntimed_out(delta, t, err) if err then fail(err) end
if err == "timeout" then back, err = receive(data, len)
if delta + 0.1 >= t then if err then fail(err) end
pass("got right timeout") if back == s1..s2 then pass("blocks match")
return 1 else fail("blocks don't match") end
else end
fail("shouldn't have gotten timeout")
end -----------------------------------------------------------------------------
elseif delta > t then -- Tests if return-timeout was respected
fail("should have gotten timeout") -- delta: time elapsed during transfer
end -- t: timeout value
end -- s: time server slept
-- err: error code returned by I/O operation
----------------------------------------------------------------------------- -- o: operation being executed
-- Tests if return-timeout was respected -----------------------------------------------------------------------------
-- delta: time elapsed during transfer function blockedtimed_out(t, s, err, o)
-- t: timeout value if err == "timeout" then
-- err: error code returned by I/O operation if s >= t then
-- o: operation being executed pass("got rightfull forced timeout")
----------------------------------------------------------------------------- return 1
function blockedtimed_out(t, s, err, o) else
if err == "timeout" then pass("got natural cause timeout")
if s >= t then return 1
pass("got right forced timeout") end
return 1 elseif 0.9*s > t then
else if o == "send" then
pass("got natural cause timeout (may be wrong)") pass("must have been buffered (may be wrong)")
return 1 else
end fail("should have gotten timeout")
elseif s > t then end
if o == "send" then end
pass("must have been buffered (may be wrong)") end
else
fail("should have gotten timeout") -----------------------------------------------------------------------------
end -- Tests blocked-timeout conformance
end -- Input
end -- len: length of block to be tested
-- t: timeout value
----------------------------------------------------------------------------- -- s: server sleep between transfers
-- Tests blocked-timeout conformance -----------------------------------------------------------------------------
-- Input function test_blockedtimeout(len, t, s)
-- len: length of block to be tested local str, err, back, total
-- t: timeout value reconnect()
-- s: server sleep between transfers send_command(RECEIVE_BLOCK, len)
----------------------------------------------------------------------------- send_command(SLEEP, s)
function test_blockedtimeout(len, t, s) send_command(RECEIVE_BLOCK, len)
local str, err, back, total write("testing ", len, " bytes, ", t,
reconnect() "s block timeout, ", s, "s sleep\n")
send_command(RECEIVE_BLOCK, len) timeout(data, t)
send_command(SLEEP, s) str = strrep("a", 2*len)
send_command(RECEIVE_BLOCK, len) err, total = send(data, str)
write("testing ", len, " bytes, ", t, if blockedtimed_out(t, s, err, "send") then return end
"s block timeout, ", s, "s sleep\n") if err then fail(err) end
data:timeout(t) send_command(SEND_BLOCK)
str = strrep("a", 2*len) send_command(SLEEP, s)
err, total = data:send(str) send_command(SEND_BLOCK)
if blockedtimed_out(t, s, err, "send") then return end back, err = receive(data, 2*len)
if err then fail(err) end if blockedtimed_out(t, s, err, "receive") then return end
send_command(SEND_BLOCK) if err then fail(err) end
send_command(SLEEP, s) if back == str then pass("blocks match")
send_command(SEND_BLOCK) else fail("blocks don't match") end
back, err = data:receive(2*len) end
if blockedtimed_out(t, s, err, "receive") then return end
if err then fail(err) end -----------------------------------------------------------------------------
if back == str then pass("blocks match") -- Tests if return-timeout was respected
else fail("blocks don't match") end -- delta: time elapsed during transfer
end -- t: timeout value
-- err: error code returned by I/O operation
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Tests return-timeout conformance function returntimed_out(delta, t, err)
-- Input if err == "timeout" then
-- len: length of block to be tested if 1.1*delta >= t then
-- t: timeout value pass("got rightfull timeout")
-- s: server sleep between transfers return 1
----------------------------------------------------------------------------- else
function test_returntimeout(len, t, s) fail("shouldn't have gotten timeout")
local str, err, back, delta, total end
reconnect() elseif 0.9*delta > t then
send_command(RECEIVE_BLOCK, len) fail("should have gotten timeout")
send_command(SLEEP, s) end
send_command(RECEIVE_BLOCK, len) end
write("testing ", len, " bytes, ", t,
"s return timeout, ", s, "s sleep\n") -----------------------------------------------------------------------------
data:timeout(t, "return") -- Tests return-timeout conformance
str = strrep("a", 2*len) -- Input
err, total, delta = data:send(str) -- len: length of block to be tested
print("delta: " .. delta) -- t: timeout value
if returntimed_out(delta, t, err) then return end -- s: server sleep between transfers
if err then fail(err) end -----------------------------------------------------------------------------
send_command(SEND_BLOCK) function test_returntimeout(len, t, s)
send_command(SLEEP, s) local str, err, back, delta, total
send_command(SEND_BLOCK) reconnect()
back, err, delta = data:receive(2*len) send_command(RECEIVE_BLOCK, len)
print("delta: " .. delta) send_command(SLEEP, s)
if returntimed_out(delta, t, err) then return end send_command(RECEIVE_BLOCK, len)
if err then fail(err) end write("testing ", len, " bytes, ", t,
if back == str then pass("blocks match") "s return timeout, ", s, "s sleep\n")
else fail("blocks don't match") end timeout(data, t, "return")
end str = strrep("a", 2*len)
err, total, delta = send(data, str)
----------------------------------------------------------------------------- print("sent in " .. delta .. "s")
-- Execute all tests if returntimed_out(delta, t, err) then return end
----------------------------------------------------------------------------- if err then fail("unexpected error: " .. err) end
new_test("control connection test") send_command(SEND_BLOCK)
test_command(EXIT) send_command(SLEEP, s)
test_command(CONNECT) send_command(SEND_BLOCK)
test_command(CLOSE) back, err, delta = receive(data, 2*len)
test_command(ECHO_BLOCK, 12234) print("received in " .. delta .. "s")
test_command(SLEEP, 1111) if returntimed_out(delta, t, err) then return end
test_command(ECHO_LINE) if err then fail("unexpected error: " .. err) end
if back == str then pass("blocks match")
new_test("connection close test") else fail("blocks don't match") end
test_closed() end
new_test("binary string test") -----------------------------------------------------------------------------
test_rawline(1) -- Tests return-timeout conformance
test_rawline(17) -----------------------------------------------------------------------------
test_rawline(200) function test_patterns()
test_rawline(3000) local dos_line1 = "this the first dos line"
test_rawline(8000) local dos_line2 = "this is another dos line"
test_rawline(40000) local unix_line1 = "this the first unix line"
local unix_line2 = "this is another unix line"
new_test("blocking transfer test") local block = dos_line1 .. "\r\n" .. dos_line2 .. "\r\n"
test_block(1) reconnect()
test_block(17) block = block .. unix_line1 .. "\n" .. unix_line2 .. "\n"
test_block(200) block = block .. block
test_block(3000) send_command(ECHO_BLOCK, strlen(block))
test_block(80000) err = send(data, block)
test_block(800000) if err then fail(err) end
local back = receive(data, "*l")
new_test("non-blocking transfer test") if back ~= dos_line1 then fail("'*l' failed") end
-- the value is not important, we only want back = receive(data, "*l")
-- to test non-blockin I/O anyways if back ~= dos_line2 then fail("'*l' failed") end
data:timeout(200) back = receive(data, "*lu")
test_block(1) if back ~= unix_line1 then fail("'*lu' failed") end
test_block(17) back = receive(data, "*lu")
test_block(200) if back ~= unix_line2 then fail("'*lu' failed") end
test_block(3000) back = receive(data)
test_block(80000) if back ~= dos_line1 then fail("default failed") end
test_block(800000) back = receive(data)
test_block(8000000) if back ~= dos_line2 then fail("default failed") end
back = receive(data, "*lu")
new_test("character string test") if back ~= unix_line1 then fail("'*lu' failed") end
test_asciiline(1) back = receive(data, "*lu")
test_asciiline(17) if back ~= unix_line2 then fail("'*lu' failed") end
test_asciiline(200) pass("line patterns are ok")
test_asciiline(3000) send_command(ECHO_BLOCK, strlen(block))
test_asciiline(8000) err = send(data, block)
test_asciiline(40000) if err then fail(err) end
back = receive(data, strlen(block))
new_test("return timeout test") if back ~= block then fail("number failed") end
test_returntimeout(80, .5, 1) pass("number is ok")
test_returntimeout(80, 1, 0.5) send_command(ECHO_BLOCK, strlen(block))
test_returntimeout(8000, .5, 0) send_command(SLEEP, 1)
test_returntimeout(80000, .5, 0) send_command(CLOSE)
test_returntimeout(800000, .5, 0) err = send(data, block)
if err then fail(err) end
new_test("blocked timeout test") back = receive(data, "*a")
test_blockedtimeout(80, .5, 1) if back ~= block then fail("'*a' failed") end
test_blockedtimeout(80, 1, 1) pass("'*a' is ok")
test_blockedtimeout(80, 1.5, 1) end
test_blockedtimeout(800, 1, 0)
test_blockedtimeout(8000, 1, 0) -----------------------------------------------------------------------------
test_blockedtimeout(80000, 1, 0) -- Execute all tests
test_blockedtimeout(800000, 1, 0) -----------------------------------------------------------------------------
start = time()
-----------------------------------------------------------------------------
-- Close connection and exit server. We are done. new_test("control connection test")
----------------------------------------------------------------------------- test_command(EXIT)
new_test("the library has passed all tests") test_command(CONNECT)
print("client: closing connection with server") test_command(CLOSE)
send_command(CLOSE) test_command(ECHO_BLOCK, 12234)
send_command(EXIT) test_command(SLEEP, 1111)
control:close() test_command(ECHO_LINE)
print("client: exiting...")
exit() new_test("connection close test")
test_closed()
new_test("read pattern test")
test_patterns()
new_test("character string test")
test_asciiline(1)
test_asciiline(17)
test_asciiline(200)
test_asciiline(3000)
test_asciiline(80000)
test_asciiline(800000)
new_test("binary string test")
test_rawline(1)
test_rawline(17)
test_rawline(200)
test_rawline(3000)
test_rawline(8000)
test_rawline(80000)
test_rawline(800000)
new_test("blocking transfer test")
test_block(1)
test_block(17)
test_block(200)
test_block(3000)
test_block(80000)
test_block(800000)
new_test("non-blocking transfer test")
-- the value is not important, we only want
-- to test non-blockin I/O anyways
timeout(data, 200)
test_block(1)
test_block(17)
test_block(200)
test_block(3000)
test_block(80000)
test_block(800000)
new_test("blocked timeout test")
test_blockedtimeout(80, .5, 1)
test_blockedtimeout(80, 1, 1)
test_blockedtimeout(80, 1.5, 1)
test_blockedtimeout(800, 1, 0)
test_blockedtimeout(8000, 1, 1.5)
test_blockedtimeout(80000, 1, 0)
test_blockedtimeout(800000, 0.01, 0)
new_test("return timeout test")
test_returntimeout(80, 1, 0.5)
test_returntimeout(80, 0.5, 1)
test_returntimeout(8000, .5, 1)
test_returntimeout(80000, 1, 0.5)
test_returntimeout(800000, 1, 0.5)
-----------------------------------------------------------------------------
-- Close connection and exit server. We are done.
-----------------------------------------------------------------------------
print("client: closing connection with server")
send_command(CLOSE)
send_command(EXIT)
close(control)
new_test("the library has passed all tests")
print(format("time elapsed: %6.2fs", time() - start))
print("client: exiting...")
exit()