From 87e8737218c732011fc7c6a620c432e29b32f3de Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Sun, 18 Jan 2004 05:35:56 +0000 Subject: [PATCH] Fixed bug in accept, added tests for it and for connect with timeout. --- src/inet.c | 1 + test/testclnt.lua | 62 ++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/src/inet.c b/src/inet.c index 80c488b..282d616 100644 --- a/src/inet.c +++ b/src/inet.c @@ -259,6 +259,7 @@ const char *inet_tryaccept(p_sock ps, p_tm tm, p_sock pc) /* loop until connection accepted or timeout happens */ do err = sock_accept(ps, pc, (SA *) &addr, &addr_len, tm_getretry(tm)); while (err == IO_RETRY && tm_getretry(tm) != 0); + if (err == IO_RETRY) err = IO_TIMEOUT; return io_strerror(err); } diff --git a/test/testclnt.lua b/test/testclnt.lua index 3dea831..4d66193 100644 --- a/test/testclnt.lua +++ b/test/testclnt.lua @@ -346,6 +346,39 @@ function test_selectbugs() pass("invalid input: ok") end +------------------------------------------------------------------------ +function accept_timeout() + local s, e = socket.bind("*", 0, 0) + assert(s, e) + local t = socket.time() + s:settimeout(1) + local c, e = s:accept() + assert(not c, "should not accept") + assert(e == "timeout", "wrong error message") + assert(socket.time() - t < 2, "took to long to give up") + s:close() + pass("good") +end + +------------------------------------------------------------------------ +function connect_timeout() + local s, e = socket.bind("*", 0, 0) + assert(s, e) + i, p = s:getsockname() + assert(i, p) + local t = socket.time() + local c, e = socket.tcp() + assert(c, e) + c:settimeout(1) + local r, e = c:connect("localhost", p) + assert(not r and e == "timeout", "wrong error message") + assert(socket.time() - t < 2, "took to long to give up") + pass("good") + s:close() + c:close() +end + +------------------------------------------------------------------------ test("method registration") test_methods(socket.tcp(), { "connect", @@ -377,6 +410,24 @@ test_methods(socket.udp(), { "close", }) +test("select function") +test_selectbugs() + +test("empty host connect: ") +empty_connect() + +test("active close: ") +active_close() + +test("closed connection detection: ") +test_closed() + +test("accept with timeout (if it hangs, it failed:)") +accept_timeout() + +test("accept with timeout (if it hangs, it failed:)") +connect_timeout() + test("mixed patterns") reconnect() test_mixed(1) @@ -448,17 +499,6 @@ test_raw(200) test_raw(17) test_raw(1) -test("select function") -test_selectbugs() - -test("empty host connect: ") -empty_connect() - -test("active close: ") -active_close() - -test("closed connection detection: ") -test_closed() a = [[ test("total timeout on send")