message_bus 2.2.3 → 3.3.1

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.

Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +9 -1
  3. data/.travis.yml +1 -1
  4. data/CHANGELOG +39 -0
  5. data/Gemfile +8 -3
  6. data/Guardfile +1 -0
  7. data/README.md +62 -11
  8. data/Rakefile +5 -0
  9. data/assets/message-bus.js +64 -78
  10. data/examples/bench/config.ru +1 -0
  11. data/examples/bench/puma.rb +1 -0
  12. data/examples/bench/unicorn.conf.rb +1 -0
  13. data/examples/chat/Gemfile +1 -0
  14. data/examples/chat/chat.rb +2 -0
  15. data/examples/chat/config.ru +2 -0
  16. data/examples/diagnostics/Gemfile +1 -0
  17. data/examples/diagnostics/config.ru +1 -0
  18. data/examples/minimal/Gemfile +1 -0
  19. data/examples/minimal/config.ru +1 -0
  20. data/lib/message_bus.rb +33 -0
  21. data/lib/message_bus/backends/redis.rb +11 -8
  22. data/lib/message_bus/client.rb +36 -8
  23. data/lib/message_bus/diagnostics.rb +1 -1
  24. data/lib/message_bus/em_ext.rb +1 -0
  25. data/lib/message_bus/http_client.rb +2 -1
  26. data/lib/message_bus/http_client/channel.rb +1 -0
  27. data/lib/message_bus/rack/diagnostics.rb +5 -4
  28. data/lib/message_bus/rack/middleware.rb +8 -4
  29. data/lib/message_bus/rails/railtie.rb +15 -13
  30. data/lib/message_bus/version.rb +1 -1
  31. data/package.json +20 -0
  32. data/spec/assets/message-bus.spec.js +0 -9
  33. data/spec/assets/support/jasmine_helper.rb +1 -0
  34. data/spec/fixtures/test/Gemfile +1 -0
  35. data/spec/fixtures/test/config.ru +1 -0
  36. data/spec/helpers.rb +1 -0
  37. data/spec/integration/http_client_spec.rb +2 -0
  38. data/spec/lib/fake_async_middleware.rb +3 -2
  39. data/spec/lib/message_bus/assets/asset_encoding_spec.rb +1 -0
  40. data/spec/lib/message_bus/backend_spec.rb +2 -0
  41. data/spec/lib/message_bus/client_spec.rb +208 -23
  42. data/spec/lib/message_bus/connection_manager_spec.rb +3 -1
  43. data/spec/lib/message_bus/distributed_cache_spec.rb +2 -0
  44. data/spec/lib/message_bus/multi_process_spec.rb +2 -0
  45. data/spec/lib/message_bus/rack/middleware_spec.rb +63 -0
  46. data/spec/lib/message_bus/timer_thread_spec.rb +2 -0
  47. data/spec/lib/message_bus_spec.rb +34 -0
  48. data/spec/performance/publish.rb +2 -0
  49. data/spec/spec_helper.rb +3 -1
  50. metadata +4 -3
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require_relative '../../../spec_helper'
2
3
  asset_directory = File.expand_path('../../../../../assets', __FILE__)
3
4
  asset_file_paths = Dir.glob(File.join(asset_directory, 'message-bus.js'))
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative '../../spec_helper'
2
4
  require 'message_bus'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative '../../spec_helper'
2
4
  require 'message_bus'
3
5
 
@@ -83,7 +85,7 @@ describe MessageBus::Client do
83
85
 
84
86
  data[-5..-1].must_equal "0\r\n\r\n"
85
87
 
86
- _, _, chunks = http_parse("HTTP/1.1 200 OK\r\n\r\n" << data)
88
+ _, _, chunks = http_parse(+"HTTP/1.1 200 OK\r\n\r\n" << data)
87
89
 
88
90
  chunks.length.must_equal 2
89
91
 
@@ -167,36 +169,219 @@ describe MessageBus::Client do
167
169
  log[0].data.must_equal 'world'
168
170
  end
169
171
 
170
- it "allows only client_id in list if message contains client_ids" do
171
- @message = MessageBus::Message.new(1, 2, '/test', 'hello')
172
- @message.client_ids = ["1", "2"]
173
- @client.client_id = "2"
174
- @client.allowed?(@message).must_equal true
172
+ describe '#allowed?' do
173
+ it "allows only client_id in list if message contains client_ids" do
174
+ @message = MessageBus::Message.new(1, 2, '/test', 'hello')
175
+ @message.client_ids = ["1", "2"]
176
+ @client.client_id = "2"
177
+ @client.allowed?(@message).must_equal true
175
178
 
176
- @client.client_id = "3"
177
- @client.allowed?(@message).must_equal false
178
- end
179
+ @client.client_id = "3"
180
+ @client.allowed?(@message).must_equal false
179
181
 
180
- describe "targetted at group" do
181
- before do
182
- @message = MessageBus::Message.new(1, 2, '/test', 'hello')
183
- @message.group_ids = [1, 2, 3]
182
+ @message.client_ids = []
183
+
184
+ @client.client_id = "3"
185
+ @client.allowed?(@message).must_equal true
186
+
187
+ @message.client_ids = nil
188
+
189
+ @client.client_id = "3"
190
+ @client.allowed?(@message).must_equal true
184
191
  end
185
192
 
186
- it "denies users that are not members of group" do
187
- @client.group_ids = [77, 0, 10]
188
- @client.allowed?(@message).must_equal false
193
+ describe 'targetted at user' do
194
+ before do
195
+ @message = MessageBus::Message.new(1, 2, '/test', 'hello')
196
+ @message.user_ids = [1, 2, 3]
197
+ end
198
+
199
+ it "allows client with user_id that is included in message's user_ids" do
200
+ @client.user_id = 1
201
+ @client.allowed?(@message).must_equal(true)
202
+ end
203
+
204
+ it "denies client with user_id that is not included in message's user_ids" do
205
+ @client.user_id = 4
206
+ @client.allowed?(@message).must_equal(false)
207
+ end
208
+
209
+ it "denies client with nil user_id" do
210
+ @client.user_id = nil
211
+
212
+ @client.allowed?(@message).must_equal(false)
213
+ end
214
+
215
+ it "allows client if message's user_ids is not set" do
216
+ @message.user_ids = nil
217
+ @client.user_id = 4
218
+ @client.allowed?(@message).must_equal(true)
219
+ end
220
+
221
+ it "allows client if message's user_ids is empty" do
222
+ @message.user_ids = []
223
+ @client.user_id = 4
224
+ @client.allowed?(@message).must_equal(true)
225
+ end
226
+
227
+ it "allows client with client_id that is included in message's client_ids" do
228
+ @message.client_ids = ["1", "2"]
229
+ @client.client_id = "1"
230
+ @client.user_id = 1
231
+
232
+ @client.allowed?(@message).must_equal(true)
233
+ end
234
+
235
+ it "denies client with client_id that is not included in message's client_ids" do
236
+ @message.client_ids = ["1", "2"]
237
+ @client.client_id = "3"
238
+ @client.user_id = 1
239
+
240
+ @client.allowed?(@message).must_equal(false)
241
+ end
189
242
  end
190
243
 
191
- it "allows users that are members of group" do
192
- @client.group_ids = [1, 2, 3]
193
- @client.allowed?(@message).must_equal true
244
+ describe "targetted at group" do
245
+ before do
246
+ @message = MessageBus::Message.new(1, 2, '/test', 'hello')
247
+ @message.group_ids = [1, 2, 3]
248
+ end
249
+
250
+ it "denies client that are not members of group" do
251
+ @client.group_ids = [77, 0, 10]
252
+ @client.allowed?(@message).must_equal false
253
+ end
254
+
255
+ it 'denies client with nil group_ids' do
256
+ @client.group_ids = nil
257
+ @client.allowed?(@message).must_equal false
258
+ end
259
+
260
+ it "allows client that are members of group" do
261
+ @client.group_ids = [1, 2, 3]
262
+ @client.allowed?(@message).must_equal true
263
+ end
264
+
265
+ it "allows any client if message's group_ids is not set" do
266
+ @message.group_ids = nil
267
+ @client.group_ids = [77, 0, 10]
268
+ @client.allowed?(@message).must_equal true
269
+ end
270
+
271
+ it "allows any client if message's group_ids is empty" do
272
+ @message.group_ids = []
273
+ @client.group_ids = [77, 0, 10]
274
+ @client.allowed?(@message).must_equal true
275
+ end
276
+
277
+ it "allows client with client_id that is included in message's client_ids" do
278
+ @message.client_ids = ["1", "2"]
279
+ @client.client_id = "1"
280
+ @client.group_ids = [1]
281
+
282
+ @client.allowed?(@message).must_equal(true)
283
+ end
284
+
285
+ it "denies client with client_id that is not included in message's client_ids" do
286
+ @message.client_ids = ["1", "2"]
287
+ @client.client_id = "3"
288
+ @client.group_ids = [1]
289
+
290
+ @client.allowed?(@message).must_equal(false)
291
+ end
194
292
  end
195
293
 
196
- it "allows all users if groups not set" do
197
- @message.group_ids = nil
198
- @client.group_ids = [77, 0, 10]
199
- @client.allowed?(@message).must_equal true
294
+ describe 'targetted at group and user' do
295
+ before do
296
+ @message = MessageBus::Message.new(1, 2, '/test', 'hello')
297
+ @message.group_ids = [1, 2, 3]
298
+ @message.user_ids = [4, 5, 6]
299
+ end
300
+
301
+ it "allows client with user_id that is included in message's user_ids" do
302
+ @client.user_id = 4
303
+ @client.allowed?(@message).must_equal(true)
304
+ end
305
+
306
+ it "denies client with user_id that is not included in message's user_ids" do
307
+ @client.user_id = 1
308
+ @client.allowed?(@message).must_equal(false)
309
+ end
310
+
311
+ it "allows client with group_ids that is included in message's group_ids" do
312
+ @client.group_ids = [1, 0, 3]
313
+ @client.allowed?(@message).must_equal(true)
314
+ end
315
+
316
+ it "denies client with group_ids that is not included in message's group_ids" do
317
+ @client.group_ids = [8, 9, 10]
318
+ @client.allowed?(@message).must_equal(false)
319
+ end
320
+
321
+ it "allows client with allowed client_id and user_id" do
322
+ @message.client_ids = ["1", "2"]
323
+ @client.user_id = 4
324
+ @client.client_id = "2"
325
+
326
+ @client.allowed?(@message).must_equal(true)
327
+ end
328
+
329
+ it "denies client with allowed client_id but disallowed user_id" do
330
+ @message.client_ids = ["1", "2"]
331
+ @client.user_id = 99
332
+ @client.client_id = "2"
333
+
334
+ @client.allowed?(@message).must_equal(false)
335
+ end
336
+
337
+ it "allows client with allowed client_id and group_id" do
338
+ @message.client_ids = ["1", "2"]
339
+ @client.group_ids = [1]
340
+ @client.client_id = "2"
341
+
342
+ @client.allowed?(@message).must_equal(true)
343
+ end
344
+
345
+ it "denies client with allowed client_id but disallowed group_id" do
346
+ @message.client_ids = ["1", "2"]
347
+ @client.group_ids = [99]
348
+ @client.client_id = "2"
349
+
350
+ @client.allowed?(@message).must_equal(false)
351
+ end
352
+ end
353
+
354
+ describe 'when MessageBus#client_message_filters has been configured' do
355
+
356
+ it 'filters messages correctly' do
357
+ message = MessageBus::Message.new(1, 2, '/test/5', 'hello')
358
+ @client.allowed?(message).must_equal(true)
359
+
360
+ @bus.register_client_message_filter('/test') do |m|
361
+ m.data != 'hello'
362
+ end
363
+
364
+ @client.allowed?(message).must_equal(false)
365
+ end
366
+
367
+ it 'filters messages correctly when multiple filters have been configured' do
368
+
369
+ bob_message = MessageBus::Message.new(1, 2, '/test/5', 'bob')
370
+ fred_message = MessageBus::Message.new(1, 2, '/test/5', 'fred')
371
+ random_message = MessageBus::Message.new(1, 2, '/test/77', 'random')
372
+
373
+ @bus.register_client_message_filter('/test') do |message|
374
+ message.data == 'bob' || message.data == 'fred'
375
+ end
376
+
377
+ @bus.register_client_message_filter('/test') do |message|
378
+ message.data == 'fred'
379
+ end
380
+
381
+ @client.allowed?(fred_message).must_equal(true)
382
+ @client.allowed?(bob_message).must_equal(false)
383
+ @client.allowed?(random_message).must_equal(false)
384
+ end
200
385
  end
201
386
  end
202
387
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative '../../spec_helper'
2
4
  require 'message_bus'
3
5
 
@@ -6,7 +8,7 @@ class FakeAsync
6
8
 
7
9
  def <<(val)
8
10
  sleep 0.01 # simulate IO
9
- @sent ||= ""
11
+ @sent ||= +""
10
12
  @sent << val
11
13
  end
12
14
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative '../../spec_helper'
2
4
  require 'minitest/hooks/default'
3
5
  require 'message_bus'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative '../../spec_helper'
2
4
  require 'message_bus'
3
5
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # coding: utf-8
2
3
 
3
4
  require_relative '../../../spec_helper'
@@ -7,11 +8,13 @@ require 'rack/test'
7
8
  describe MessageBus::Rack::Middleware do
8
9
  include Rack::Test::Methods
9
10
  let(:extra_middleware) { nil }
11
+ let(:base_route) { nil }
10
12
 
11
13
  before do
12
14
  bus = @bus = MessageBus::Instance.new
13
15
  @bus.configure(MESSAGE_BUS_CONFIG)
14
16
  @bus.long_polling_enabled = false
17
+ @bus.base_route = base_route if base_route
15
18
 
16
19
  e_m = extra_middleware
17
20
  builder = Rack::Builder.new {
@@ -43,12 +46,27 @@ describe MessageBus::Rack::Middleware do
43
46
  @bus.long_polling_enabled = true
44
47
  end
45
48
 
49
+ describe "with altered base_route" do
50
+ let(:base_route) { "/base/route/" }
51
+
52
+ it "should respond as normal" do
53
+ post "/base/route/message-bus/ABC?dlp=t", '/foo1' => 0
54
+ @async_middleware.in_async?.must_equal false
55
+ last_response.ok?.must_equal true
56
+ end
57
+ end
58
+
46
59
  it "should respond right away if dlp=t" do
47
60
  post "/message-bus/ABC?dlp=t", '/foo1' => 0
48
61
  @async_middleware.in_async?.must_equal false
49
62
  last_response.ok?.must_equal true
50
63
  end
51
64
 
65
+ it "should respond with a 404 if the client_id is missing" do
66
+ post "/message-bus/?dlp=t", '/foo1' => 0
67
+ last_response.not_found?.must_equal true
68
+ end
69
+
52
70
  it "should respond right away to long polls that are polling on -1 with the last_id" do
53
71
  post "/message-bus/ABC", '/foo' => -1
54
72
  last_response.ok?.must_equal true
@@ -140,6 +158,19 @@ describe MessageBus::Rack::Middleware do
140
158
  last_response.status.must_equal 200
141
159
  end
142
160
 
161
+ describe "with an altered base_route" do
162
+ let(:base_route) { "/base/route/" }
163
+
164
+ it "should get a 200 with html for an authorized user" do
165
+ def @bus.is_admin_lookup
166
+ proc { |_| true }
167
+ end
168
+
169
+ get "/base/route/message-bus/_diagnostics"
170
+ last_response.status.must_equal 200
171
+ end
172
+ end
173
+
143
174
  it "should get the script it asks for" do
144
175
 
145
176
  def @bus.is_admin_lookup
@@ -242,6 +273,38 @@ describe MessageBus::Rack::Middleware do
242
273
  parsed[1]["data"].must_equal "borbs"
243
274
  end
244
275
 
276
+ it "should use the correct client ID" do
277
+ id = @bus.last_id('/foo')
278
+
279
+ client_id = "aBc123"
280
+ @bus.publish("/foo", "msg1", client_ids: [client_id])
281
+ @bus.publish("/foo", "msg2", client_ids: ["not_me#{client_id}"])
282
+
283
+ post "/message-bus/#{client_id}",
284
+ '/foo' => id
285
+
286
+ parsed = JSON.parse(last_response.body)
287
+ parsed.length.must_equal 2
288
+ parsed[0]["data"].must_equal("msg1")
289
+ parsed[1]["data"].wont_equal("msg2")
290
+ end
291
+
292
+ it "should use the correct client ID with additional path" do
293
+ id = @bus.last_id('/foo')
294
+
295
+ client_id = "aBc123"
296
+ @bus.publish("/foo", "msg1", client_ids: [client_id])
297
+ @bus.publish("/foo", "msg2", client_ids: ["not_me#{client_id}"])
298
+
299
+ post "/message-bus/#{client_id}/path/not/needed",
300
+ '/foo' => id
301
+
302
+ parsed = JSON.parse(last_response.body)
303
+ parsed.length.must_equal 2
304
+ parsed[0]["data"].must_equal("msg1")
305
+ parsed[1]["data"].wont_equal("msg2")
306
+ end
307
+
245
308
  it "should have no cross talk" do
246
309
  seq = 0
247
310
  @bus.site_id_lookup do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative '../../spec_helper'
2
4
  require 'message_bus/timer_thread'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative '../spec_helper'
2
4
  require 'message_bus'
3
5
  require 'redis'
@@ -35,6 +37,23 @@ describe MessageBus do
35
37
  @bus.after_fork
36
38
  end
37
39
 
40
+ describe "#base_route=" do
41
+ it "adds leading and trailing slashes" do
42
+ @bus.base_route = "my/base/route"
43
+ @bus.base_route.must_equal '/my/base/route/'
44
+ end
45
+
46
+ it "leaves existing leading and trailing slashes" do
47
+ @bus.base_route = "/my/base/route/"
48
+ @bus.base_route.must_equal '/my/base/route/'
49
+ end
50
+
51
+ it "removes duplicate slashes" do
52
+ @bus.base_route = "//my///base/route"
53
+ @bus.base_route.must_equal '/my/base/route/'
54
+ end
55
+ end
56
+
38
57
  it "can subscribe from a point in time" do
39
58
  @bus.publish("/minion", "banana")
40
59
 
@@ -290,4 +309,19 @@ describe MessageBus do
290
309
 
291
310
  data.must_equal(["pre-fork", "from-fork", "continuation"])
292
311
  end
312
+
313
+ describe '#register_client_message_filter' do
314
+ it 'should register the message filter correctly' do
315
+ @bus.register_client_message_filter('/test')
316
+
317
+ @bus.client_message_filters.must_equal([])
318
+
319
+ @bus.register_client_message_filter('/test') { puts "hello world" }
320
+
321
+ channel, blk = @bus.client_message_filters[0]
322
+
323
+ blk.must_respond_to(:call)
324
+ channel.must_equal('/test')
325
+ end
326
+ end
293
327
  end