headers: updates Lua 5.2 workaround to allow 5.3 version
[luaevent.git] / test / test.lua
blobb7485421d43e04a6448b1875433a02119858b4b5
1 -- Tests Copas with a simple Echo server
2 --
3 -- Run the test file and the connect to the server by telnet on the used port
4 -- to stop the test just send the command "quit"
6 local luaevent = require("luaevent")
7 local socket = require("socket")
9 local oldPrint = print
10 print = function(...)
11 oldPrint("SRV", ...)
12 end
14 local function echoHandler(skt)
15 while true do
16 local data,ret = luaevent.receive(skt, 10)
17 --print("GOT: ", data, ret)
18 if data == "quit" or ret == 'closed' then
19 break
20 end
21 luaevent.send(skt, data)
22 collectgarbage()
23 end
24 skt:close()
25 --print("DONE")
26 end
27 local server = assert(socket.bind("localhost", 20000))
28 server:settimeout(0)
29 local coro = coroutine.create
30 coroutine.create = function(...)
31 local ret = coro(...)
32 return ret
33 end
34 luaevent.addserver(server, echoHandler)
35 luaevent.loop()