Mercurial > prosody-hg
comparison spec/util_async_spec.lua @ 11961:542a9a503073
util.async: Add sleep() method with configurable scheduling backend
No scheduler set by default, so it will error (we plan to initialize it in
util.startup).
We wanted to avoid a hard dependency on util.timer (which in turn depends on
network backends, etc.), and we didn't add timer.sleep() because we didn't
want to add a hard dependency on util.async for things that don't need it.
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Mon, 29 Nov 2021 14:11:24 +0000 |
| parents | 6c6ff4509082 |
| children | 9a70a543c727 |
comparison
equal
deleted
inserted
replaced
| 11960:12a3c05aa12d | 11961:542a9a503073 |
|---|---|
| 613 r:run(true); | 613 r:run(true); |
| 614 assert.spy(r.func).was.called(); | 614 assert.spy(r.func).was.called(); |
| 615 assert.spy(r.watchers.error).was_not.called(); | 615 assert.spy(r.watchers.error).was_not.called(); |
| 616 end); | 616 end); |
| 617 end); | 617 end); |
| 618 | |
| 619 describe("#sleep()", function () | |
| 620 after_each(function () | |
| 621 -- Restore to default | |
| 622 async.set_schedule_function(nil); | |
| 623 end); | |
| 624 | |
| 625 it("should fail if no scheduler configured", function () | |
| 626 local r = new(function () | |
| 627 async.sleep(5); | |
| 628 end); | |
| 629 r:run(true); | |
| 630 assert.spy(r.watchers.error).was.called(); | |
| 631 | |
| 632 -- Set dummy scheduler | |
| 633 async.set_schedule_function(function () end); | |
| 634 | |
| 635 local r2 = new(function () | |
| 636 async.sleep(5); | |
| 637 end); | |
| 638 r2:run(true); | |
| 639 assert.spy(r2.watchers.error).was_not.called(); | |
| 640 end); | |
| 641 it("should work", function () | |
| 642 local queue = {}; | |
| 643 local add_task = spy.new(function (t, f) | |
| 644 table.insert(queue, { t, f }); | |
| 645 end); | |
| 646 async.set_schedule_function(add_task); | |
| 647 | |
| 648 local processed_item; | |
| 649 local r = new(function (item) | |
| 650 async.sleep(5); | |
| 651 processed_item = item; | |
| 652 end); | |
| 653 r:run("test"); | |
| 654 | |
| 655 -- Nothing happened, because the runner is sleeping | |
| 656 assert.is_nil(processed_item); | |
| 657 assert.equal(r.state, "waiting"); | |
| 658 assert.spy(add_task).was_called(1); | |
| 659 assert.spy(add_task).was_called_with(match.is_number(), match.is_function()); | |
| 660 assert.spy(r.watchers.waiting).was.called(); | |
| 661 assert.spy(r.watchers.ready).was_not.called(); | |
| 662 | |
| 663 -- Pretend the timer has triggered, call the handler | |
| 664 queue[1][2](); | |
| 665 | |
| 666 assert.equal(processed_item, "test"); | |
| 667 assert.equal(r.state, "ready"); | |
| 668 | |
| 669 assert.spy(r.watchers.ready).was.called(); | |
| 670 end); | |
| 671 end); | |
| 618 end); | 672 end); |
