aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2022-10-07 17:43:26 +0100
committerMatthew Wild <mwild1@gmail.com>2022-10-07 17:43:26 +0100
commitaed0c1c5ababe8b44ac8f88ef423b641c0c6ec4f (patch)
treedcdc35f85e95dce57fbcb32ec6c7cfbaa32b876c
parentd08ddc1f4a1c6d93342bd8efd69c837782656f1f (diff)
downloadprosody-aed0c1c5ababe8b44ac8f88ef423b641c0c6ec4f.tar.gz
prosody-aed0c1c5ababe8b44ac8f88ef423b641c0c6ec4f.zip
util.promise: Remove some redundant checks, add tests confirming redundancy
This lines don't appear to do anything useful, and all tests pass when they are removed. Discovered via mutation testing. I added extra tests to exercise this code, because I wasn't certain that there were no side-effects caused by removal. Everything appears to be fine, thanks to the "pending" check at the start of promise_settle().
-rw-r--r--spec/util_promise_spec.lua21
-rw-r--r--util/promise.lua4
2 files changed, 21 insertions, 4 deletions
diff --git a/spec/util_promise_spec.lua b/spec/util_promise_spec.lua
index 597b56f8..75e9294e 100644
--- a/spec/util_promise_spec.lua
+++ b/spec/util_promise_spec.lua
@@ -30,6 +30,27 @@ describe("util.promise", function ()
r("foo");
assert.spy(cb).was_called(1);
end);
+ it("ignores resolve/reject of settled promises", function ()
+ local res, rej;
+ local p = promise.new(function (resolve, reject)
+ res, rej = resolve, reject;
+ end);
+ local cb = spy.new(function (v)
+ assert.equal("foo", v);
+ end);
+ p:next(cb, cb);
+ assert.spy(cb).was_called(0);
+ res("foo");
+ assert.spy(cb).was_called(1);
+ rej("bar");
+ assert.spy(cb).was_called(1);
+ rej(promise.resolve("bar"));
+ assert.spy(cb).was_called(1);
+ res(promise.reject("bar"));
+ assert.spy(cb).was_called(1);
+ res(promise.resolve("bar"));
+ assert.spy(cb).was_called(1);
+ end);
it("allows chaining :next() calls", function ()
local r;
local result;
diff --git a/util/promise.lua b/util/promise.lua
index 1762d501..d8f0b3d6 100644
--- a/util/promise.lua
+++ b/util/promise.lua
@@ -57,9 +57,7 @@ local function promise_settle(promise, new_state, new_next, cbs, value)
end
local function new_resolve_functions(p)
- local resolved = false;
local function _resolve(v)
- if resolved then return; end
resolved = true;
if is_promise(v) then
v:next(new_resolve_functions(p));
@@ -69,8 +67,6 @@ local function new_resolve_functions(p)
end
local function _reject(e)
- if resolved then return; end
- resolved = true;
if promise_settle(p, "rejected", next_rejected, p._pending_on_rejected, e) then
p.reason = e;
end