aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2018-03-17 11:47:07 +0000
committerMatthew Wild <mwild1@gmail.com>2018-03-17 11:47:07 +0000
commit7d562e915e665f0157ef903831f58274eef7dd3d (patch)
tree3a379ba0e7212495ed4734d020c80cc6c7f40092
parent18c5a6ca6b9d567a12a68f97b309621a44f2c7a2 (diff)
downloadprosody-7d562e915e665f0157ef903831f58274eef7dd3d.tar.gz
prosody-7d562e915e665f0157ef903831f58274eef7dd3d.zip
util.async: Yet more tests
-rw-r--r--spec/util_async_spec.lua52
1 files changed, 52 insertions, 0 deletions
diff --git a/spec/util_async_spec.lua b/spec/util_async_spec.lua
index bc6e04f8..e4e74e80 100644
--- a/spec/util_async_spec.lua
+++ b/spec/util_async_spec.lua
@@ -32,6 +32,58 @@ describe("util.async", function()
assert.equal(r.state, "ready");
end);
+ it("should do nothing if the queue is empty", function ()
+ local did_run;
+ local r = async.runner(function (item) did_run = true end);
+ r:run();
+ assert.equal(r.state, "ready");
+ assert.is_nil(did_run);
+ r:run("hello");
+ assert.is_true(did_run);
+ end);
+
+ it("should support queuing work items without running", function ()
+ local did_run;
+ local r = async.runner(function (item) did_run = true end);
+ r:enqueue("hello");
+ assert.equal(r.state, "ready");
+ assert.is_nil(did_run);
+ r:run();
+ assert.is_true(did_run);
+ end);
+
+ it("should support queuing multiple work items", function ()
+ local last_item;
+ local s = spy(function (item) last_item = item; end);
+ local r = async.runner(s);
+ r:enqueue("hello");
+ r:enqueue("there");
+ r:enqueue("world");
+ assert.equal(r.state, "ready");
+ r:run();
+ assert.equal(r.state, "ready");
+ assert.spy(s).was.called(3);
+ assert.equal(last_item, "world");
+ end);
+
+ it("should support all simple data types", function ()
+ local last_item;
+ local s = spy(function (item) last_item = item; end);
+ local r = async.runner(s);
+ local values = { {}, 123, "hello", true, false };
+ for i = 1, #values do
+ r:enqueue(values[i]);
+ end
+ assert.equal(r.state, "ready");
+ r:run();
+ assert.equal(r.state, "ready");
+ assert.spy(s).was.called(#values);
+ for i = 1, #values do
+ assert.spy(s).was.called_with(values[i]);
+ end
+ assert.equal(last_item, values[#values]);
+ end);
+
describe("#errors", function ()
local last_processed_item, last_error;
local r;