aboutsummaryrefslogtreecommitdiffstats
path: root/spec/util_promise_spec.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2018-10-25 14:38:00 +0100
committerMatthew Wild <mwild1@gmail.com>2018-10-25 14:38:00 +0100
commitb74a643e349651c8161512c3180c32da7c6665c0 (patch)
tree1fa523ee426ab08664929087982e08ddc961d210 /spec/util_promise_spec.lua
parentc7d9ee0bb860dcb41cccafd02cfcd654ccd4c1d5 (diff)
downloadprosody-b74a643e349651c8161512c3180c32da7c6665c0.tar.gz
prosody-b74a643e349651c8161512c3180c32da7c6665c0.zip
util.promise: Ensure chained promises always receive a value/rejection even if an intermediate promise has no handlers
Diffstat (limited to 'spec/util_promise_spec.lua')
-rw-r--r--spec/util_promise_spec.lua37
1 files changed, 37 insertions, 0 deletions
diff --git a/spec/util_promise_spec.lua b/spec/util_promise_spec.lua
index c2a8ba95..a06232b1 100644
--- a/spec/util_promise_spec.lua
+++ b/spec/util_promise_spec.lua
@@ -138,6 +138,43 @@ describe("util.promise", function ()
assert.equal("ok", result);
end);
+ it("propagates errors down the chain, even when some handlers are not provided", function ()
+ local r, result;
+ local test_error = {};
+ local p = promise.new(function (resolve, reject)
+ r = resolve;
+ end);
+ local cb = spy.new(function () end);
+ local err_cb = spy.new(function (e) result = e end);
+ local p2 = p:next(function () error(test_error) end);
+ local p3 = p2:next(cb)
+ p3:catch(err_cb);
+ assert.spy(cb).was_called(0);
+ assert.spy(err_cb).was_called(0);
+ r("oh doh");
+ assert.spy(cb).was_called(0);
+ assert.spy(err_cb).was_called(1);
+ assert.equal(test_error, result);
+ end);
+
+ it("propagates values down the chain, even when some handlers are not provided", function ()
+ local r;
+ local p = promise.new(function (resolve, reject)
+ r = resolve;
+ end);
+ local cb = spy.new(function () end);
+ local err_cb = spy.new(function () end);
+ local p2 = p:next(function (v) return v; end);
+ local p3 = p2:catch(err_cb)
+ p3:next(cb);
+ assert.spy(cb).was_called(0);
+ assert.spy(err_cb).was_called(0);
+ r(1337);
+ assert.spy(cb).was_called(1);
+ assert.spy(cb).was_called_with(1337);
+ assert.spy(err_cb).was_called(0);
+ end);
+
describe("race()", function ()
it("works with fulfilled promises", function ()
local p1, p2 = promise.resolve("yep"), promise.resolve("nope");