From d46c43fbeb5e2fcc1d3cccf348ebb92f60f4bc2d Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 29 Nov 2021 14:14:30 +0000 Subject: util.async: Add next-tick configuration Running woken runners in the next iteration of the event loop prevents unexpected recursion, unexpected tracebacks, and is generally more predictable. The pattern is borrowed from util.promise, where we're now doing the same. --- spec/util_async_spec.lua | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'spec') diff --git a/spec/util_async_spec.lua b/spec/util_async_spec.lua index 435a844c..77afd1fc 100644 --- a/spec/util_async_spec.lua +++ b/spec/util_async_spec.lua @@ -669,4 +669,50 @@ describe("util.async", function() 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); -- cgit v1.2.3