aboutsummaryrefslogtreecommitdiffstats
path: root/spec/util_async_spec.lua
diff options
context:
space:
mode:
Diffstat (limited to 'spec/util_async_spec.lua')
-rw-r--r--spec/util_async_spec.lua103
1 files changed, 103 insertions, 0 deletions
diff --git a/spec/util_async_spec.lua b/spec/util_async_spec.lua
index d2de8c94..dbb49ff7 100644
--- a/spec/util_async_spec.lua
+++ b/spec/util_async_spec.lua
@@ -1,4 +1,5 @@
local async = require "util.async";
+local match = require "luassert.match";
describe("util.async", function()
local debug = false;
@@ -544,6 +545,8 @@ describe("util.async", function()
assert.equal(r1.state, "ready");
end);
+ -- luacheck: ignore 211/rf
+ -- FIXME what's rf?
it("should support multiple done() calls", function ()
local processed_item;
local wait, done;
@@ -613,4 +616,104 @@ describe("util.async", function()
assert.spy(r.watchers.error).was_not.called();
end);
end);
+
+ describe("#sleep()", function ()
+ after_each(function ()
+ -- Restore to default
+ async.set_schedule_function(nil);
+ end);
+
+ it("should fail if no scheduler configured", function ()
+ local r = new(function ()
+ async.sleep(5);
+ end);
+ r:run(true);
+ assert.spy(r.watchers.error).was.called();
+
+ -- Set dummy scheduler
+ async.set_schedule_function(function () end);
+
+ local r2 = new(function ()
+ async.sleep(5);
+ end);
+ r2:run(true);
+ assert.spy(r2.watchers.error).was_not.called();
+ end);
+ it("should work", function ()
+ local queue = {};
+ local add_task = spy.new(function (t, f)
+ table.insert(queue, { t, f });
+ end);
+ async.set_schedule_function(add_task);
+
+ local processed_item;
+ local r = new(function (item)
+ async.sleep(5);
+ processed_item = item;
+ end);
+ r:run("test");
+
+ -- Nothing happened, because the runner is sleeping
+ assert.is_nil(processed_item);
+ assert.equal(r.state, "waiting");
+ assert.spy(add_task).was_called(1);
+ assert.spy(add_task).was_called_with(match.is_number(), match.is_function());
+ assert.spy(r.watchers.waiting).was.called();
+ assert.spy(r.watchers.ready).was_not.called();
+
+ -- Pretend the timer has triggered, call the handler
+ queue[1][2]();
+
+ assert.equal(processed_item, "test");
+ assert.equal(r.state, "ready");
+
+ assert.spy(r.watchers.ready).was.called();
+ end);
+ end);
+
+ describe("#set_nexttick()", function ()
+ after_each(function ()
+ -- Restore to default
+ async.set_nexttick(nil);
+ end);
+ it("should work", function ()
+ local queue = {};
+ local nexttick = spy.new(function (f)
+ assert.is_function(f);
+ table.insert(queue, f);
+ end);
+ async.set_nexttick(nexttick);
+
+ local processed_item;
+ local wait, done;
+ local r = new(function (item)
+ wait, done = async.waiter();
+ wait();
+ processed_item = item;
+ end);
+ r:run("test");
+
+ -- Nothing happened, because the runner is waiting
+ assert.is_nil(processed_item);
+ assert.equal(r.state, "waiting");
+ assert.spy(nexttick).was_called(0);
+ assert.spy(r.watchers.waiting).was.called();
+ assert.spy(r.watchers.ready).was_not.called();
+
+ -- Mark the runner as ready, it should be scheduled for
+ -- the next tick
+ done();
+
+ assert.spy(nexttick).was_called(1);
+ assert.spy(nexttick).was_called_with(match.is_function());
+ assert.equal(1, #queue);
+
+ -- Pretend it's the next tick - call the pending function
+ queue[1]();
+
+ assert.equal(processed_item, "test");
+ assert.equal(r.state, "ready");
+ assert.spy(r.watchers.ready).was.called();
+ end);
+ end);
end);