message_bus 0.9.5 → 0.9.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: f276c95da4b72d4c4ed5710fd1af3883116ee77a
4
- data.tar.gz: ace4ed261e91e1efa464c4a30eec2ffed0a05291
3
+ metadata.gz: f439c4637f8f2b6271244a4ed2e3ab2637ff6c79
4
+ data.tar.gz: a35a858e95c8b3b218ba29e0773296420bf9f69b
5
5
  SHA512:
6
- metadata.gz: fe593ca357284143f1ab125a4dea02eb8060414830f8b613e2cabca8bbbdfb4746c9a9df83529d318bf233b00bc84d297d191e0185dbd4782d1cf7388e40cf8b
7
- data.tar.gz: 0af4fe02cd3479d72285b6e3b7d6f937279c6defc02a4bcf0603a2e832ee240da78e12c4d8ccf08fa5ff54a7e0799dda5d6a72ea9a18829e4ca82b8e6611016f
6
+ metadata.gz: 9ad5513fdaaadb637035aab15cc4d8edf76e5c8bee0774cdf90a90e3f5eb725abb987de5a4072213ba447b77f600113755330772d7169c75104e9bba816a8f6e
7
+ data.tar.gz: 72e05629c7e15d6d19cd3bf78238a3def6cb96007668ca4031e930f0b6fbb66f19524c55028ce9fdbc16aa9a3bb8e47ded98434e70ba60445731e5e599fe82b1
@@ -1,6 +1,29 @@
1
1
  require 'json' unless defined? ::JSON
2
2
 
3
3
  class MessageBus::ConnectionManager
4
+ require 'monitor'
5
+
6
+ class SynchronizedSet
7
+ include MonitorMixin
8
+
9
+ def initialize
10
+ super
11
+ @set = Set.new
12
+ end
13
+
14
+ def self.synchronize(methods)
15
+ methods.each do |method|
16
+ define_method method do |*args, &blk|
17
+ synchronize do
18
+ @set.send method,*args,&blk
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ synchronize(Set.new.methods - Object.new.methods)
25
+
26
+ end
4
27
 
5
28
  def initialize(bus = nil)
6
29
  @clients = {}
@@ -75,7 +98,7 @@ class MessageBus::ConnectionManager
75
98
  def subscribe_client(client,channel)
76
99
  set = @subscriptions[client.site_id][channel]
77
100
  unless set
78
- set = Set.new
101
+ set = SynchronizedSet.new
79
102
  @subscriptions[client.site_id][channel] = set
80
103
  end
81
104
  set << client.client_id
@@ -1,3 +1,3 @@
1
1
  module MessageBus
2
- VERSION = "0.9.5"
2
+ VERSION = "0.9.6"
3
3
  end
@@ -31,18 +31,18 @@ describe MessageBus::Client do
31
31
 
32
32
  it "denies users that are not members of group" do
33
33
  @client.group_ids = [77,0,10]
34
- @client.allowed?(@message).should be_false
34
+ @client.allowed?(@message).should == false
35
35
  end
36
36
 
37
37
  it "allows users that are members of group" do
38
38
  @client.group_ids = [1,2,3]
39
- @client.allowed?(@message).should be_true
39
+ @client.allowed?(@message).should == true
40
40
  end
41
41
 
42
42
  it "allows all users if groups not set" do
43
43
  @message.group_ids = nil
44
44
  @client.group_ids = [77,0,10]
45
- @client.allowed?(@message).should be_true
45
+ @client.allowed?(@message).should == true
46
46
  end
47
47
  end
48
48
  end
@@ -6,6 +6,7 @@ class FakeAsync
6
6
  attr_accessor :cleanup_timer
7
7
 
8
8
  def <<(val)
9
+ sleep 0.01 # simulate IO
9
10
  @sent ||= ""
10
11
  @sent << val
11
12
  end
@@ -79,6 +80,39 @@ describe MessageBus::ConnectionManager do
79
80
  @manager.notify_clients(m)
80
81
  @resp.sent.should == nil
81
82
  end
83
+ end
84
+
85
+ describe MessageBus::ConnectionManager, "notifying and subscribing concurrently" do
86
+
87
+ it "is thread-safe" do
88
+ @bus = MessageBus
89
+ @manager = MessageBus::ConnectionManager.new(@bus)
82
90
 
91
+ client_threads = 10.times.map do |id|
92
+ Thread.new do
93
+ expect {
94
+ @client = MessageBus::Client.new(client_id: "xyz_#{id}", site_id: 10)
95
+ @resp = FakeAsync.new
96
+ @client.async_response = @resp
97
+ @client.subscribe("test", -1)
98
+ @manager.add_client(@client)
99
+ @client.cleanup_timer = FakeTimer.new
100
+ }.not_to raise_error
101
+ end
102
+ end
103
+
104
+ subscriber_threads = 10.times.map do |id|
105
+ Thread.new do
106
+ expect {
107
+ m = MessageBus::Message.new(1,id,"test","data_#{id}")
108
+ m.site_id = 10
109
+ @manager.notify_clients(m)
110
+ }.not_to raise_error
111
+ end
112
+ end
113
+
114
+ client_threads.each(&:join)
115
+ subscriber_threads.each(&:join)
116
+ end
83
117
 
84
118
  end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,15 @@
1
1
  require 'thin'
2
2
  require 'lib/fake_async_middleware'
3
3
 
4
+ RSpec.configure do |config|
5
+ config.expect_with :rspec do |c|
6
+ c.syntax = [:should, :expect]
7
+ end
8
+ config.mock_with :rspec do |mocks|
9
+ mocks.syntax = :should
10
+ end
11
+ end
12
+
4
13
  def wait_for(timeout_milliseconds)
5
14
  timeout = (timeout_milliseconds + 0.0) / 1000
6
15
  finish = Time.now + timeout
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: 0.9.5
4
+ version: 0.9.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: 2014-08-11 00:00:00.000000000 Z
11
+ date: 2014-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack