summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorEmil Renner Berthing <esmil@mailme.dk>2012-07-15 19:58:28 +0200
committerEmil Renner Berthing <esmil@mailme.dk>2012-07-31 18:47:58 +0200
commit9db0a1b8d4539fe5f7aa678e0ef8fb634fb48bf7 (patch)
tree7c4c361ec7b8de44868036902256b3afe2d2a67b /test
parent78e6e89431e4ef9419a716f246f1f3cfaf7d9dfe (diff)
downloadlem-9db0a1b8d4539fe5f7aa678e0ef8fb634fb48bf7.tar.gz
lem-9db0a1b8d4539fe5f7aa678e0ef8fb634fb48bf7.tar.xz
lem-9db0a1b8d4539fe5f7aa678e0ef8fb634fb48bf7.zip
add more libraries
Diffstat (limited to 'test')
-rwxr-xr-xtest/ctest.lua48
-rwxr-xr-xtest/htest.lua148
-rwxr-xr-xtest/httptest.lua58
-rwxr-xr-xtest/multiplexing.lua79
-rwxr-xr-xtest/sleep.lua46
-rwxr-xr-xtest/stest.lua64
-rwxr-xr-xtest/test.lua48
-rwxr-xr-xtest/test2.lua40
8 files changed, 531 insertions, 0 deletions
diff --git a/test/ctest.lua b/test/ctest.lua
new file mode 100755
index 0000000..e0bb6c0
--- /dev/null
+++ b/test/ctest.lua
@@ -0,0 +1,48 @@
+#!bin/lem
+--
+-- This file is part of LEM, a Lua Event Machine.
+-- Copyright 2011-2012 Emil Renner Berthing
+--
+-- LEM is free software: you can redistribute it and/or
+-- modify it under the terms of the GNU General Public License as
+-- published by the Free Software Foundation, either version 3 of
+-- the License, or (at your option) any later version.
+--
+-- LEM is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+-- GNU General Public License for more details.
+--
+-- You should have received a copy of the GNU General Public License
+-- along with LEM. If not, see <http://www.gnu.org/licenses/>.
+--
+
+print('Entered ' .. arg[0])
+
+local utils = require 'lem.utils'
+local streams = require 'lem.streams'
+
+local sleeper = utils.sleeper()
+local iconn, oconn = assert(streams.tcp_connect('127.0.0.1', arg[1] or 8080))
+
+for i = 1, 10 do
+ assert(oconn:write('ping\n'))
+
+ local line, err = iconn:read('*l')
+ if not line then
+ if err == 'closed' then
+ print("Server closed connection")
+ return
+ end
+
+ error(err)
+ end
+
+ print("Server answered: '" .. line .. "'")
+end
+
+oconn:write('quit\n')
+
+print('Exiting ' .. arg[0])
+
+-- vim: syntax=lua ts=2 sw=2 noet:
diff --git a/test/htest.lua b/test/htest.lua
new file mode 100755
index 0000000..4272118
--- /dev/null
+++ b/test/htest.lua
@@ -0,0 +1,148 @@
+#!bin/lem
+--
+-- This file is part of LEM, a Lua Event Machine.
+-- Copyright 2011-2012 Emil Renner Berthing
+--
+-- LEM is free software: you can redistribute it and/or
+-- modify it under the terms of the GNU General Public License as
+-- published by the Free Software Foundation, either version 3 of
+-- the License, or (at your option) any later version.
+--
+-- LEM is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+-- GNU General Public License for more details.
+--
+-- You should have received a copy of the GNU General Public License
+-- along with LEM. If not, see <http://www.gnu.org/licenses/>.
+--
+
+local utils = require 'lem.utils'
+local hathaway = require 'lem.hathaway'
+hathaway.import()
+
+GET('/', function(req, res)
+ res.status = 302
+ res.headers['Location'] = '/dump'
+end)
+
+GET('/hello', function(req, res)
+ res.headers['Content-Type'] = 'text/plain'
+ res:add('Hello, World!\n')
+end)
+
+GET('/self', function(req, res)
+ res.headers['Content-Type'] = 'text/plain'
+ res.file = arg[0]
+end)
+
+GET('/dump', function(req, res)
+ local accept = req.headers['Accept']
+ if accept and accept:match('application/xhtml%+xml') then
+ res.headers['Content-Type'] = 'application/xhtml+xml'
+ else
+ res.headers['Content-Type'] = 'text/html'
+ end
+ res:add([[
+<?xml version="1.0" encoding="UTF-8"?>
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
+<head>
+ <title>Hathaway HTTP dump</title>
+ <style type="text/css">
+ th { text-align:left; }
+ </style>
+</head>
+<body>
+
+<h2>Request</h2>
+<table>
+ <tr><th>Method:</th><td>%s</td></tr>
+ <tr><th>Uri:</th><td>%s</td></tr>
+ <tr><th>Version:</th><td>%s</td></tr>
+</table>
+
+<h2>Headers</h2>
+<table>
+]], req.method or '', req.uri or '', req.version)
+
+ for k, v in pairs(req.headers) do
+ res:add(' <tr><th>%s</th><td>%s</td></tr>\n', k, v)
+ end
+
+ res:add([[
+</table>
+
+<h2>Body</h2>
+<form action="/form" method="POST" accept-charset="UTF-8">
+ <p>
+ <textarea name="text" cols="80" rows="25"></textarea><br />
+ <input type="submit" value="Submit" />
+ </p>
+</form>
+
+<form action="/quit" method="post">
+ <p>
+ <input type="hidden" name="quit" value="secret" />
+ <input type="submit" value="Quit" />
+ </p>
+</form>
+
+</body>
+</html>
+]])
+end)
+
+local function urldecode(str)
+ return str:gsub('+', ' '):gsub('%%(%x%x)', function (str)
+ return string.char(tonumber(str, 16))
+ end)
+end
+
+local function parseform(str)
+ local t = {}
+ for k, v in str:gmatch('([^&]+)=([^&]*)') do
+ t[urldecode(k)] = urldecode(v)
+ end
+ return t
+end
+
+POST('/form', function(req, res)
+ res.headers['Content-Type'] = 'text/plain'
+ local body =req:body()
+ res:add("You sent:\n%s\n", body)
+ res:add('{\n')
+ for k, v in pairs(parseform(body)) do
+ res:add(" ['%s'] = '%s'\n", k, v)
+ end
+ res:add('}\n')
+end)
+
+GET('/close', function(req, res)
+ res.headers['Content-Type'] = 'text/plain'
+ res.headers['Connection'] = 'close'
+ res:add('This connection should close\n')
+end)
+
+POST('/quit', function(req, res)
+ local body = req:body()
+
+ req.headers['Content-Type'] = 'text/plain'
+
+ if body == 'quit=secret' then
+ res:add("Bye o/\n")
+ hathaway.server:close()
+ else
+ res:add("You didn't supply the right value...\n")
+ end
+end)
+
+GETM('^/hello/([^/]+)$', function(req, res, name)
+ res.headers['Content-Type'] = 'text/plain'
+ res:add('Hello, %s!\n', name)
+end)
+
+hathaway.debug = print
+Hathaway('*', arg[1] or 8080)
+utils.exit(0) -- otherwise open connections will keep us running
+
+-- vim: syntax=lua ts=2 sw=2 noet:
diff --git a/test/httptest.lua b/test/httptest.lua
new file mode 100755
index 0000000..7bc3b93
--- /dev/null
+++ b/test/httptest.lua
@@ -0,0 +1,58 @@
+#!bin/lem
+--
+-- This file is part of LEM, a Lua Event Machine.
+-- Copyright 2011-2012 Emil Renner Berthing
+--
+-- LEM is free software: you can redistribute it and/or
+-- modify it under the terms of the GNU General Public License as
+-- published by the Free Software Foundation, either version 3 of
+-- the License, or (at your option) any later version.
+--
+-- LEM is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+-- GNU General Public License for more details.
+--
+-- You should have received a copy of the GNU General Public License
+-- along with LEM. If not, see <http://www.gnu.org/licenses/>.
+--
+
+local utils = require 'lem.utils'
+local streams = require 'lem.streams'
+local http = require 'lem.http'
+
+local format = string.format
+local concat = table.concat
+
+local done = false
+
+utils.spawn(function()
+ --local istream, ostream = assert(streams.tcp_connect('www.google.dk', 80))
+ local istream, ostream = assert(streams.tcp_connect('127.0.0.1', 8080))
+
+ print('\nConnected.')
+
+ for i = 1, 2 do
+ --assert(ostream:write('GET / HTTP/1.1\r\nHost: www.google.dk\r\nConnection: close\r\n\r\n'))
+ assert(ostream:write('GET / HTTP/1.1\r\nHost: www.google.dk\r\n\r\n'))
+
+ local res = assert(istream:read('HTTPResponse'))
+
+ print(format('\nHTTP/%s %d %s', res.version, res.status, res.text))
+ for k, v in pairs(res.headers) do
+ print(format('%s: %s', k, v))
+ end
+
+ print(format('\n#body = %d', #assert(res:body())))
+ end
+
+ done = true
+end)
+
+local write, yield = io.write, utils.yield
+repeat
+ write('.')
+ yield()
+until done
+
+-- vim: set ts=2 sw=2 noet:
diff --git a/test/multiplexing.lua b/test/multiplexing.lua
new file mode 100755
index 0000000..8265e4c
--- /dev/null
+++ b/test/multiplexing.lua
@@ -0,0 +1,79 @@
+#!bin/lem
+--
+-- This file is part of LEM, a Lua Event Machine.
+-- Copyright 2011-2012 Emil Renner Berthing
+--
+-- LEM is free software: you can redistribute it and/or
+-- modify it under the terms of the GNU General Public License as
+-- published by the Free Software Foundation, either version 3 of
+-- the License, or (at your option) any later version.
+--
+-- LEM is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+-- GNU General Public License for more details.
+--
+-- You should have received a copy of the GNU General Public License
+-- along with LEM. If not, see <http://www.gnu.org/licenses/>.
+--
+
+local utils = require 'lem.utils'
+local streams = require 'lem.streams'
+local queue = require 'lem.streams.queue'
+
+local exit = false
+local ticker = utils.sleeper()
+local stdout = queue.wrap(streams.stdout)
+
+do
+ local format = string.format
+ function queue.QOStream:printf(...)
+ return self:write(format(...))
+ end
+end
+
+-- this function just reads lines from a
+-- stream and prints them to stdout
+local function poll(stream, name)
+ repeat
+ local line, err = stream:read('*l')
+ if not line then
+ stdout:printf('%s: %s\n', name, err)
+ break
+ end
+
+ stdout:printf('%s: %s\n', name, line)
+ until line == 'quit'
+
+ exit = true
+ ticker:wakeup()
+end
+
+-- type 'mkfifo pipe' to create a named pipe for this script
+-- and do 'cat > pipe' (in another terminal) to write to it
+local pipe = assert(streams.open(arg[1] or 'pipe', 'r'))
+
+-- spawn coroutines to read from stdin and the pipe
+utils.spawn(poll, streams.stdin, 'stdin')
+utils.spawn(poll, pipe, 'pipe')
+
+do
+ --local out = streams.stderr
+ local out = stdout
+ local sound
+
+ repeat
+ if sound == 'tick\n' then
+ sound = 'tock\n'
+ else
+ sound = 'tick\n'
+ end
+ out:write(sound)
+ ticker:sleep(1.0)
+ until exit
+end
+
+streams.stdin:close()
+pipe:close()
+
+-- vim: syntax=lua ts=2 sw=2 noet:
diff --git a/test/sleep.lua b/test/sleep.lua
new file mode 100755
index 0000000..41f7fe5
--- /dev/null
+++ b/test/sleep.lua
@@ -0,0 +1,46 @@
+#!bin/lem
+--
+-- This file is part of LEM, a Lua Event Machine.
+-- Copyright 2011-2012 Emil Renner Berthing
+--
+-- LEM is free software: you can redistribute it and/or
+-- modify it under the terms of the GNU General Public License as
+-- published by the Free Software Foundation, either version 3 of
+-- the License, or (at your option) any later version.
+--
+-- LEM is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+-- GNU General Public License for more details.
+--
+-- You should have received a copy of the GNU General Public License
+-- along with LEM. If not, see <http://www.gnu.org/licenses/>.
+--
+
+local utils = require 'lem.utils'
+
+local sum, coros, n = 0, 0, 0
+local done = utils.sleeper()
+
+local function test(t)
+ local sleeper = utils.sleeper()
+ local diff = utils.now()
+ sleeper:sleep(t)
+ diff = utils.now() - diff
+
+ print(string.format('%fs, %fms off', diff, 1000*(diff - t)))
+ sum = sum + math.abs(diff - t)
+ n = n + 1
+ if n == coros then done:wakeup() end
+end
+
+for t = 0, 3, 0.1 do
+ coros = coros + 1
+ utils.spawn(test, t)
+end
+
+done:sleep()
+
+print(string.format('%fms off on average', 1000*sum / n))
+
+-- vim: syntax=lua ts=2 sw=2 noet:
diff --git a/test/stest.lua b/test/stest.lua
new file mode 100755
index 0000000..f54a415
--- /dev/null
+++ b/test/stest.lua
@@ -0,0 +1,64 @@
+#!bin/lem
+--
+-- This file is part of LEM, a Lua Event Machine.
+-- Copyright 2011-2012 Emil Renner Berthing
+--
+-- LEM is free software: you can redistribute it and/or
+-- modify it under the terms of the GNU General Public License as
+-- published by the Free Software Foundation, either version 3 of
+-- the License, or (at your option) any later version.
+--
+-- LEM is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+-- GNU General Public License for more details.
+--
+-- You should have received a copy of the GNU General Public License
+-- along with LEM. If not, see <http://www.gnu.org/licenses/>.
+--
+
+print('Entered ' .. arg[0])
+
+local utils = require 'lem.utils'
+local streams = require 'lem.streams'
+
+local server = assert(streams.tcp4_listen('*', arg[1] or 8080))
+
+--timer(10, function() exit(0) end)
+utils.timer(10, function()
+ print 'Closing server'
+ server:close()
+end)
+
+local ok, err = server:autospawn(function(i, o)
+ print 'Accepted a connection'
+ local sleeper = utils.sleeper()
+
+ while true do
+ local line, err = i:read('*l')
+ if not line then
+ if err == 'closed' then
+ print("Client closed connection")
+ return
+ end
+
+ error(err)
+ end
+
+ print("Client sent: '" .. line .. "'")
+ if line ~= 'ping' then break end
+
+ sleeper:sleep(0.4)
+ assert(o:write('pong\n'))
+ end
+
+ print "Ok, I'm out"
+ assert(i:close())
+ assert(o:close())
+end)
+
+if not ok and err ~= 'interrupted' then error(err) end
+
+print('Exiting ' .. arg[0])
+
+-- vim: syntax=lua ts=2 sw=2 noet:
diff --git a/test/test.lua b/test/test.lua
new file mode 100755
index 0000000..57d76f2
--- /dev/null
+++ b/test/test.lua
@@ -0,0 +1,48 @@
+#!bin/lem
+--
+-- This file is part of LEM, a Lua Event Machine.
+-- Copyright 2011-2012 Emil Renner Berthing
+--
+-- LEM is free software: you can redistribute it and/or
+-- modify it under the terms of the GNU General Public License as
+-- published by the Free Software Foundation, either version 3 of
+-- the License, or (at your option) any later version.
+--
+-- LEM is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+-- GNU General Public License for more details.
+--
+-- You should have received a copy of the GNU General Public License
+-- along with LEM. If not, see <http://www.gnu.org/licenses/>.
+--
+
+local utils = require 'lem.utils'
+
+local function sleep(n)
+ utils.sleeper():sleep(n)
+end
+
+--print('package.cpath = ', package.cpath)
+
+print 'Saying booh in 2.5 seconds'
+
+utils.timer(2.5, function() print 'Booh!' end)
+
+print 'Sleeping 5 seconds'
+
+sleep(5)
+
+print 'Done sleeping'
+
+print 'Spawning new thread..'
+utils.spawn(function(s) print(s) end, "I'm the new thread!")
+
+print 'Yielding..'
+utils.yield()
+
+print 'Back.'
+
+print 'Bye!'
+
+-- vim: syntax=lua ts=3 sw=3 et:
diff --git a/test/test2.lua b/test/test2.lua
new file mode 100755
index 0000000..b64eedf
--- /dev/null
+++ b/test/test2.lua
@@ -0,0 +1,40 @@
+#!bin/lem
+--
+-- This file is part of LEM, a Lua Event Machine.
+-- Copyright 2011-2012 Emil Renner Berthing
+--
+-- LEM is free software: you can redistribute it and/or
+-- modify it under the terms of the GNU General Public License as
+-- published by the Free Software Foundation, either version 3 of
+-- the License, or (at your option) any later version.
+--
+-- LEM is distributed in the hope that it will be useful,
+-- but WITHOUT ANY WARRANTY; without even the implied warranty of
+-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+-- GNU General Public License for more details.
+--
+-- You should have received a copy of the GNU General Public License
+-- along with LEM. If not, see <http://www.gnu.org/licenses/>.
+--
+
+print 'Entered test.lua'
+
+local utils = require 'lem.utils'
+
+local function sleep(n)
+ utils.sleeper():sleep(n)
+end
+
+print 'Saying "Fee!" in 1 second'
+utils.timer(1, function() print 'Fee!' end)
+
+print 'Saying "Fo!" in 3 seconds'
+utils.timer(3, function() print 'Fo!' end)
+
+utils.spawn(function()
+ print 'Sleeping for 2 seconds, then saying "Fi!" before the script ends'
+ sleep(2)
+ print 'Fi!'
+end)
+
+-- vim: syntax=lua ts=3 sw=3 et: