Annotate

spec/util_pubsub_spec.lua @ 9028:d4c2a3060e7e

util.pposix: Use freopen(3) when replacing standard IO streams (thanks ratchetfreak) Man page says this is what freopen(3) is for.
author Kim Alvefur <zash@zash.se>
date Thu, 12 Jul 2018 20:42:22 +0200
parent 9004:50a0f405e6c9
child 9159:a0fd7064f4ac
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8564
fd41dc4a78e9 util_pubsub_spec: Move util.pubsub import into a setup block
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
1 local pubsub;
fd41dc4a78e9 util_pubsub_spec: Move util.pubsub import into a setup block
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
2 setup(function ()
fd41dc4a78e9 util_pubsub_spec: Move util.pubsub import into a setup block
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
3 pubsub = require "util.pubsub";
fd41dc4a78e9 util_pubsub_spec: Move util.pubsub import into a setup block
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
4 end);
fd41dc4a78e9 util_pubsub_spec: Move util.pubsub import into a setup block
Kim Alvefur <zash@zash.se>
parents: 8558
diff changeset
5
8558
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 describe("util.pubsub", function ()
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 describe("simple node creation and deletion", function ()
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 -- Roughly a port of scansion/scripts/pubsub_createdelete.scs
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local service = pubsub.new();
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 describe("#create", function ()
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 it("creates a new node", function ()
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 assert.truthy(service:create("princely_musings", true));
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 end);
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 it("fails to create the same node again", function ()
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 assert.falsy(service:create("princely_musings", true));
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 end);
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 end);
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 describe("#delete", function ()
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 it("deletes the node", function ()
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 assert.truthy(service:delete("princely_musings", true));
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 end);
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 it("can't delete an already deleted node", function ()
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 assert.falsy(service:delete("princely_musings", true));
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 end);
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 end);
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 end);
8647
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8564
diff changeset
31
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8564
diff changeset
32 describe("simple publishing", function ()
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8564
diff changeset
33 local broadcaster = spy.new(function () end);
8817
9a3066a580ad spec/util_pubsub: Test whether someone can subscribe to a node
Kim Alvefur <zash@zash.se>
parents: 8647
diff changeset
34 local service = pubsub.new({
9a3066a580ad spec/util_pubsub: Test whether someone can subscribe to a node
Kim Alvefur <zash@zash.se>
parents: 8647
diff changeset
35 broadcaster = broadcaster;
9a3066a580ad spec/util_pubsub: Test whether someone can subscribe to a node
Kim Alvefur <zash@zash.se>
parents: 8647
diff changeset
36 capabilities = {
9a3066a580ad spec/util_pubsub: Test whether someone can subscribe to a node
Kim Alvefur <zash@zash.se>
parents: 8647
diff changeset
37 none = {
9a3066a580ad spec/util_pubsub: Test whether someone can subscribe to a node
Kim Alvefur <zash@zash.se>
parents: 8647
diff changeset
38 subscribe = true;
9a3066a580ad spec/util_pubsub: Test whether someone can subscribe to a node
Kim Alvefur <zash@zash.se>
parents: 8647
diff changeset
39 be_subscribed = true;
9a3066a580ad spec/util_pubsub: Test whether someone can subscribe to a node
Kim Alvefur <zash@zash.se>
parents: 8647
diff changeset
40 };
9a3066a580ad spec/util_pubsub: Test whether someone can subscribe to a node
Kim Alvefur <zash@zash.se>
parents: 8647
diff changeset
41 }
9a3066a580ad spec/util_pubsub: Test whether someone can subscribe to a node
Kim Alvefur <zash@zash.se>
parents: 8647
diff changeset
42 });
8647
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8564
diff changeset
43
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8564
diff changeset
44 it("creates a node", function ()
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8564
diff changeset
45 assert.truthy(service:create("node", true));
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8564
diff changeset
46 end);
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8564
diff changeset
47
8817
9a3066a580ad spec/util_pubsub: Test whether someone can subscribe to a node
Kim Alvefur <zash@zash.se>
parents: 8647
diff changeset
48 it("lets someone subscribe", function ()
9a3066a580ad spec/util_pubsub: Test whether someone can subscribe to a node
Kim Alvefur <zash@zash.se>
parents: 8647
diff changeset
49 assert.truthy(service:add_subscription("node", true, "someone"));
9a3066a580ad spec/util_pubsub: Test whether someone can subscribe to a node
Kim Alvefur <zash@zash.se>
parents: 8647
diff changeset
50 end);
9a3066a580ad spec/util_pubsub: Test whether someone can subscribe to a node
Kim Alvefur <zash@zash.se>
parents: 8647
diff changeset
51
8647
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8564
diff changeset
52 it("publishes an item", function ()
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8564
diff changeset
53 assert.truthy(service:publish("node", true, "1", "item 1"));
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8564
diff changeset
54 end);
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8564
diff changeset
55
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8564
diff changeset
56 it("called the broadcaster", function ()
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8564
diff changeset
57 assert.spy(broadcaster).was_called();
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8564
diff changeset
58 end);
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8564
diff changeset
59
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8564
diff changeset
60 it("should return one item", function ()
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8564
diff changeset
61 local ok, ret = service:get_items("node", true);
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8564
diff changeset
62 assert.truthy(ok);
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8564
diff changeset
63 assert.same({ "1", ["1"] = "item 1" }, ret);
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8564
diff changeset
64 end);
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8564
diff changeset
65
638ff2ad98e6 util.pubsub: Add simple test covering publishing and retrieving an item
Kim Alvefur <zash@zash.se>
parents: 8564
diff changeset
66 end);
9004
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
67
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
68 describe("#issue1082", function ()
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
69 local service = pubsub.new();
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
70
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
71 it("creates a node with max_items = 1", function ()
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
72 assert.truthy(service:create("node", true, { max_items = 1 }));
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
73 end);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
74
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
75 it("changes max_items to 2", function ()
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
76 assert.truthy(service:set_node_config("node", true, { max_items = 2 }));
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
77 end);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
78
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
79 it("publishes one item", function ()
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
80 assert.truthy(service:publish("node", true, "1", "item 1"));
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
81 end);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
82
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
83 it("should return one item", function ()
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
84 local ok, ret = service:get_items("node", true);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
85 assert.truthy(ok);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
86 assert.same({ "1", ["1"] = "item 1" }, ret);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
87 end);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
88
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
89 it("publishes another item", function ()
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
90 assert.truthy(service:publish("node", true, "2", "item 2"));
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
91 end);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
92
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
93 it("should return two items", function ()
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
94 local ok, ret = service:get_items("node", true);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
95 assert.truthy(ok);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
96 assert.same({
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
97 "2",
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
98 "1",
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
99 ["1"] = "item 1",
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
100 ["2"] = "item 2",
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
101 }, ret);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
102 end);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
103
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
104 it("publishes yet another item", function ()
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
105 assert.truthy(service:publish("node", true, "3", "item 3"));
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
106 end);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
107
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
108 it("should still return only two items", function ()
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
109 local ok, ret = service:get_items("node", true);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
110 assert.truthy(ok);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
111 assert.same({
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
112 "3",
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
113 "2",
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
114 ["2"] = "item 2",
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
115 ["3"] = "item 3",
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
116 }, ret);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
117 end);
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
118
50a0f405e6c9 util_pubsub_spec: Add test for #1082
Kim Alvefur <zash@zash.se>
parents: 8817
diff changeset
119 end);
8558
5de663cef508 util_pubsub_spec: Beginnings of tests for util.pubsub
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 end);