aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2021-11-29 14:11:24 +0000
committerMatthew Wild <mwild1@gmail.com>2021-11-29 14:11:24 +0000
commitcfef2c6ef6609d3d917edb144123295f64187545 (patch)
tree704f2854d4ec07e338f2feff58968bf20996e025 /spec
parentebd4ea2bb3743aa6ee8442fec4bac465e0127c87 (diff)
downloadprosody-cfef2c6ef6609d3d917edb144123295f64187545.tar.gz
prosody-cfef2c6ef6609d3d917edb144123295f64187545.zip
util.async: Add sleep() method with configurable scheduling backend
No scheduler set by default, so it will error (we plan to initialize it in util.startup). We wanted to avoid a hard dependency on util.timer (which in turn depends on network backends, etc.), and we didn't add timer.sleep() because we didn't want to add a hard dependency on util.async for things that don't need it.
Diffstat (limited to 'spec')
-rw-r--r--spec/util_async_spec.lua54
1 files changed, 54 insertions, 0 deletions
diff --git a/spec/util_async_spec.lua b/spec/util_async_spec.lua
index 8123503b..435a844c 100644
--- a/spec/util_async_spec.lua
+++ b/spec/util_async_spec.lua
@@ -615,4 +615,58 @@ 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);
end);