Software /
code /
prosody
Comparison
spec/util_async_spec.lua @ 8614:bfbafeced0c4
util.async: Yet more tests
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 17 Mar 2018 11:47:07 +0000 |
parent | 8609:9f6ab206d741 |
child | 8615:e77b37de482e |
comparison
equal
deleted
inserted
replaced
8613:dbb4788db8e3 | 8614:bfbafeced0c4 |
---|---|
30 it("should be ready after creation", function () | 30 it("should be ready after creation", function () |
31 local r = async.runner(function (item) end); | 31 local r = async.runner(function (item) end); |
32 assert.equal(r.state, "ready"); | 32 assert.equal(r.state, "ready"); |
33 end); | 33 end); |
34 | 34 |
35 it("should do nothing if the queue is empty", function () | |
36 local did_run; | |
37 local r = async.runner(function (item) did_run = true end); | |
38 r:run(); | |
39 assert.equal(r.state, "ready"); | |
40 assert.is_nil(did_run); | |
41 r:run("hello"); | |
42 assert.is_true(did_run); | |
43 end); | |
44 | |
45 it("should support queuing work items without running", function () | |
46 local did_run; | |
47 local r = async.runner(function (item) did_run = true end); | |
48 r:enqueue("hello"); | |
49 assert.equal(r.state, "ready"); | |
50 assert.is_nil(did_run); | |
51 r:run(); | |
52 assert.is_true(did_run); | |
53 end); | |
54 | |
55 it("should support queuing multiple work items", function () | |
56 local last_item; | |
57 local s = spy(function (item) last_item = item; end); | |
58 local r = async.runner(s); | |
59 r:enqueue("hello"); | |
60 r:enqueue("there"); | |
61 r:enqueue("world"); | |
62 assert.equal(r.state, "ready"); | |
63 r:run(); | |
64 assert.equal(r.state, "ready"); | |
65 assert.spy(s).was.called(3); | |
66 assert.equal(last_item, "world"); | |
67 end); | |
68 | |
69 it("should support all simple data types", function () | |
70 local last_item; | |
71 local s = spy(function (item) last_item = item; end); | |
72 local r = async.runner(s); | |
73 local values = { {}, 123, "hello", true, false }; | |
74 for i = 1, #values do | |
75 r:enqueue(values[i]); | |
76 end | |
77 assert.equal(r.state, "ready"); | |
78 r:run(); | |
79 assert.equal(r.state, "ready"); | |
80 assert.spy(s).was.called(#values); | |
81 for i = 1, #values do | |
82 assert.spy(s).was.called_with(values[i]); | |
83 end | |
84 assert.equal(last_item, values[#values]); | |
85 end); | |
86 | |
35 describe("#errors", function () | 87 describe("#errors", function () |
36 local last_processed_item, last_error; | 88 local last_processed_item, last_error; |
37 local r; | 89 local r; |
38 local wait, done; | 90 local wait, done; |
39 r = async.runner(function (item) | 91 r = async.runner(function (item) |