message_bus 2.0.0.beta.2 → 2.0.0.beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of message_bus might be problematic. Click here for more details.

@@ -0,0 +1,208 @@
1
+ require_relative '../../../spec_helper'
2
+ require 'message_bus'
3
+
4
+ if MESSAGE_BUS_CONFIG[:backend] == :postgres
5
+ describe PUB_SUB_CLASS do
6
+
7
+ def new_test_bus
8
+ PUB_SUB_CLASS.new(MESSAGE_BUS_CONFIG.merge(:db => 10))
9
+ end
10
+
11
+ before do
12
+ @bus = new_test_bus
13
+ @bus.reset!
14
+ end
15
+
16
+ describe "readonly" do
17
+
18
+ after do
19
+ @bus.pub_redis.slaveof "no", "one"
20
+ end
21
+
22
+ it "should be able to store messages in memory for a period while in read only" do
23
+
24
+ skip "This spec changes redis behavior that in turn means other specs run slow"
25
+
26
+ @bus.pub_redis.slaveof "127.0.0.80", "666"
27
+ @bus.max_in_memory_publish_backlog = 2
28
+
29
+ 3.times do
30
+ result = @bus.publish "/foo", "bar"
31
+ result.must_equal nil
32
+ end
33
+
34
+ @bus.pub_redis.slaveof "no", "one"
35
+ sleep 0.01
36
+
37
+ @bus.backlog("/foo", 0).map(&:data).must_equal ["bar","bar"]
38
+
39
+ end
40
+ end
41
+
42
+ it "should be able to access the backlog" do
43
+ @bus.publish "/foo", "bar"
44
+ @bus.publish "/foo", "baz"
45
+
46
+ @bus.backlog("/foo", 0).to_a.must_equal [
47
+ MessageBus::Message.new(1,1,'/foo','bar'),
48
+ MessageBus::Message.new(2,2,'/foo','baz')
49
+ ]
50
+ end
51
+
52
+ it "should initialize with max_backlog_size" do
53
+ PUB_SUB_CLASS.new({},2000).max_backlog_size.must_equal 2000
54
+ end
55
+
56
+ it "should truncate channels correctly" do
57
+ @bus.max_backlog_size = 2
58
+ 4.times do |t|
59
+ @bus.publish "/foo", t.to_s
60
+ end
61
+
62
+ @bus.backlog("/foo").to_a.must_equal [
63
+ MessageBus::Message.new(3,3,'/foo','2'),
64
+ MessageBus::Message.new(4,4,'/foo','3'),
65
+ ]
66
+ end
67
+
68
+ it "should truncate global backlog correctly" do
69
+ @bus.max_global_backlog_size = 2
70
+ @bus.publish "/foo", "1"
71
+ @bus.publish "/bar", "2"
72
+ @bus.publish "/baz", "3"
73
+
74
+ @bus.global_backlog.length.must_equal 2
75
+ end
76
+
77
+ it "should be able to grab a message by id" do
78
+ id1 = @bus.publish "/foo", "bar"
79
+ id2 = @bus.publish "/foo", "baz"
80
+ @bus.get_message("/foo", id2).must_equal MessageBus::Message.new(2, 2, "/foo", "baz")
81
+ @bus.get_message("/foo", id1).must_equal MessageBus::Message.new(1, 1, "/foo", "bar")
82
+ end
83
+
84
+ it "should be able to access the global backlog" do
85
+ @bus.publish "/foo", "bar"
86
+ @bus.publish "/hello", "world"
87
+ @bus.publish "/foo", "baz"
88
+ @bus.publish "/hello", "planet"
89
+
90
+ @bus.global_backlog.to_a.must_equal [
91
+ MessageBus::Message.new(1, 1, "/foo", "bar"),
92
+ MessageBus::Message.new(2, 2, "/hello", "world"),
93
+ MessageBus::Message.new(3, 3, "/foo", "baz"),
94
+ MessageBus::Message.new(4, 4, "/hello", "planet")
95
+ ]
96
+ end
97
+
98
+ it "should correctly omit dropped messages from the global backlog" do
99
+ @bus.max_backlog_size = 1
100
+ @bus.publish "/foo", "a"
101
+ @bus.publish "/foo", "b"
102
+ @bus.publish "/bar", "a"
103
+ @bus.publish "/bar", "b"
104
+
105
+ @bus.global_backlog.to_a.must_equal [
106
+ MessageBus::Message.new(2, 2, "/foo", "b"),
107
+ MessageBus::Message.new(4, 4, "/bar", "b")
108
+ ]
109
+ end
110
+
111
+ it "should have the correct number of messages for multi threaded access" do
112
+ threads = []
113
+ 4.times do
114
+ threads << Thread.new do
115
+ bus = new_test_bus
116
+ 25.times {
117
+ bus.publish "/foo", "."
118
+ }
119
+ end
120
+ end
121
+
122
+ threads.each{|t| t.join}
123
+ @bus.backlog("/foo").length == 100
124
+ end
125
+
126
+ it "should be able to subscribe globally with recovery" do
127
+ @bus.publish("/foo", "1")
128
+ @bus.publish("/bar", "2")
129
+ got = []
130
+
131
+ t = Thread.new do
132
+ new_test_bus.global_subscribe(0) do |msg|
133
+ got << msg
134
+ end
135
+ end
136
+
137
+ @bus.publish("/bar", "3")
138
+
139
+ wait_for(100) do
140
+ got.length == 3
141
+ end
142
+
143
+ t.kill
144
+
145
+ got.length.must_equal 3
146
+ got.map{|m| m.data}.must_equal ["1","2","3"]
147
+ end
148
+
149
+ it "should be able to encode and decode messages properly" do
150
+ m = MessageBus::Message.new 1,2,'||','||'
151
+ MessageBus::Message.decode(m.encode).must_equal m
152
+ end
153
+
154
+ it "should handle subscribe on single channel, with recovery" do
155
+ @bus.publish("/foo", "1")
156
+ @bus.publish("/bar", "2")
157
+ got = []
158
+
159
+ t = Thread.new do
160
+ new_test_bus.subscribe("/foo",0) do |msg|
161
+ got << msg
162
+ end
163
+ end
164
+
165
+ @bus.publish("/foo", "3")
166
+
167
+ wait_for(100) do
168
+ got.length == 2
169
+ end
170
+
171
+ t.kill
172
+
173
+ got.map{|m| m.data}.must_equal ["1","3"]
174
+ end
175
+
176
+ it "should not get backlog if subscribe is called without params" do
177
+ @bus.publish("/foo", "1")
178
+ got = []
179
+
180
+ t = Thread.new do
181
+ new_test_bus.subscribe("/foo") do |msg|
182
+ got << msg
183
+ end
184
+ end
185
+
186
+ # sleep 50ms to allow the bus to correctly subscribe,
187
+ # I thought about adding a subscribed callback, but outside of testing it matters less
188
+ sleep 0.05
189
+
190
+ @bus.publish("/foo", "2")
191
+
192
+ wait_for(100) do
193
+ got.length == 1
194
+ end
195
+
196
+ t.kill
197
+
198
+ got.map{|m| m.data}.must_equal ["2"]
199
+ end
200
+
201
+ it "should allow us to get last id on a channel" do
202
+ @bus.last_id("/foo").must_equal 0
203
+ @bus.publish("/foo", "1")
204
+ @bus.last_id("/foo").must_equal 1
205
+ end
206
+
207
+ end
208
+ end
@@ -1,10 +1,11 @@
1
- require 'spec_helper'
1
+ require_relative '../../../spec_helper'
2
2
  require 'message_bus'
3
3
 
4
- describe MessageBus::Redis::ReliablePubSub do
4
+ if MESSAGE_BUS_CONFIG[:backend] == :redis
5
+ describe PUB_SUB_CLASS do
5
6
 
6
7
  def new_test_bus
7
- MessageBus::Redis::ReliablePubSub.new(:db => 10)
8
+ PUB_SUB_CLASS.new(MESSAGE_BUS_CONFIG.merge(:db => 10))
8
9
  end
9
10
 
10
11
  before do
@@ -12,7 +13,7 @@ describe MessageBus::Redis::ReliablePubSub do
12
13
  @bus.reset!
13
14
  end
14
15
 
15
- context "readonly" do
16
+ describe "readonly" do
16
17
 
17
18
  after do
18
19
  @bus.pub_redis.slaveof "no", "one"
@@ -27,13 +28,13 @@ describe MessageBus::Redis::ReliablePubSub do
27
28
 
28
29
  3.times do
29
30
  result = @bus.publish "/foo", "bar"
30
- result.should == nil
31
+ result.must_equal nil
31
32
  end
32
33
 
33
34
  @bus.pub_redis.slaveof "no", "one"
34
35
  sleep 0.01
35
36
 
36
- @bus.backlog("/foo", 0).map(&:data).should == ["bar","bar"]
37
+ @bus.backlog("/foo", 0).map(&:data).must_equal ["bar","bar"]
37
38
 
38
39
  end
39
40
  end
@@ -41,22 +42,22 @@ describe MessageBus::Redis::ReliablePubSub do
41
42
  it "can set backlog age" do
42
43
  @bus.max_backlog_age = 100
43
44
  @bus.publish "/foo", "bar"
44
- @bus.pub_redis.ttl(@bus.backlog_key("/foo")).should be <= 100
45
- @bus.pub_redis.ttl(@bus.backlog_key("/foo")).should be > 0
45
+ @bus.pub_redis.ttl(@bus.backlog_key("/foo")).must_be :<=, 100
46
+ @bus.pub_redis.ttl(@bus.backlog_key("/foo")).must_be :>, 0
46
47
  end
47
48
 
48
49
  it "should be able to access the backlog" do
49
50
  @bus.publish "/foo", "bar"
50
51
  @bus.publish "/foo", "baz"
51
52
 
52
- @bus.backlog("/foo", 0).to_a.should == [
53
+ @bus.backlog("/foo", 0).to_a.must_equal [
53
54
  MessageBus::Message.new(1,1,'/foo','bar'),
54
55
  MessageBus::Message.new(2,2,'/foo','baz')
55
56
  ]
56
57
  end
57
58
 
58
59
  it "should initialize with max_backlog_size" do
59
- MessageBus::Redis::ReliablePubSub.new({},2000).max_backlog_size.should == 2000
60
+ PUB_SUB_CLASS.new({},2000).max_backlog_size.must_equal 2000
60
61
  end
61
62
 
62
63
  it "should truncate channels correctly" do
@@ -65,7 +66,7 @@ describe MessageBus::Redis::ReliablePubSub do
65
66
  @bus.publish "/foo", t.to_s
66
67
  end
67
68
 
68
- @bus.backlog("/foo").to_a.should == [
69
+ @bus.backlog("/foo").to_a.must_equal [
69
70
  MessageBus::Message.new(3,3,'/foo','2'),
70
71
  MessageBus::Message.new(4,4,'/foo','3'),
71
72
  ]
@@ -77,14 +78,14 @@ describe MessageBus::Redis::ReliablePubSub do
77
78
  @bus.publish "/bar", "2"
78
79
  @bus.publish "/baz", "3"
79
80
 
80
- @bus.global_backlog.length.should == 2
81
+ @bus.global_backlog.length.must_equal 2
81
82
  end
82
83
 
83
84
  it "should be able to grab a message by id" do
84
85
  id1 = @bus.publish "/foo", "bar"
85
86
  id2 = @bus.publish "/foo", "baz"
86
- @bus.get_message("/foo", id2).should == MessageBus::Message.new(2, 2, "/foo", "baz")
87
- @bus.get_message("/foo", id1).should == MessageBus::Message.new(1, 1, "/foo", "bar")
87
+ @bus.get_message("/foo", id2).must_equal MessageBus::Message.new(2, 2, "/foo", "baz")
88
+ @bus.get_message("/foo", id1).must_equal MessageBus::Message.new(1, 1, "/foo", "bar")
88
89
  end
89
90
 
90
91
  it "should be able to access the global backlog" do
@@ -93,7 +94,7 @@ describe MessageBus::Redis::ReliablePubSub do
93
94
  @bus.publish "/foo", "baz"
94
95
  @bus.publish "/hello", "planet"
95
96
 
96
- @bus.global_backlog.to_a.should == [
97
+ @bus.global_backlog.to_a.must_equal [
97
98
  MessageBus::Message.new(1, 1, "/foo", "bar"),
98
99
  MessageBus::Message.new(2, 1, "/hello", "world"),
99
100
  MessageBus::Message.new(3, 2, "/foo", "baz"),
@@ -108,7 +109,7 @@ describe MessageBus::Redis::ReliablePubSub do
108
109
  @bus.publish "/bar", "a"
109
110
  @bus.publish "/bar", "b"
110
111
 
111
- @bus.global_backlog.to_a.should == [
112
+ @bus.global_backlog.to_a.must_equal [
112
113
  MessageBus::Message.new(2, 2, "/foo", "b"),
113
114
  MessageBus::Message.new(4, 2, "/bar", "b")
114
115
  ]
@@ -148,13 +149,13 @@ describe MessageBus::Redis::ReliablePubSub do
148
149
 
149
150
  t.kill
150
151
 
151
- got.length.should == 3
152
- got.map{|m| m.data}.should == ["1","2","3"]
152
+ got.length.must_equal 3
153
+ got.map{|m| m.data}.must_equal ["1","2","3"]
153
154
  end
154
155
 
155
156
  it "should be able to encode and decode messages properly" do
156
157
  m = MessageBus::Message.new 1,2,'||','||'
157
- MessageBus::Message.decode(m.encode).should == m
158
+ MessageBus::Message.decode(m.encode).must_equal m
158
159
  end
159
160
 
160
161
  it "should handle subscribe on single channel, with recovery" do
@@ -176,7 +177,7 @@ describe MessageBus::Redis::ReliablePubSub do
176
177
 
177
178
  t.kill
178
179
 
179
- got.map{|m| m.data}.should == ["1","3"]
180
+ got.map{|m| m.data}.must_equal ["1","3"]
180
181
  end
181
182
 
182
183
  it "should not get backlog if subscribe is called without params" do
@@ -201,13 +202,14 @@ describe MessageBus::Redis::ReliablePubSub do
201
202
 
202
203
  t.kill
203
204
 
204
- got.map{|m| m.data}.should == ["2"]
205
+ got.map{|m| m.data}.must_equal ["2"]
205
206
  end
206
207
 
207
208
  it "should allow us to get last id on a channel" do
208
- @bus.last_id("/foo").should == 0
209
+ @bus.last_id("/foo").must_equal 0
209
210
  @bus.publish("/foo", "1")
210
- @bus.last_id("/foo").should == 1
211
+ @bus.last_id("/foo").must_equal 1
211
212
  end
212
213
 
213
214
  end
215
+ end
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require_relative '../../spec_helper'
2
2
  require 'message_bus'
3
3
 
4
4
  describe MessageBus::Client do
@@ -7,6 +7,7 @@ describe MessageBus::Client do
7
7
 
8
8
  before do
9
9
  @bus = MessageBus::Instance.new
10
+ @bus.redis_config = MESSAGE_BUS_CONFIG
10
11
  @client = MessageBus::Client.new client_id: 'abc', message_bus: @bus
11
12
  end
12
13
 
@@ -60,36 +61,36 @@ describe MessageBus::Client do
60
61
 
61
62
  status, headers, chunks = http_parse(lines)
62
63
 
63
- headers["Content-Type"].should == "text/plain; charset=utf-8"
64
- status.should == "200"
65
- chunks.length.should == 2
64
+ headers["Content-Type"].must_equal "text/plain; charset=utf-8"
65
+ status.must_equal "200"
66
+ chunks.length.must_equal 2
66
67
 
67
68
  chunk1 = parse_chunk(chunks[0])
68
- chunk1.length.should == 1
69
- chunk1.first["data"].should == 'test'
69
+ chunk1.length.must_equal 1
70
+ chunk1.first["data"].must_equal 'test'
70
71
 
71
72
  chunk2 = parse_chunk(chunks[1])
72
- chunk2.length.should == 1
73
- chunk2.first["data"].should == "a|\r\n|\r\n|b"
73
+ chunk2.length.must_equal 1
74
+ chunk2.first["data"].must_equal "a|\r\n|\r\n|b"
74
75
 
75
76
  @client << MessageBus::Message.new(3, 3, '/test', 'test3')
76
77
  @client.close
77
78
 
78
79
  data = r.read
79
80
 
80
- data[-5..-1].should == "0\r\n\r\n"
81
+ data[-5..-1].must_equal "0\r\n\r\n"
81
82
 
82
83
  _,_,chunks = http_parse("HTTP/1.1 200 OK\r\n\r\n" << data)
83
84
 
84
- chunks.length.should == 2
85
+ chunks.length.must_equal 2
85
86
 
86
87
  chunk1 = parse_chunk(chunks[0])
87
- chunk1.length.should == 1
88
- chunk1.first["data"].should == 'test3'
88
+ chunk1.length.must_equal 1
89
+ chunk1.first["data"].must_equal 'test3'
89
90
 
90
91
  # end with []
91
92
  chunk2 = parse_chunk(chunks[1])
92
- chunk2.length.should == 0
93
+ chunk2.length.must_equal 0
93
94
 
94
95
  end
95
96
 
@@ -99,7 +100,7 @@ describe MessageBus::Client do
99
100
  @client.subscribe('/hello', nil)
100
101
  @bus.publish '/hello', 'world'
101
102
  log = @client.backlog
102
- log.length.should == 0
103
+ log.length.must_equal 0
103
104
  end
104
105
 
105
106
  it "does not bleed status accross sites" do
@@ -108,41 +109,41 @@ describe MessageBus::Client do
108
109
  @client.subscribe('/hello', -1)
109
110
  @bus.publish '/hello', 'world'
110
111
  log = @client.backlog
111
- log[0].data.should == {"/hello" => 0}
112
+ log[0].data.must_equal("/hello" => 0)
112
113
  end
113
114
 
114
115
  it "provides status" do
115
116
  @client.subscribe('/hello', -1)
116
117
  log = @client.backlog
117
- log.length.should == 1
118
- log[0].data.should == {"/hello" => 0}
118
+ log.length.must_equal 1
119
+ log[0].data.must_equal("/hello" => 0)
119
120
  end
120
121
 
121
122
  it "should provide a list of subscriptions" do
122
123
  @client.subscribe('/hello', nil)
123
- @client.subscriptions['/hello'].should_not be_nil
124
+ @client.subscriptions['/hello'].wont_equal nil
124
125
  end
125
126
 
126
127
  it "should provide backlog for subscribed channel" do
127
128
  @client.subscribe('/hello', nil)
128
129
  @bus.publish '/hello', 'world'
129
130
  log = @client.backlog
130
- log.length.should == 1
131
- log[0].channel.should == '/hello'
132
- log[0].data.should == 'world'
131
+ log.length.must_equal 1
132
+ log[0].channel.must_equal '/hello'
133
+ log[0].data.must_equal 'world'
133
134
  end
134
135
 
135
136
  it "allows only client_id in list if message contains client_ids" do
136
137
  @message = MessageBus::Message.new(1, 2, '/test', 'hello')
137
138
  @message.client_ids = ["1","2"]
138
139
  @client.client_id = "2"
139
- @client.allowed?(@message).should == true
140
+ @client.allowed?(@message).must_equal true
140
141
 
141
142
  @client.client_id = "3"
142
- @client.allowed?(@message).should == false
143
+ @client.allowed?(@message).must_equal false
143
144
  end
144
145
 
145
- context "targetted at group" do
146
+ describe "targetted at group" do
146
147
  before do
147
148
  @message = MessageBus::Message.new(1,2,'/test', 'hello')
148
149
  @message.group_ids = [1,2,3]
@@ -150,18 +151,18 @@ describe MessageBus::Client do
150
151
 
151
152
  it "denies users that are not members of group" do
152
153
  @client.group_ids = [77,0,10]
153
- @client.allowed?(@message).should == false
154
+ @client.allowed?(@message).must_equal false
154
155
  end
155
156
 
156
157
  it "allows users that are members of group" do
157
158
  @client.group_ids = [1,2,3]
158
- @client.allowed?(@message).should == true
159
+ @client.allowed?(@message).must_equal true
159
160
  end
160
161
 
161
162
  it "allows all users if groups not set" do
162
163
  @message.group_ids = nil
163
164
  @client.group_ids = [77,0,10]
164
- @client.allowed?(@message).should == true
165
+ @client.allowed?(@message).must_equal true
165
166
  end
166
167
 
167
168
  end