From 364f1605a88bb07b52af85a6e02c75e1c5e02db2 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 16 Mar 2018 17:50:16 +0000 Subject: util.async: Add tests to specifically cover error handling --- spec/util_async_spec.lua | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'spec') diff --git a/spec/util_async_spec.lua b/spec/util_async_spec.lua index 9681673f..24f2efdb 100644 --- a/spec/util_async_spec.lua +++ b/spec/util_async_spec.lua @@ -26,6 +26,62 @@ describe("util.async", function() r:run(1); r:run(2); end); + + it("should be ready after creation", function () + local r = async.runner(function (item) end); + assert.equal(r.state, "ready"); + end); + + describe("#errors", function () + local last_processed_item, last_error; + local r; + r = async.runner(function (item) + if item == "error" then + error({ e = "test error" }); + end + last_processed_item = item; + end, { + error = function (runner, err) + assert.equal(r, runner); + last_error = err; + end; + }); + + randomize(false); + + it("should notify", function () + local last_processed_item, last_error; + local r; + r = async.runner(function (item) + if item == "error" then + error({ e = "test error" }); + end + last_processed_item = item; + end, { + error = function (runner, err) + assert.equal(r, runner); + last_error = err; + end; + }); + + r:run("hello"); + assert.equal(r.state, "ready"); + assert.equal(last_processed_item, "hello"); + + assert.equal(last_error, nil); + r:run("error"); + assert.is_table(last_error); + assert.equal(last_error.e, "test error"); + last_error = nil; + assert.equal(r.state, "ready"); + assert.equal(last_processed_item, "hello"); + end); + it("should not be fatal to the runner", function () + r:run("world"); + assert.equal(r.state, "ready"); + assert.equal(last_processed_item, "world"); + end); + end); end); describe("#waiter", function() it("should work", function () -- cgit v1.2.3