Software /
code /
prosody
Comparison
spec/util_async_spec.lua @ 8623:ab242c513bf4
util.async: tests: Add helper function to create mock watcher callbacks
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 17 Mar 2018 18:12:31 +0000 |
parent | 8622:92fee8a6c988 |
child | 8624:5325f0e1791b |
comparison
equal
deleted
inserted
replaced
8622:92fee8a6c988 | 8623:ab242c513bf4 |
---|---|
6 if debug then | 6 if debug then |
7 require "util.logger".add_simple_sink(print); | 7 require "util.logger".add_simple_sink(print); |
8 else | 8 else |
9 print = function () end | 9 print = function () end |
10 end | 10 end |
11 | |
12 local function mock_watchers() | |
13 return setmetatable(mock{ | |
14 ready = function () end; | |
15 waiting = function () end; | |
16 error = function () end; | |
17 }, { | |
18 __index = function (_, event) | |
19 -- Unexpected watcher called | |
20 assert(false); | |
21 end; | |
22 }) | |
23 end | |
24 | |
11 local function new(func, name) | 25 local function new(func, name) |
12 local log = {}; | 26 local log = {}; |
13 return async.runner(func, setmetatable({}, { | 27 return async.runner(func, mock_watchers()), log; |
14 __index = function (_, event) | |
15 return function (runner, err) | |
16 print(name or runner.id, "event", event, err) | |
17 print "--" | |
18 table.insert(log, { event = event, err = err }); | |
19 end; | |
20 end; | |
21 })), log; | |
22 end | 28 end |
23 describe("#runner", function() | 29 describe("#runner", function() |
24 it("should work", function() | 30 it("should work", function() |
25 local r, l = new(function (item) assert(type(item) == "number") end); | 31 local r, l = new(function (item) assert(type(item) == "number") end); |
26 r:run(1); | 32 r:run(1); |