diff options
author | Matthew Wild <mwild1@gmail.com> | 2018-03-23 14:02:33 +0000 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2018-03-23 14:02:33 +0000 |
commit | 8a38579fa00e08ea10ff0eee2e4cdb7cd6bb1a3a (patch) | |
tree | 9a66ab2c432a6f383fc2add6965d4df6d6e38c03 | |
parent | faea440e51848cff1f52f469199d7cf8cbc92051 (diff) | |
download | prosody-8a38579fa00e08ea10ff0eee2e4cdb7cd6bb1a3a.tar.gz prosody-8a38579fa00e08ea10ff0eee2e4cdb7cd6bb1a3a.zip |
util.async: Make parameters to async.runner() optional
-rw-r--r-- | spec/util_async_spec.lua | 20 | ||||
-rw-r--r-- | util/async.lua | 10 |
2 files changed, 27 insertions, 3 deletions
diff --git a/spec/util_async_spec.lua b/spec/util_async_spec.lua index 959ad65b..d81b18b5 100644 --- a/spec/util_async_spec.lua +++ b/spec/util_async_spec.lua @@ -94,6 +94,26 @@ describe("util.async", function() assert.equal(last_item, values[#values]); end); + it("should work with no parameters", function () + local item = "fail"; + local r = async.runner(); + local f = spy.new(function () item = "success"; end); + r:run(f); + assert.spy(f).was.called(); + assert.equal(item, "success"); + end); + + it("supports a default error handler", function () + local item = "fail"; + local r = async.runner(); + local f = spy.new(function () error("test error"); end); + assert.error_matches(function () + r:run(f); + end, "test error"); + assert.spy(f).was.called(); + assert.equal(item, "fail"); + end); + describe("#errors", function () describe("should notify", function () local last_processed_item, last_error; diff --git a/util/async.lua b/util/async.lua index f042b177..9456874d 100644 --- a/util/async.lua +++ b/util/async.lua @@ -130,10 +130,14 @@ local function runner_create_thread(func, self) return thread; end -local empty_watchers = {}; +local function default_error_watcher(runner, err) + runner:log("error", "Encountered error: %s", err); + error(err); +end +local function default_func(f) f(); end local function runner(func, watchers, data) - return setmetatable({ func = func, thread = false, state = "ready", notified_state = "ready", - queue = {}, watchers = watchers or empty_watchers, data = data, id = new_id() } + return setmetatable({ func = func or default_func, thread = false, state = "ready", notified_state = "ready", + queue = {}, watchers = watchers or { error = default_error_watcher }, data = data, id = new_id() } , runner_mt); end |