Skip to content

Commit

Permalink
fix(test): wrap socket as a copas one
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Mar 2, 2024
1 parent dea9a3b commit 4a431e6
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions tests/removeserver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -35,6 +47,7 @@ copas.addthread(function()
trigger_it(i)
copas.pause(0.1)
end
timeout_timer:cancel()
end)

copas.loop()

0 comments on commit 4a431e6

Please sign in to comment.