From 4a431e64f3d8b858318a31bcab3dc3202a620b9a Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Sat, 2 Mar 2024 14:23:01 +0100 Subject: [PATCH] fix(test): wrap socket as a copas one --- tests/removeserver.lua | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/tests/removeserver.lua b/tests/removeserver.lua index cf6a280..10928c2 100644 --- a/tests/removeserver.lua +++ b/tests/removeserver.lua @@ -9,19 +9,31 @@ local wskt = socket.bind("*", 0) local whost, wport = wskt:getsockname() wport = tonumber(wport) +-- set up a timeout to not hang on failure +local timeout_timer = copas.timer.new { + delay = 10, + callback = function() + print("timeout!") + os.exit(1) + end +} + +local connection_handler = function(cskt) + print(tostring(cskt).." ("..type(cskt)..") received a connection") + local data, _, partial = cskt:receive() + if partial and not data then + data = partial + end + print("triggered", data) + copas.removeserver(wskt, true) +end + local function wait_for_trigger() - copas.addserver(wskt, function(cskt) - local data, _, partial = cskt:receive() - if partial and not data then - data = partial - end - print("triggered", data) - copas.removeserver(wskt, true) - end) + copas.addserver(wskt, copas.handler(connection_handler), "my_TCP_server") end local function trigger_it(n) - local cskt = socket.tcp() + local cskt = copas.wrap(socket.tcp()) local ok = cskt:connect(whost, wport) if ok then cskt:send("hi "..n) @@ -35,6 +47,7 @@ copas.addthread(function() trigger_it(i) copas.pause(0.1) end + timeout_timer:cancel() end) copas.loop()