aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/kill.lua17
-rw-r--r--tests/tests.lua45
-rw-r--r--tests/units/net-nested.lua27
-rw-r--r--tests/units/net-parameter.lua29
-rw-r--r--tests/units/net-query.lua30
-rw-r--r--tests/units/thread-buffer.lua21
-rw-r--r--tests/units/thread-local-return.lua13
-rw-r--r--tests/units/thread-locals.lua12
-rw-r--r--tests/units/thread-nested.lua22
9 files changed, 216 insertions, 0 deletions
diff --git a/tests/kill.lua b/tests/kill.lua
new file mode 100644
index 0000000..a1fa218
--- /dev/null
+++ b/tests/kill.lua
@@ -0,0 +1,17 @@
+local llby = require"lullaby"
+
+local t, e = llby.thread.async(function(res)
+ for i = 1, 50 do
+ print(i)
+ os.execute("sleep 1")
+ end
+end)
+
+print(t, e)
+
+print("killing")
+t:close()
+t:await()
+print("after kill")
+
+os.execute("sleep 0.1");
diff --git a/tests/tests.lua b/tests/tests.lua
new file mode 100644
index 0000000..39c4d12
--- /dev/null
+++ b/tests/tests.lua
@@ -0,0 +1,45 @@
+llby = require("lullaby")
+PORT = 5552
+
+local failed = {}
+local total = 0
+
+function yay(M)
+ print(string.format("\27[32m%s\27[0m passed", M))
+end
+
+function nay(M)
+ print(string.format("\27[31m%s\27[0m failed", M))
+end
+
+local search = ""
+if arg[1] ~= nil then
+ search = "*" .. arg[1] .. "*"
+end
+
+local handle = assert(io.popen("find tests/units/".. search .." -type f"))
+
+for file in handle:lines() do
+ total = total + 1
+ local f = loadfile(file)()
+
+ if f == true then
+ yay(file)
+ else
+ nay(file)
+ table.insert(failed, file)
+ end
+end
+
+if #failed > 0 then
+ print("\n--- failed units (".. #failed .."/".. total ..") ---")
+ for _,fail in ipairs(failed) do
+ nay(fail)
+ end
+ print("--- failed units (".. #failed .."/".. total ..") ---")
+else
+ print("passed all (".. total ..")")
+end
+
+handle:close()
+
diff --git a/tests/units/net-nested.lua b/tests/units/net-nested.lua
new file mode 100644
index 0000000..a9bbcb3
--- /dev/null
+++ b/tests/units/net-nested.lua
@@ -0,0 +1,27 @@
+local bserver = llby.thread.buffer(nil)
+
+local net = llby.thread.async(function(tres)
+ llby.net.listen(function(server)
+ _G.server = server
+ bserver:set(server)
+
+ server:GET("/c", function(res, req)
+ _G.server:GET("/test", function(res, req)
+ res:send("555221")
+ end)
+ res:send("")
+ end)
+ end, PORT)
+end)
+
+--should wait for the server to start
+os.execute("sleep 0.1")
+
+llby.net.request(string.format("localhost:%i/c", PORT))
+local s = llby.net.request(string.format("localhost:%i/test", PORT))
+local num = s.content:read()
+
+bserver:get():close()
+
+net:await()
+return tonumber(num) == 555221
diff --git a/tests/units/net-parameter.lua b/tests/units/net-parameter.lua
new file mode 100644
index 0000000..ce42759
--- /dev/null
+++ b/tests/units/net-parameter.lua
@@ -0,0 +1,29 @@
+local bserver = llby.thread.buffer(nil)
+
+local a = 255
+local b = 992
+
+local net = llby.thread.async(function(tres)
+ local c = 943
+ llby.net.listen(function(server)
+ bserver:set(server)
+ server:GET("/{a}/{b}/{c}", function(res, req)
+ res:send(tostring(tonumber(req.parameters.a) * a + tonumber(req.parameters.b) * b + tonumber(req.parameters.c) * c))
+ end)
+ end, PORT)
+end)
+
+local t1 = 293
+local t2 = 928
+local t3 = 777
+
+--should wait for the server to start
+os.execute("sleep 0.1")
+
+local s = llby.net.request(string.format("localhost:%i/%i/%i/%i", PORT, t1, t2, t3))
+local num = s.content:read()
+
+bserver:get():close()
+
+net:await()
+return tonumber(num) == (t1 * a + t2 * b + t3 * 943)
diff --git a/tests/units/net-query.lua b/tests/units/net-query.lua
new file mode 100644
index 0000000..2e07262
--- /dev/null
+++ b/tests/units/net-query.lua
@@ -0,0 +1,30 @@
+local bserver = llby.thread.buffer(nil)
+
+local a = 255
+local b = 992
+
+local net = llby.thread.async(function(tres)
+ local c = 943
+ llby.net.listen(function(server)
+ bserver:set(server)
+
+ server:GET("/", function(res, req)
+ res:send(tostring(tonumber(req.query.t1) * a + tonumber(req.query.t2) * b + tonumber(req.query.t3) * c))
+ end)
+ end, PORT)
+end)
+
+local t1 = 293
+local t2 = 928
+local t3 = 777
+
+--should wait for the server to start
+os.execute("sleep 0.1")
+
+local s = llby.net.request(string.format("localhost:%i/?t1=%i&t2=%i&t3=%i", PORT, t1, t2, t3))
+local num = s.content:read()
+
+bserver:get():close()
+
+net:await()
+return tonumber(num) == (t1 * a + t2 * b + t3 * 943)
diff --git a/tests/units/thread-buffer.lua b/tests/units/thread-buffer.lua
new file mode 100644
index 0000000..e83ad4a
--- /dev/null
+++ b/tests/units/thread-buffer.lua
@@ -0,0 +1,21 @@
+local h1 = llby.thread.buffer(llby.crypto.md5())
+local h2 = llby.thread.buffer(llby.crypto.sha256())
+local h3 = llby.thread.buffer(llby.crypto.sha1())
+
+local tthread = llby.thread.async(function(res)
+ h1:set(h1:get():update("mrrp"))
+ h2:set(h2:get():update("mrrp"))
+
+ h3:mod(function(M)
+ return M:update("mrrp")
+ end)
+
+ res(h1:get(), h2:get(), h3:get())
+end)
+
+local h4, h5, h6 = tthread:await()
+
+return h1:get():final() == h4:final() and h2:get():final() == h5:final() and h3:get():final() == h6:final()
+ and h4:final() == "be40416e1491ae73fee43a0cf01132fa"
+ and h5:final() == "a4ba2864e6dcc988c6df73cdfbee6d308e39174dd86dddc4e328c4f2df1c48e9"
+ and h6:final() == "7bf0ffbd68005c35faa12f5ba6df54288969220c"
diff --git a/tests/units/thread-local-return.lua b/tests/units/thread-local-return.lua
new file mode 100644
index 0000000..cc6c417
--- /dev/null
+++ b/tests/units/thread-local-return.lua
@@ -0,0 +1,13 @@
+local a = 224
+
+local thread = llby.thread.async(function(res)
+ local b = 948
+
+ res(function(c)
+ return a * b * c
+ end)
+end)
+
+local fun = thread:await()
+
+return fun(448) == 224 * 948 * 448
diff --git a/tests/units/thread-locals.lua b/tests/units/thread-locals.lua
new file mode 100644
index 0000000..6f328f2
--- /dev/null
+++ b/tests/units/thread-locals.lua
@@ -0,0 +1,12 @@
+local lvar = 224400
+_G.gvar = 33220
+
+function lfun(a, b)
+ return (a + lvar) * (b + gvar)
+end
+
+local thread = llby.thread.async(function(res)
+ res(lfun(lvar, gvar))
+end)
+
+return thread:await() == lfun(lvar, gvar)
diff --git a/tests/units/thread-nested.lua b/tests/units/thread-nested.lua
new file mode 100644
index 0000000..e0227c6
--- /dev/null
+++ b/tests/units/thread-nested.lua
@@ -0,0 +1,22 @@
+local mutex = llby.thread.mutex()
+mutex:lock()
+
+local t1 = llby.thread.async(function(res)
+
+ local t2 = llby.thread.async(function(res)
+
+ local t3 = llby.thread.async(function(res)
+ mutex:lock()
+
+ res(254)
+ end)
+
+
+ res(t3)
+ end)
+
+ res(t2)
+end)
+
+mutex:unlock()
+return t1:await():await():await() == 254