message_bus 2.0.5 → 2.0.6

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


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

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6ab5f806affa121e66619143bef08f116e700722
4
- data.tar.gz: 43e51a93bb2ac7fc232b89af04b08f25c526af0e
3
+ metadata.gz: 6e3233d29ea9404805798a4b1a4caddf2a50477c
4
+ data.tar.gz: f77eed645f31f2182dc28d94fdf80dc8746ffffd
5
5
  SHA512:
6
- metadata.gz: 1ce530dbf86a71ae8d7f593446768d4090055cdcddc52335d2ba38482e959375c0165221cda880b1f5a5c2098e7fcdd5a4b28880e5b8161530fd99068da8ef35
7
- data.tar.gz: 8b9b5cc7eff8bea4ae4d94d92fa09a24ff42eb348414d862415162103e97cb814f3e29306299ec9a9f4327f1b524373ef46da6d53c3d7db246a88ad67627341c
6
+ metadata.gz: 4a1b8e23c41ea624f21012e8c9f83be06217d1cfb103dd5c13417aff52bf49fab6dde813185f5186b8bdd226267bd5ac2aec859cdd49c35323e35f8c51a8af57
7
+ data.tar.gz: b1285bcba99c5263eba003781a8423245d198edb88bf2c1265d895a771d6b75bc1da89e119cb973d08a58fa4c98726d9bb75ed425c8b28f089c5fdbed70db8f1
data/CHANGELOG CHANGED
@@ -1,6 +1,14 @@
1
+ 29-09-2017
2
+
3
+ - Version 2.0.6
4
+
5
+ - Fix: correct after_fork so it correctly disconnnects redis
6
+ - Fix: correct message_bus #destroy used in tests to clean up spec (deadlock)
7
+ - Fix: deliver backlog unconditionally when polling (and not long polling)
8
+
1
9
  16-08-2017
2
10
 
3
- - Version 2.0.4
11
+ - Version 2.0.5
4
12
 
5
13
  - Quick fix, build on earlier version of Ruby cause https://github.com/rubygems/rubygems/issues/1448 is still open
6
14
 
data/Gemfile CHANGED
@@ -10,4 +10,8 @@ group :test do
10
10
  gem 'thin'
11
11
  gem 'rack-test', require: 'rack/test'
12
12
  gem 'jasmine'
13
+ gem 'redis'
13
14
  end
15
+
16
+ gem 'redis', '3.3.5', platform: :mri_20
17
+ gem 'rack', '1.6.4', platform: :mri_20
data/lib/message_bus.rb CHANGED
@@ -313,10 +313,13 @@ module MessageBus::Implementation
313
313
  end
314
314
  end
315
315
 
316
+ # mostly used in tests to detroy entire bus
316
317
  def destroy
318
+ return if @destroyed
319
+ reliable_pub_sub.global_unsubscribe
320
+
317
321
  @mutex.synchronize do
318
322
  @subscriptions ||= {}
319
- reliable_pub_sub.global_unsubscribe
320
323
  @destroyed = true
321
324
  end
322
325
  @subscriber_thread.join if @subscriber_thread
@@ -45,7 +45,7 @@ class MessageBus::Redis::ReliablePubSub
45
45
  end
46
46
 
47
47
  def after_fork
48
- pub_redis.client.reconnect
48
+ pub_redis.disconnect!
49
49
  end
50
50
 
51
51
  def redis_channel_name
@@ -270,7 +270,8 @@ class MessageBus::Redis::ReliablePubSub
270
270
 
271
271
  def global_unsubscribe
272
272
  if @redis_global
273
- pub_redis.publish(redis_channel_name, UNSUB_MESSAGE)
273
+ # new connection to avoid deadlock
274
+ new_redis_connection.publish(redis_channel_name, UNSUB_MESSAGE)
274
275
  @redis_global.disconnect
275
276
  @redis_global = nil
276
277
  end
@@ -180,7 +180,7 @@ class MessageBus::Rack::Middleware
180
180
 
181
181
  throw :async
182
182
  else
183
- [200, headers, ["[]"]]
183
+ [200, headers, [self.class.backlog_to_json(backlog)]]
184
184
  end
185
185
 
186
186
  rescue => e
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module MessageBus
3
- VERSION = "2.0.5"
3
+ VERSION = "2.0.6"
4
4
  end
@@ -6,7 +6,7 @@ require 'rack/test'
6
6
 
7
7
  describe MessageBus::Rack::Middleware do
8
8
  include Rack::Test::Methods
9
- let(:extra_middleware){nil}
9
+ let(:extra_middleware) { nil }
10
10
 
11
11
  before do
12
12
  bus = @bus = MessageBus::Instance.new
@@ -15,10 +15,10 @@ describe MessageBus::Rack::Middleware do
15
15
 
16
16
  e_m = extra_middleware
17
17
  builder = Rack::Builder.new {
18
- use FakeAsyncMiddleware, :message_bus => bus
18
+ use FakeAsyncMiddleware, message_bus: bus
19
19
  use e_m if e_m
20
- use MessageBus::Rack::Middleware, :message_bus => bus
21
- run lambda {|env| [500, {'Content-Type' => 'text/html'}, 'should not be called' ]}
20
+ use MessageBus::Rack::Middleware, message_bus: bus
21
+ run lambda { |env| [500, { 'Content-Type' => 'text/html' }, 'should not be called' ] }
22
22
  }
23
23
 
24
24
  @async_middleware = builder.to_app
@@ -63,11 +63,11 @@ describe MessageBus::Rack::Middleware do
63
63
  bus = @bus
64
64
 
65
65
  @bus.extra_response_headers_lookup do |env|
66
- {"FOO" => "BAR"}
66
+ { "FOO" => "BAR" }
67
67
  end
68
68
 
69
69
  Thread.new do
70
- wait_for(2000) {middleware.in_async?}
70
+ wait_for(2000) { middleware.in_async? }
71
71
  bus.publish "/foo", "םוֹלשָׁ"
72
72
  end
73
73
 
@@ -119,13 +119,13 @@ describe MessageBus::Rack::Middleware do
119
119
  end
120
120
 
121
121
  it "should get a 200 with html for an authorized user" do
122
- def @bus.is_admin_lookup; proc{|_| true} end
122
+ def @bus.is_admin_lookup; proc { |_| true } end
123
123
  get "/message-bus/_diagnostics"
124
124
  last_response.status.must_equal 200
125
125
  end
126
126
 
127
127
  it "should get the script it asks for" do
128
- def @bus.is_admin_lookup; proc{|_| true} end
128
+ def @bus.is_admin_lookup; proc { |_| true } end
129
129
  get "/message-bus/_diagnostics/assets/message-bus.js"
130
130
  last_response.status.must_equal 200
131
131
  last_response.content_type.must_equal "text/javascript;"
@@ -140,16 +140,15 @@ describe MessageBus::Rack::Middleware do
140
140
 
141
141
  it "should include access control headers" do
142
142
  @bus.extra_response_headers_lookup do |env|
143
- {"FOO" => "BAR"}
143
+ { "FOO" => "BAR" }
144
144
  end
145
145
 
146
146
  client_id = "ABCD"
147
147
 
148
148
  # client always keeps a list of channels with last message id they got on each
149
- post "/message-bus/#{client_id}", {
149
+ post "/message-bus/#{client_id}",
150
150
  '/foo' => nil,
151
151
  '/bar' => nil
152
- }
153
152
 
154
153
  last_response.headers["FOO"].must_equal "BAR"
155
154
  end
@@ -158,39 +157,47 @@ describe MessageBus::Rack::Middleware do
158
157
  client_id = "ABCD"
159
158
 
160
159
  # client always keeps a list of channels with last message id they got on each
161
- post "/message-bus/#{client_id}", {
160
+ post "/message-bus/#{client_id}",
162
161
  '/foo' => nil,
163
162
  '/bar' => nil
164
- }
163
+
165
164
  last_response.ok?.must_equal true
166
165
  end
167
166
 
168
167
  it "should correctly understand that -1 means stuff from now onwards" do
169
168
 
170
- @bus.publish('foo', 'bar')
169
+ # even if allow chunked
170
+ @bus.chunked_encoding_enabled = true
171
+
172
+ @bus.publish('/foo', 'bar')
173
+ @bus.publish('/baz', 'test')
174
+ @bus.publish('/boom', 'bang')
175
+
176
+ post "/message-bus/ABCD",
177
+ '/foo' => -1,
178
+ '/baz' => @bus.last_id('/baz') + 1,
179
+ '/boom' => -1
171
180
 
172
- post "/message-bus/ABCD", {
173
- '/foo' => -1
174
- }
175
181
  last_response.ok?.must_equal true
176
182
  parsed = JSON.parse(last_response.body)
183
+
177
184
  parsed.length.must_equal 1
178
185
  parsed[0]["channel"].must_equal "/__status"
179
- parsed[0]["data"]["/foo"].must_equal@bus.last_id("/foo")
186
+ parsed[0]["data"]["/foo"].must_equal @bus.last_id("/foo")
187
+ parsed[0]["data"]["/boom"].must_equal @bus.last_id("/boom")
180
188
 
181
189
  end
182
190
 
183
191
  it "should respond with the data if messages exist in the backlog" do
184
- id =@bus.last_id('/foo')
192
+ id = @bus.last_id('/foo')
185
193
 
186
194
  @bus.publish("/foo", "barbs")
187
195
  @bus.publish("/foo", "borbs")
188
196
 
189
197
  client_id = "ABCD"
190
- post "/message-bus/#{client_id}", {
198
+ post "/message-bus/#{client_id}",
191
199
  '/foo' => id,
192
200
  '/bar' => nil
193
- }
194
201
 
195
202
  parsed = JSON.parse(last_response.body)
196
203
  parsed.length.must_equal 2
@@ -202,16 +209,15 @@ describe MessageBus::Rack::Middleware do
202
209
 
203
210
  seq = 0
204
211
  @bus.site_id_lookup do
205
- (seq+=1).to_s
212
+ (seq += 1).to_s
206
213
  end
207
214
 
208
215
  # published on channel 1
209
216
  msg = @bus.publish("/foo", "test")
210
217
 
211
218
  # subscribed on channel 2
212
- post "/message-bus/ABCD", {
213
- '/foo' => (msg-1)
214
- }
219
+ post "/message-bus/ABCD",
220
+ '/foo' => (msg - 1)
215
221
 
216
222
  parsed = JSON.parse(last_response.body)
217
223
  parsed.length.must_equal 0
@@ -222,14 +228,13 @@ describe MessageBus::Rack::Middleware do
222
228
 
223
229
  seq = 0
224
230
  @bus.site_id_lookup do
225
- (seq+=1).to_s
231
+ (seq += 1).to_s
226
232
  end
227
233
 
228
234
  msg = @bus.publish("/global/foo", "test")
229
235
 
230
- post "/message-bus/ABCD", {
231
- '/global/foo' => (msg-1)
232
- }
236
+ post "/message-bus/ABCD",
237
+ '/global/foo' => (msg - 1)
233
238
 
234
239
  parsed = JSON.parse(last_response.body)
235
240
  parsed.length.must_equal 1
@@ -237,27 +242,25 @@ describe MessageBus::Rack::Middleware do
237
242
 
238
243
  it "should not get consumed messages" do
239
244
  @bus.publish("/foo", "barbs")
240
- id =@bus.last_id('/foo')
245
+ id = @bus.last_id('/foo')
241
246
 
242
247
  client_id = "ABCD"
243
- post "/message-bus/#{client_id}", {
248
+ post "/message-bus/#{client_id}",
244
249
  '/foo' => id
245
- }
246
250
 
247
251
  parsed = JSON.parse(last_response.body)
248
252
  parsed.length.must_equal 0
249
253
  end
250
254
 
251
255
  it "should filter by user correctly" do
252
- id =@bus.publish("/foo", "test", user_ids: [1])
256
+ id = @bus.publish("/foo", "test", user_ids: [1])
253
257
  @bus.user_id_lookup do |env|
254
258
  0
255
259
  end
256
260
 
257
261
  client_id = "ABCD"
258
- post "/message-bus/#{client_id}", {
262
+ post "/message-bus/#{client_id}",
259
263
  '/foo' => id - 1
260
- }
261
264
 
262
265
  parsed = JSON.parse(last_response.body)
263
266
  parsed.length.must_equal 1
@@ -271,24 +274,22 @@ describe MessageBus::Rack::Middleware do
271
274
  1
272
275
  end
273
276
 
274
- post "/message-bus/#{client_id}", {
277
+ post "/message-bus/#{client_id}",
275
278
  '/foo' => id - 1
276
- }
277
279
 
278
280
  parsed = JSON.parse(last_response.body)
279
281
  parsed.length.must_equal 1
280
282
  end
281
283
 
282
284
  it "should filter by group correctly" do
283
- id =@bus.publish("/foo", "test", group_ids: [3,4,5])
285
+ id = @bus.publish("/foo", "test", group_ids: [3, 4, 5])
284
286
  @bus.group_ids_lookup do |env|
285
- [0,1,2]
287
+ [0, 1, 2]
286
288
  end
287
289
 
288
290
  client_id = "ABCD"
289
- post "/message-bus/#{client_id}", {
291
+ post "/message-bus/#{client_id}",
290
292
  '/foo' => id - 1
291
- }
292
293
 
293
294
  parsed = JSON.parse(last_response.body)
294
295
  message = parsed.first
@@ -297,12 +298,11 @@ describe MessageBus::Rack::Middleware do
297
298
  message["data"].must_equal("/foo" => 1)
298
299
 
299
300
  @bus.group_ids_lookup do |env|
300
- [1,7,4,100]
301
+ [1, 7, 4, 100]
301
302
  end
302
303
 
303
- post "/message-bus/#{client_id}", {
304
+ post "/message-bus/#{client_id}",
304
305
  '/foo' => id - 1
305
- }
306
306
 
307
307
  parsed = JSON.parse(last_response.body)
308
308
  parsed.length.must_equal 1
@@ -310,11 +310,11 @@ describe MessageBus::Rack::Middleware do
310
310
 
311
311
  it "can decode a JSON encoded request" do
312
312
  id = @bus.last_id('/foo')
313
- @bus.publish("/foo", {json: true})
314
- post( "/message-bus/1234",
315
- JSON.generate({'/foo' => id}),
316
- { "CONTENT_TYPE" => "application/json" })
317
- JSON.parse(last_response.body).first["data"].must_equal({'json' => true})
313
+ @bus.publish("/foo", json: true)
314
+ post("/message-bus/1234",
315
+ JSON.generate('/foo' => id),
316
+ "CONTENT_TYPE" => "application/json")
317
+ JSON.parse(last_response.body).first["data"].must_equal('json' => true)
318
318
  end
319
319
 
320
320
  describe "on_middleware_error handling" do
@@ -322,7 +322,7 @@ describe MessageBus::Rack::Middleware do
322
322
 
323
323
  @bus.on_middleware_error do |env, err|
324
324
  if ArgumentError === err
325
- [407,{},[]]
325
+ [407, {}, []]
326
326
  end
327
327
  end
328
328
 
@@ -330,9 +330,9 @@ describe MessageBus::Rack::Middleware do
330
330
  raise ArgumentError
331
331
  end
332
332
 
333
- post( "/message-bus/1234",
334
- JSON.generate({'/foo' => 1}),
335
- { "CONTENT_TYPE" => "application/json" })
333
+ post("/message-bus/1234",
334
+ JSON.generate('/foo' => 1),
335
+ "CONTENT_TYPE" => "application/json")
336
336
 
337
337
  last_response.status.must_equal 407
338
338
 
@@ -349,7 +349,7 @@ describe MessageBus::Rack::Middleware do
349
349
  end
350
350
 
351
351
  def call(env)
352
- @app.call(env.merge('message_bus.channels'=>{'/foo'=>0}))
352
+ @app.call(env.merge('message_bus.channels' => { '/foo' => 0 }))
353
353
  end
354
354
  end
355
355
  end
@@ -359,16 +359,14 @@ describe MessageBus::Rack::Middleware do
359
359
  foo_id = @bus.publish("/foo", "testfoo")
360
360
  bar_id = @bus.publish("/bar", "testbar")
361
361
 
362
- post "/message-bus/ABCD", {
362
+ post "/message-bus/ABCD",
363
363
  '/foo' => foo_id - 1
364
- }
365
364
 
366
365
  parsed = JSON.parse(last_response.body)
367
366
  parsed.first['data'].must_equal 'testfoo'
368
367
 
369
- post "/message-bus/ABCD", {
368
+ post "/message-bus/ABCD",
370
369
  '/bar' => bar_id - 1
371
- }
372
370
 
373
371
  parsed = JSON.parse(last_response.body)
374
372
  parsed.first['data'].must_equal 'testfoo'
@@ -2,7 +2,6 @@ require_relative '../spec_helper'
2
2
  require 'message_bus'
3
3
  require 'redis'
4
4
 
5
-
6
5
  describe MessageBus do
7
6
 
8
7
  before do
@@ -56,9 +55,8 @@ describe MessageBus do
56
55
  client_ids = msg.client_ids
57
56
  end
58
57
 
59
- @bus.publish("/chuck", {:yeager => true}, client_ids: ['a','b'])
60
-
61
- wait_for(2000){ client_ids}
58
+ @bus.publish("/chuck", { yeager: true }, client_ids: ['a', 'b'])
59
+ wait_for(2000) { client_ids }
62
60
 
63
61
  client_ids.must_equal ['a', 'b']
64
62
 
@@ -70,15 +68,15 @@ describe MessageBus do
70
68
  @bus.subscribe("/chuck") do |msg|
71
69
  data = msg.data
72
70
  end
73
- @bus.publish("/chuck", {:norris => true})
74
- @bus.publish("/chuck", {:norris => true})
75
- @bus.publish("/chuck", {:norris => true})
71
+ @bus.publish("/chuck", norris: true)
72
+ @bus.publish("/chuck", norris: true)
73
+ @bus.publish("/chuck", norris: true)
76
74
 
77
75
  @bus.reliable_pub_sub.reset!
78
76
 
79
- @bus.publish("/chuck", {:yeager => true})
77
+ @bus.publish("/chuck", yeager: true)
80
78
 
81
- wait_for(2000){ data && data["yeager"]}
79
+ wait_for(2000) { data && data["yeager"] }
82
80
 
83
81
  data["yeager"].must_equal true
84
82
 
@@ -89,14 +87,14 @@ describe MessageBus do
89
87
  @bus.subscribe("/chuck") do |msg|
90
88
  data = msg.data
91
89
  end
92
- @bus.publish("/chuck", {:norris => true})
93
- wait_for(2000){ data }
90
+ @bus.publish("/chuck", norris: true)
91
+ wait_for(2000) { data }
94
92
 
95
93
  data["norris"].must_equal true
96
94
  end
97
95
 
98
96
  it "should get a message if it subscribes to it" do
99
- user_ids,data,site_id,channel = nil
97
+ user_ids, data, site_id, channel = nil
100
98
 
101
99
  @bus.subscribe("/chuck") do |msg|
102
100
  data = msg.data
@@ -105,20 +103,19 @@ describe MessageBus do
105
103
  user_ids = msg.user_ids
106
104
  end
107
105
 
108
- @bus.publish("/chuck", "norris", user_ids: [1,2,3])
106
+ @bus.publish("/chuck", "norris", user_ids: [1, 2, 3])
109
107
 
110
- wait_for(2000){data}
108
+ wait_for(2000) { data }
111
109
 
112
110
  data.must_equal 'norris'
113
111
  site_id.must_equal 'magic'
114
112
  channel.must_equal '/chuck'
115
- user_ids.must_equal [1,2,3]
113
+ user_ids.must_equal [1, 2, 3]
116
114
 
117
115
  end
118
116
 
119
-
120
117
  it "should get global messages if it subscribes to them" do
121
- data,site_id,channel = nil
118
+ data, site_id, channel = nil
122
119
 
123
120
  @bus.subscribe do |msg|
124
121
  data = msg.data
@@ -128,7 +125,7 @@ describe MessageBus do
128
125
 
129
126
  @bus.publish("/chuck", "norris")
130
127
 
131
- wait_for(2000){data}
128
+ wait_for(2000) { data }
132
129
 
133
130
  data.must_equal 'norris'
134
131
  site_id.must_equal 'magic'
@@ -143,7 +140,7 @@ describe MessageBus do
143
140
 
144
141
  r = @bus.backlog("/chuck", id)
145
142
 
146
- r.map{|i| i.data}.to_a.must_equal ['foo', 'bar']
143
+ r.map { |i| i.data }.to_a.must_equal ['foo', 'bar']
147
144
  end
148
145
 
149
146
  it "should correctly get full backlog of a channel" do
@@ -151,7 +148,7 @@ describe MessageBus do
151
148
  @bus.publish("/chuck", "foo")
152
149
  @bus.publish("/chuckles", "bar")
153
150
 
154
- @bus.backlog("/chuck").map{|i| i.data}.to_a.must_equal ['norris', 'foo']
151
+ @bus.backlog("/chuck").map { |i| i.data }.to_a.must_equal ['norris', 'foo']
155
152
 
156
153
  end
157
154
 
@@ -166,7 +163,7 @@ describe MessageBus do
166
163
  before do
167
164
  seq = 0
168
165
  @bus.site_id_lookup do
169
- (seq+=1).to_s
166
+ (seq += 1).to_s
170
167
  end
171
168
  end
172
169
 
@@ -183,7 +180,7 @@ describe MessageBus do
183
180
  end
184
181
 
185
182
  @bus.publish("/global/test", "test")
186
- wait_for(1000){ data }
183
+ wait_for(1000) { data }
187
184
 
188
185
  data.must_equal "test"
189
186
  end
@@ -196,7 +193,7 @@ describe MessageBus do
196
193
  end
197
194
 
198
195
  @bus.publish("/global/test", "test")
199
- wait_for(1000){ data }
196
+ wait_for(1000) { data }
200
197
 
201
198
  data.must_equal "test"
202
199
  end
@@ -217,27 +214,33 @@ describe MessageBus do
217
214
 
218
215
  unless MESSAGE_BUS_CONFIG[:backend] == :memory
219
216
  it "should support forking properly do" do
220
- data = nil
217
+ data = []
221
218
  @bus.subscribe do |msg|
222
- data = msg.data
219
+ data << msg.data
223
220
  end
224
221
 
225
222
  @bus.publish("/hello", "world")
226
-
227
- wait_for(2000){ data }
223
+ wait_for(2000) { data.length > 0 }
228
224
 
229
225
  if child = Process.fork
230
- wait_for(2000) { data == "ready" }
226
+
227
+ wait_for(2000) { data.include?("ready") }
228
+ data.must_include "ready"
229
+
231
230
  @bus.publish("/hello", "world1")
232
- wait_for(2000) { data == "got it" }
233
- data.must_equal "got it"
231
+
232
+ wait_for(2000) { data.include?("got it") }
233
+ data.must_include "got it"
234
234
  Process.wait(child)
235
+
235
236
  else
236
237
  begin
237
238
  @bus.after_fork
238
239
  @bus.publish("/hello", "ready")
239
- wait_for(2000) { data == "world1" }
240
- if(data=="world1")
240
+
241
+ wait_for(2000) { data.include? "world1" }
242
+
243
+ if (data.include? "world1")
241
244
  @bus.publish("/hello", "got it")
242
245
  end
243
246
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: message_bus
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.5
4
+ version: 2.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Saffron
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-16 00:00:00.000000000 Z
11
+ date: 2017-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack