Software /
code /
prosody
Comparison
spec/util_promise_spec.lua @ 12802:4a8740e01813
Merge 0.12->trunk
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 12 Dec 2022 07:10:54 +0100 |
parent | 12752:9ef8f248635c |
comparison
equal
deleted
inserted
replaced
12801:ebd6b4d8bf04 | 12802:4a8740e01813 |
---|---|
5 describe("new()", function () | 5 describe("new()", function () |
6 it("returns a promise object", function () | 6 it("returns a promise object", function () |
7 assert(promise.new()); | 7 assert(promise.new()); |
8 end); | 8 end); |
9 end); | 9 end); |
10 it("supplies a sensible tostring()", function () | |
11 local s = tostring(promise.new()); | |
12 assert.truthy(s:find("promise", 1, true)); | |
13 assert.truthy(s:find("pending", 1, true)); | |
14 end); | |
10 it("notifies immediately for fulfilled promises", function () | 15 it("notifies immediately for fulfilled promises", function () |
11 local p = promise.new(function (resolve) | 16 local p = promise.new(function (resolve) |
12 resolve("foo"); | 17 resolve("foo"); |
13 end); | 18 end); |
14 local cb = spy.new(function (v) | 19 local cb = spy.new(function (v) |
26 assert.equal("foo", v); | 31 assert.equal("foo", v); |
27 end); | 32 end); |
28 p:next(cb); | 33 p:next(cb); |
29 assert.spy(cb).was_called(0); | 34 assert.spy(cb).was_called(0); |
30 r("foo"); | 35 r("foo"); |
36 assert.spy(cb).was_called(1); | |
37 end); | |
38 it("ignores resolve/reject of settled promises", function () | |
39 local res, rej; | |
40 local p = promise.new(function (resolve, reject) | |
41 res, rej = resolve, reject; | |
42 end); | |
43 local cb = spy.new(function (v) | |
44 assert.equal("foo", v); | |
45 end); | |
46 p:next(cb, cb); | |
47 assert.spy(cb).was_called(0); | |
48 res("foo"); | |
49 assert.spy(cb).was_called(1); | |
50 rej("bar"); | |
51 assert.spy(cb).was_called(1); | |
52 rej(promise.resolve("bar")); | |
53 assert.spy(cb).was_called(1); | |
54 res(promise.reject("bar")); | |
55 assert.spy(cb).was_called(1); | |
56 res(promise.resolve("bar")); | |
31 assert.spy(cb).was_called(1); | 57 assert.spy(cb).was_called(1); |
32 end); | 58 end); |
33 it("allows chaining :next() calls", function () | 59 it("allows chaining :next() calls", function () |
34 local r; | 60 local r; |
35 local result; | 61 local result; |
433 assert.spy(cb).was_called(0); | 459 assert.spy(cb).was_called(0); |
434 r1("this succeeds"); | 460 r1("this succeeds"); |
435 assert.spy(cb).was_called(1); | 461 assert.spy(cb).was_called(1); |
436 assert.same({ | 462 assert.same({ |
437 { status = "fulfilled", value = "this succeeds" }; | 463 { status = "fulfilled", value = "this succeeds" }; |
464 { status = "rejected", reason = "this fails" }; | |
465 }, result); | |
466 end); | |
467 it("works when all promises reject", function () | |
468 local r1, r2; | |
469 local p1, p2 = promise.new(function (_, reject) r1 = reject end), promise.new(function (_, reject) r2 = reject end); | |
470 local p = promise.all_settled({ p1, p2 }); | |
471 | |
472 local result; | |
473 local cb = spy.new(function (v) | |
474 result = v; | |
475 end); | |
476 p:next(cb); | |
477 assert.spy(cb).was_called(0); | |
478 r2("this fails"); | |
479 assert.spy(cb).was_called(0); | |
480 r1("this fails too"); | |
481 assert.spy(cb).was_called(1); | |
482 assert.same({ | |
483 { status = "rejected", reason = "this fails too" }; | |
438 { status = "rejected", reason = "this fails" }; | 484 { status = "rejected", reason = "this fails" }; |
439 }, result); | 485 }, result); |
440 end); | 486 end); |
441 it("works with non-numeric keys", function () | 487 it("works with non-numeric keys", function () |
442 local r1, r2; | 488 local r1, r2; |