diff options
author | Matthew Wild <mwild1@gmail.com> | 2018-03-19 16:24:42 +0000 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2018-03-19 16:24:42 +0000 |
commit | 3f856fe19948ac570216a1a07b3f41f89cafadfd (patch) | |
tree | 912f9666097215ec908248037491d821ab365978 /spec | |
parent | 6191ad50b7dc77564373da83abadc54a38780594 (diff) | |
download | prosody-3f856fe19948ac570216a1a07b3f41f89cafadfd.tar.gz prosody-3f856fe19948ac570216a1a07b3f41f89cafadfd.zip |
util.async: tests: slight modifications to allow more code reuse in tests
Diffstat (limited to 'spec')
-rw-r--r-- | spec/util_async_spec.lua | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/spec/util_async_spec.lua b/spec/util_async_spec.lua index 68c137e5..7bad0b6d 100644 --- a/spec/util_async_spec.lua +++ b/spec/util_async_spec.lua @@ -9,11 +9,16 @@ describe("util.async", function() print = function () end end - local function mock_watchers() + local function mock_watchers(event_log) + local function generic_logging_watcher(name) + return function (...) + table.insert(event_log, { name = name, n = select("#", ...)-1, select(2, ...) }); + end; + end; return setmetatable(mock{ - ready = function () end; - waiting = function () end; - error = function () end; + ready = generic_logging_watcher("ready"); + waiting = generic_logging_watcher("waiting"); + error = generic_logging_watcher("error"); }, { __index = function (_, event) -- Unexpected watcher called @@ -23,8 +28,9 @@ describe("util.async", function() end local function new(func, name) - local log = {}; - return async.runner(func, mock_watchers()), log; + local event_log = {}; + local spy_func = spy.new(func); + return async.runner(spy_func, mock_watchers(event_log)), event_log, spy_func; end describe("#runner", function() it("should work", function() @@ -537,12 +543,11 @@ describe("util.async", function() it("should support multiple done() calls", function () local processed_item; local wait, done; - local rf = spy.new(function (item) + local r, _, rf = new(function (item) wait, done = async.waiter(4); wait(); processed_item = item; end); - local r = async.runner(rf, mock_watchers()); r:run("test"); for i = 1, 3 do done(); @@ -558,12 +563,11 @@ describe("util.async", function() it("should not allow done() to be called more than specified", function () local processed_item; local wait, done; - local rf = spy.new(function (item) + local r, _, rf = new(function (item) wait, done = async.waiter(4); wait(); processed_item = item; end); - local r = async.runner(rf, mock_watchers()); r:run("test"); for i = 1, 4 do done(); @@ -576,13 +580,12 @@ describe("util.async", function() it("should allow done() to be called before wait()", function () local processed_item; - local rf = spy.new(function (item) + local r, _, rf = new(function (item) local wait, done = async.waiter(); done(); wait(); processed_item = item; end); - local r = async.runner(rf, mock_watchers()); r:run("test"); assert.equal(processed_item, "test"); assert.equal(r.state, "ready"); |