Comparison

spec/util_async_spec.lua @ 8607:62bb06cf8a43

util.async: Add tests to specifically cover error handling
author Matthew Wild <mwild1@gmail.com>
date Fri, 16 Mar 2018 17:50:16 +0000
parent 8605:3a1efc0532cb
child 8608:a2e6caf5848d
comparison
equal deleted inserted replaced
8606:b6b1f0f9b381 8607:62bb06cf8a43
24 it("should work", function() 24 it("should work", function()
25 local r, l = new(function (item) assert(type(item) == "number") end); 25 local r, l = new(function (item) assert(type(item) == "number") end);
26 r:run(1); 26 r:run(1);
27 r:run(2); 27 r:run(2);
28 end); 28 end);
29
30 it("should be ready after creation", function ()
31 local r = async.runner(function (item) end);
32 assert.equal(r.state, "ready");
33 end);
34
35 describe("#errors", function ()
36 local last_processed_item, last_error;
37 local r;
38 r = async.runner(function (item)
39 if item == "error" then
40 error({ e = "test error" });
41 end
42 last_processed_item = item;
43 end, {
44 error = function (runner, err)
45 assert.equal(r, runner);
46 last_error = err;
47 end;
48 });
49
50 randomize(false);
51
52 it("should notify", function ()
53 local last_processed_item, last_error;
54 local r;
55 r = async.runner(function (item)
56 if item == "error" then
57 error({ e = "test error" });
58 end
59 last_processed_item = item;
60 end, {
61 error = function (runner, err)
62 assert.equal(r, runner);
63 last_error = err;
64 end;
65 });
66
67 r:run("hello");
68 assert.equal(r.state, "ready");
69 assert.equal(last_processed_item, "hello");
70
71 assert.equal(last_error, nil);
72 r:run("error");
73 assert.is_table(last_error);
74 assert.equal(last_error.e, "test error");
75 last_error = nil;
76 assert.equal(r.state, "ready");
77 assert.equal(last_processed_item, "hello");
78 end);
79 it("should not be fatal to the runner", function ()
80 r:run("world");
81 assert.equal(r.state, "ready");
82 assert.equal(last_processed_item, "world");
83 end);
84 end);
29 end); 85 end);
30 describe("#waiter", function() 86 describe("#waiter", function()
31 it("should work", function () 87 it("should work", function ()
32 local wait, done; 88 local wait, done;
33 89