luaevent/test/test.lua

Sun, 10 Jun 2007 16:01:26 +0000

author
Thomas Harning Jr <harningt@gmail.com>
date
Sun, 10 Jun 2007 16:01:26 +0000
changeset 0
f2e807614be9
child 2
01b3a96ae760
permissions
-rw-r--r--

Initial commit:
* Created tree structure
* Committed current version

0
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
1 -- Tests Copas with a simple Echo server
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
2 --
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
3 -- Run the test file and the connect to the server by telnet on the used port
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
4 -- to stop the test just send the command "quit"
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
5
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
6 require"luaevent"
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
7 require"socket"
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
8 local function echoHandler(skt)
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
9 while true do
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
10 print(skt)
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
11 local data,ret = luaevent.receive(skt, 10)
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
12 print("GOT: ", data, ret)
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
13 if data == "quit" or ret == 'closed' then
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
14 break
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
15 end
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
16 luaevent.send(skt, data)
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
17 end
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
18 print("DONE")
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
19 end
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
20 local function setupHook(thread)
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
21 if not thread then debug.sethook(function(event) print("TRACE >: ", debug.getinfo(2, 'n').name) end, 'c')
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
22 else debug.sethook(thread, function(event) print("TRACE ", thread,">: ", debug.getinfo(2, 'n').name) end, 'c') end
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
23 end
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
24 local server = assert(socket.bind("localhost", 20000))
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
25 server:settimeout(0)
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
26 setupHook()
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
27 local coro = coroutine.create
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
28 coroutine.create = function(...)
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
29 local ret = coro(...)
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
30 setupHook(ret)
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
31 return ret
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
32 end
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
33 luaevent.addserver(server, echoHandler)
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
34 luaevent.loop()

mercurial