Software /
code /
prosody
Comparison
spec/util_async_spec.lua @ 8630:deade38ffbbd
util.async: tests: slight modifications to allow more code reuse in tests
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 19 Mar 2018 16:24:42 +0000 |
parent | 8628:e88744fa0985 |
child | 8631:1daabc077393 |
comparison
equal
deleted
inserted
replaced
8629:ddb04cacb8b1 | 8630:deade38ffbbd |
---|---|
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 | 11 |
12 local function mock_watchers() | 12 local function mock_watchers(event_log) |
13 local function generic_logging_watcher(name) | |
14 return function (...) | |
15 table.insert(event_log, { name = name, n = select("#", ...)-1, select(2, ...) }); | |
16 end; | |
17 end; | |
13 return setmetatable(mock{ | 18 return setmetatable(mock{ |
14 ready = function () end; | 19 ready = generic_logging_watcher("ready"); |
15 waiting = function () end; | 20 waiting = generic_logging_watcher("waiting"); |
16 error = function () end; | 21 error = generic_logging_watcher("error"); |
17 }, { | 22 }, { |
18 __index = function (_, event) | 23 __index = function (_, event) |
19 -- Unexpected watcher called | 24 -- Unexpected watcher called |
20 assert(false); | 25 assert(false); |
21 end; | 26 end; |
22 }) | 27 }) |
23 end | 28 end |
24 | 29 |
25 local function new(func, name) | 30 local function new(func, name) |
26 local log = {}; | 31 local event_log = {}; |
27 return async.runner(func, mock_watchers()), log; | 32 local spy_func = spy.new(func); |
33 return async.runner(spy_func, mock_watchers(event_log)), event_log, spy_func; | |
28 end | 34 end |
29 describe("#runner", function() | 35 describe("#runner", function() |
30 it("should work", function() | 36 it("should work", function() |
31 local r, l = new(function (item) assert(type(item) == "number") end); | 37 local r, l = new(function (item) assert(type(item) == "number") end); |
32 r:run(1); | 38 r:run(1); |
535 end); | 541 end); |
536 | 542 |
537 it("should support multiple done() calls", function () | 543 it("should support multiple done() calls", function () |
538 local processed_item; | 544 local processed_item; |
539 local wait, done; | 545 local wait, done; |
540 local rf = spy.new(function (item) | 546 local r, _, rf = new(function (item) |
541 wait, done = async.waiter(4); | 547 wait, done = async.waiter(4); |
542 wait(); | 548 wait(); |
543 processed_item = item; | 549 processed_item = item; |
544 end); | 550 end); |
545 local r = async.runner(rf, mock_watchers()); | |
546 r:run("test"); | 551 r:run("test"); |
547 for i = 1, 3 do | 552 for i = 1, 3 do |
548 done(); | 553 done(); |
549 assert.equal(r.state, "waiting"); | 554 assert.equal(r.state, "waiting"); |
550 assert.is_nil(processed_item); | 555 assert.is_nil(processed_item); |
556 end); | 561 end); |
557 | 562 |
558 it("should not allow done() to be called more than specified", function () | 563 it("should not allow done() to be called more than specified", function () |
559 local processed_item; | 564 local processed_item; |
560 local wait, done; | 565 local wait, done; |
561 local rf = spy.new(function (item) | 566 local r, _, rf = new(function (item) |
562 wait, done = async.waiter(4); | 567 wait, done = async.waiter(4); |
563 wait(); | 568 wait(); |
564 processed_item = item; | 569 processed_item = item; |
565 end); | 570 end); |
566 local r = async.runner(rf, mock_watchers()); | |
567 r:run("test"); | 571 r:run("test"); |
568 for i = 1, 4 do | 572 for i = 1, 4 do |
569 done(); | 573 done(); |
570 end | 574 end |
571 assert.has_error(done);; | 575 assert.has_error(done);; |
574 assert.spy(r.watchers.error).was_not.called(); | 578 assert.spy(r.watchers.error).was_not.called(); |
575 end); | 579 end); |
576 | 580 |
577 it("should allow done() to be called before wait()", function () | 581 it("should allow done() to be called before wait()", function () |
578 local processed_item; | 582 local processed_item; |
579 local rf = spy.new(function (item) | 583 local r, _, rf = new(function (item) |
580 local wait, done = async.waiter(); | 584 local wait, done = async.waiter(); |
581 done(); | 585 done(); |
582 wait(); | 586 wait(); |
583 processed_item = item; | 587 processed_item = item; |
584 end); | 588 end); |
585 local r = async.runner(rf, mock_watchers()); | |
586 r:run("test"); | 589 r:run("test"); |
587 assert.equal(processed_item, "test"); | 590 assert.equal(processed_item, "test"); |
588 assert.equal(r.state, "ready"); | 591 assert.equal(r.state, "ready"); |
589 -- Since the observable state did not change, | 592 -- Since the observable state did not change, |
590 -- the watchers should not have been called | 593 -- the watchers should not have been called |