celluloid_pubsub 0.0.12 → 0.0.13

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e59fc206ccb8f5eb1a0420099429130b92944c17
4
- data.tar.gz: ccb987b8132c80967805995ea6b96e61c3b864f7
3
+ metadata.gz: ee4d93361a3109e5377aa2434b32c9efba276870
4
+ data.tar.gz: 164fad715b42c791eca6c531720829a7dbb7a40a
5
5
  SHA512:
6
- metadata.gz: 3570bd394410645d5ab706922d3e07c079d0cbd2f969db70c2cbc81fa6cda0c695daed1eba31212d4885927735706976e1a6d912979eca2afb3485d856a6dd74
7
- data.tar.gz: 54b8958031e1bd0db10c3aabe06785a6e5e025a3c9d7700a3170ddaa6bc703ddb045aec1b6f18ac290a0c0f9d371cc33b15a579573df2c7e4d3cd9dc2981ba57
6
+ metadata.gz: ede77bfac90ac7dfc6bcbf2d814ede297fb9238f62729e783c438912a787238929520e9bf90cdec93e1ef97232be0d62236115f84edb868ca8e0ea165b13d535
7
+ data.tar.gz: afa3750ce0f42c6407e6ac58043815c72ce6f695c69829d609315017ee65b11aa248a8c2d6cec1f6b09535f6e4d4251b2274292e7bc58583e1746c6317cedbfe
data/.gitignore CHANGED
@@ -17,3 +17,4 @@ Gemfile.lock
17
17
  .yardoc/
18
18
  doc/
19
19
  bin/
20
+ examples/log/**/*
data/Rakefile CHANGED
@@ -7,7 +7,7 @@ require 'yard-rspec'
7
7
  Coveralls::RakeTask.new
8
8
 
9
9
  RSpec::Core::RakeTask.new(:spec) do |spec|
10
- spec.rspec_opts = ['--backtrace '] if ENV['DEBUG']
10
+ spec.rspec_opts = ['--backtrace', '--fail-fast'] if ENV['DEBUG']
11
11
  end
12
12
 
13
13
  # desc "Prepare dummy application"
@@ -1,14 +1,25 @@
1
1
  require 'bundler/setup'
2
2
  require 'celluloid_pubsub'
3
+ require 'logger'
4
+
5
+ debug_enabled = ENV['DEBUG'].present? && ENV['DEBUG'].to_s == 'true'
6
+
7
+ if debug_enabled == true
8
+ log_file_path = File.join(File.expand_path(File.dirname(__FILE__)), 'log', 'celluloid_pubsub.log')
9
+ puts log_file_path
10
+ FileUtils.mkdir_p(File.dirname(log_file_path))
11
+ log_file = File.open(log_file_path, 'w')
12
+ log_file.sync = true
13
+ Celluloid.logger = ::Logger.new(log_file_path)
14
+ end
3
15
 
4
- ENV['DEBUG_CELLULOID'] = ARGV.map(&:downcase).include?('debug') ? 'true' : 'false'
5
16
  # actor that subscribes to a channel
6
17
  class Subscriber
7
18
  include Celluloid
8
19
  include Celluloid::Logger
9
20
 
10
- def initialize
11
- @client = CelluloidPubsub::Client.connect(actor: Actor.current) do |ws|
21
+ def initialize(options = {})
22
+ @client = CelluloidPubsub::Client.connect({ actor: Actor.current }.merge(options)) do |ws|
12
23
  ws.subscribe('test_channel') # this will execute after the connection is opened
13
24
  end
14
25
  end
@@ -19,7 +30,7 @@ class Subscriber
19
30
  @client.publish('test_channel2', 'data' => ' subscriber got successfull subscription') # the message needs to be a Hash
20
31
  else
21
32
  puts "subscriber got message #{message.inspect}"
22
- @client.publish('test_channel2', 'data' => "subscriber got #{message}") # the message needs to be a Hash
33
+ @client.unsubscribe('test_channel')
23
34
  end
24
35
  end
25
36
 
@@ -34,17 +45,16 @@ class Publisher
34
45
  include Celluloid
35
46
  include Celluloid::Logger
36
47
 
37
- def initialize
38
- @client = CelluloidPubsub::Client.connect(actor: Actor.current) do |ws|
48
+ def initialize(options = {})
49
+ @client = CelluloidPubsub::Client.connect({ actor: Actor.current }.merge(options)) do |ws|
39
50
  ws.subscribe('test_channel2') # this will execute after the connection is opened
40
51
  end
41
52
  @client.publish('test_channel', 'data' => 'my_message') # the message needs to be a Hash
42
- @client.publish('test_channel', 'data' => 'my_message')
43
- @client.publish('test_channel', 'data' => 'my_message')
44
53
  end
45
54
 
46
55
  def on_message(message)
47
56
  puts " publisher got #{message.inspect}"
57
+ @client.unsubscribe('test_channel2')
48
58
  end
49
59
 
50
60
  def on_close(code, reason)
@@ -53,7 +63,7 @@ class Publisher
53
63
  end
54
64
  end
55
65
 
56
- CelluloidPubsub::WebServer.supervise_as(:web_server)
57
- Subscriber.supervise_as(:subscriber)
58
- Publisher.supervise_as(:publisher)
66
+ CelluloidPubsub::WebServer.supervise_as(:web_server, enable_debug: debug_enabled)
67
+ Subscriber.supervise_as(:subscriber, enable_debug: debug_enabled)
68
+ Publisher.supervise_as(:publisher, enable_debug: debug_enabled)
59
69
  sleep
@@ -78,7 +78,7 @@ module CelluloidPubsub
78
78
  #
79
79
  # @api public
80
80
  def debug_enabled?
81
- CelluloidPubsub::WebServer.debug_enabled?
81
+ @options.fetch('enable_debug', false).to_s == 'true'
82
82
  end
83
83
 
84
84
  # subscribes to a channel . need to be used inside the connect block passed to the actor
@@ -119,6 +119,42 @@ module CelluloidPubsub
119
119
  async.chat(publishing_data)
120
120
  end
121
121
 
122
+ # unsubscribes current client from a channel
123
+ #
124
+ # @param [string] channel
125
+ #
126
+ # @return [void]
127
+ #
128
+ # @api public
129
+ def unsubscribe(channel)
130
+ publishing_data = { 'client_action' => 'unsubscribe', 'channel' => channel }
131
+ debug(" #{self.class} sends: #{publishing_data}") if debug_enabled?
132
+ async.chat(publishing_data)
133
+ end
134
+
135
+ # unsubscribes all clients subscribed to a channel
136
+ #
137
+ # @param [string] channel
138
+ #
139
+ # @return [void]
140
+ #
141
+ # @api public
142
+ def unsubscribe_clients(channel)
143
+ publishing_data = { 'client_action' => 'unsubscribe_clients', 'channel' => channel }
144
+ debug(" #{self.class} sends: #{publishing_data}") if debug_enabled?
145
+ async.chat(publishing_data)
146
+ end
147
+
148
+ # unsubscribes all clients from all channels
149
+ #
150
+ # @return [void]
151
+ #
152
+ # @api public
153
+ def unsubscribe_all
154
+ publishing_data = { 'client_action' => 'unsubscribe_all' }
155
+ debug(" #{self.class} sends: #{publishing_data}") if debug_enabled?
156
+ async.chat(publishing_data)
157
+ end
122
158
  # callback executes after connection is opened and delegates action to actor
123
159
  #
124
160
  # @return [void]
@@ -1,5 +1,6 @@
1
1
  require_relative './registry'
2
2
  module CelluloidPubsub
3
+ # rubocop:disable ClassLength
3
4
  # The reactor handles new connections. Based on what the client sends it either subscribes to a channel
4
5
  # or will publish to a channel or just dispatch to the server if command is neither subscribe, publish or unsubscribe
5
6
  #
@@ -44,10 +45,21 @@ module CelluloidPubsub
44
45
  #
45
46
  # :nocov:
46
47
  def run
47
- while message = @websocket.read
48
+ while !@websocket.closed? && message = try_read_websocket
48
49
  handle_websocket_message(message)
49
50
  end
50
51
  end
52
+
53
+ def try_read_websocket
54
+ message = nil
55
+ begin
56
+ message = @websocket.read
57
+ rescue => e
58
+ debug(e) if @server.debug_enabled?
59
+ end
60
+ message
61
+ end
62
+
51
63
  # :nocov:
52
64
 
53
65
  # method used to parse a JSON object into a Hash object
@@ -136,8 +148,10 @@ module CelluloidPubsub
136
148
  case json_data['client_action']
137
149
  when 'unsubscribe_all'
138
150
  unsubscribe_all
151
+ when 'unsubscribe_clients'
152
+ async.unsubscribe_clients(json_data['channel'])
139
153
  when 'unsubscribe'
140
- async.unsubscribe_client(json_data['channel'])
154
+ async.unsubscribe(json_data['channel'])
141
155
  when 'subscribe'
142
156
  async.start_subscriber(json_data['channel'], json_data)
143
157
  when 'publish'
@@ -168,7 +182,7 @@ module CelluloidPubsub
168
182
  # @return [void]
169
183
  #
170
184
  # @api public
171
- def unsubscribe_client(channel)
185
+ def unsubscribe(channel)
172
186
  return unless channel.present?
173
187
  @channels.delete(channel) unless @channels.blank?
174
188
  @websocket.close if @channels.blank?
@@ -177,6 +191,22 @@ module CelluloidPubsub
177
191
  end if @server.subscribers[channel].present?
178
192
  end
179
193
 
194
+ # the method will unsubscribe all clients subscribed to a channel by closing the
195
+ #
196
+ # @param [String] channel
197
+ #
198
+ # @return [void]
199
+ #
200
+ # @api public
201
+ def unsubscribe_clients(channel)
202
+ return if channel.blank? || @server.subscribers[channel].blank?
203
+ @server.subscribers[channel].each do |hash|
204
+ hash[:reactor].websocket.close
205
+ Celluloid::Actor.kill(hash[:reactor])
206
+ end
207
+ @server.subscribers[channel] = []
208
+ end
209
+
180
210
  # the method will terminate the current actor
181
211
  #
182
212
  #
@@ -230,6 +260,7 @@ module CelluloidPubsub
230
260
  CelluloidPubsub::Registry.channels.map do |channel|
231
261
  @server.subscribers[channel].each do |hash|
232
262
  hash[:reactor].websocket.close
263
+ Celluloid::Actor.kill(hash[:reactor])
233
264
  end
234
265
  @server.subscribers[channel] = []
235
266
  end
@@ -17,7 +17,7 @@ module CelluloidPubsub
17
17
  # minor release version
18
18
  MINOR = 0
19
19
  # tiny release version
20
- TINY = 12
20
+ TINY = 13
21
21
  # prelease version ( set this only if it is a prelease)
22
22
  PRE = nil
23
23
 
@@ -90,19 +90,10 @@ module CelluloidPubsub
90
90
  #
91
91
  # @api public
92
92
  def debug_enabled?
93
- self.class.debug_enabled?
93
+ @options.fetch('enable_debug', false).to_s == 'true'
94
94
  end
95
95
 
96
- # checks if debug is enabled
97
- #
98
- # @return [boolean]
99
- #
100
- # @api public
101
- def self.debug_enabled?
102
- ENV['DEBUG_CELLULOID'].present? && (ENV['DEBUG_CELLULOID'] == 'true' || ENV['DEBUG_CELLULOID'] == true)
103
- end
104
-
105
- # checks if debug is enabled
96
+ # method for publishing data to a channel
106
97
  #
107
98
  # @param [String] current_topic The Channel to which the reactor instance {CelluloidPubsub::Rector} will publish the message to
108
99
  # @param [Object] message
@@ -112,8 +103,12 @@ module CelluloidPubsub
112
103
  # @api public
113
104
  def publish_event(current_topic, message)
114
105
  return if current_topic.blank? || message.blank? || @subscribers[current_topic].blank?
115
- @subscribers[current_topic].each do |hash|
116
- hash[:reactor].websocket << message
106
+ begin
107
+ @subscribers[current_topic].each do |hash|
108
+ hash[:reactor].websocket << message
109
+ end
110
+ rescue => e
111
+ debug("could not publish message #{message} into topic #{current_topic} because of #{e.inspect}") if debug_enabled?
117
112
  end
118
113
  end
119
114
 
@@ -23,7 +23,7 @@ describe CelluloidPubsub::Client::PubSubWorker do
23
23
  before(:each) do
24
24
  Celluloid::WebSocket::Client.stubs(:new).returns(socket)
25
25
  socket.stubs(:text)
26
- @worker = CelluloidPubsub::Client::PubSubWorker.new({ 'actor' => actor }, &blk)
26
+ @worker = CelluloidPubsub::Client::PubSubWorker.new({ 'actor' => actor, enable_debug: true }, &blk)
27
27
  @worker.stubs(:client).returns(socket)
28
28
  @worker.stubs(:debug)
29
29
  @worker.stubs(:async).returns(@worker)
@@ -63,7 +63,6 @@ describe CelluloidPubsub::Client::PubSubWorker do
63
63
 
64
64
  describe '#debug_enabled?' do
65
65
  it 'checks if debug is enabled' do
66
- CelluloidPubsub::WebServer.expects(:debug_enabled?).returns(true)
67
66
  act = @worker.debug_enabled?
68
67
  act.should eq(true)
69
68
  end
@@ -1,214 +1,215 @@
1
- # encoding:utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe CelluloidPubsub::Reactor do
6
- let(:websocket) { mock }
7
- let(:server) { mock }
8
-
9
- before(:each) do
10
- subject.stubs(:async).returns(subject)
11
- server.stubs(:debug_enabled?).returns(false)
12
- server.stubs(:async).returns(server)
13
- server.stubs(:handle_dispatched_message)
14
- server.stubs(:subscribers).returns({})
15
- websocket.stubs(:read)
16
- subject.stubs(:inspect).returns(subject)
17
- subject.stubs(:run)
18
- subject.work(websocket, server)
19
- end
20
-
21
- describe '#work' do
22
- it 'works ' do
23
- subject.expects(:run)
24
- subject.work(websocket, server)
25
- subject.websocket.should eq websocket
26
- subject.server.should eq server
27
- subject.channels.should eq []
28
- end
29
- end
30
-
31
- # describe '#rub' do
32
- # let(:data) { 'some message' }
33
- #
34
- # it 'works ' do
35
- # subject.unstub(:run)
36
- # websocket.stubs(:read).returns(data)
37
- # subject.expects(:handle_websocket_message).with(data)
38
- # subject.run
39
- # end
40
- # end
41
-
42
- describe '#parse_json_data' do
43
- let(:data) { 'some message' }
44
- let(:expected) { data.to_json }
45
-
46
- it 'works with hash ' do
47
- JSON.expects(:parse).with(data).returns(expected)
48
- actual = subject.parse_json_data(data)
49
- actual.should eq expected
50
- end
51
-
52
- it 'works with exception parsing ' do
53
- JSON.expects(:parse).with(data).raises(StandardError)
54
- actual = subject.parse_json_data(data)
55
- actual.should eq data
56
- end
57
- end
58
-
59
- describe '#handle_websocket_message' do
60
- let(:data) { 'some message' }
61
- let(:json_data) { { a: 'b' } }
62
-
63
- it 'handle_websocket_message' do
64
- subject.expects(:parse_json_data).with(data).returns(json_data)
65
- subject.expects(:handle_parsed_websocket_message).with(json_data)
66
- subject.handle_websocket_message(data)
67
- end
68
- end
69
-
70
- describe '#handle_parsed_websocket_message' do
71
- it 'handle_websocket_message with a hash' do
72
- data = { 'client_action' => 'b' }
73
- data.expects(:stringify_keys).returns(data)
74
- subject.expects(:delegate_action).with(data)
75
- subject.handle_parsed_websocket_message(data)
76
- end
77
-
78
- it 'handle_websocket_message with something else than a hash' do
79
- data = 'some message'
80
- subject.expects(:handle_unknown_action).with(data)
81
- subject.handle_parsed_websocket_message(data)
82
- end
83
- end
84
-
85
- describe '#delegate_action' do
86
- it 'unsubscribes all' do
87
- data = { 'client_action' => 'unsubscribe_all' }
88
- subject.expects(:unsubscribe_all).returns('bla')
89
- subject.delegate_action(data)
90
- end
91
-
92
- it 'unsubscribes all' do
93
- data = { 'client_action' => 'unsubscribe', 'channel' => 'some channel' }
94
- subject.expects(:unsubscribe_client).with(data['channel'])
95
- subject.delegate_action(data)
96
- end
97
-
98
- it 'subscribes to channell' do
99
- data = { 'client_action' => 'subscribe', 'channel' => 'some channel' }
100
- subject.expects(:start_subscriber).with(data['channel'], data)
101
- subject.delegate_action(data)
102
- end
103
-
104
- it 'publish' do
105
- data = { 'client_action' => 'publish', 'channel' => 'some channel', 'data' => 'some data' }
106
- server.expects(:publish_event).with(data['channel'], data['data'].to_json)
107
- subject.delegate_action(data)
108
- end
109
-
110
- it 'handles unknown' do
111
- data = { 'client_action' => 'some action', 'channel' => 'some channel' }
112
- subject.expects(:handle_unknown_action).with(data)
113
- subject.delegate_action(data)
114
- end
115
- end
116
-
117
- describe '#handle_unknown_action' do
118
- it 'handles unknown' do
119
- data = 'some data'
120
- server.expects(:handle_dispatched_message)
121
- subject.handle_unknown_action(data)
122
- end
123
- end
124
-
125
- describe '#unsubscribe_client' do
126
- let(:channel) { 'some channel' }
127
- it 'returns nil' do
128
- act = subject.unsubscribe_client('')
129
- act.should eq(nil)
130
- end
131
-
132
- it 'unsubscribes' do
133
- subject.channels.stubs(:blank?).returns(false)
134
- subject.channels.expects(:delete).with(channel)
135
- act = subject.unsubscribe_client(channel)
136
- act.should eq(nil)
137
- end
138
-
139
- it 'unsubscribes' do
140
- subject.channels.stubs(:blank?).returns(true)
141
- subject.websocket.expects(:close)
142
- act = subject.unsubscribe_client(channel)
143
- act.should eq(nil)
144
- end
145
-
146
- it 'unsubscribes' do
147
- subject.channels.stubs(:blank?).returns(false)
148
- subject.channels.stubs(:delete)
149
- server.stubs(:subscribers).returns("#{channel}" => [{ reactor: subject }])
150
- subject.unsubscribe_client(channel)
151
- server.subscribers[channel].should eq([])
152
- end
153
- end
154
-
155
- describe '#shutdown' do
156
- it 'shutdowns' do
157
- subject.expects(:terminate)
158
- subject.shutdown
159
- end
160
- end
161
-
162
- describe '#start_subscriber' do
163
- let(:channel) { 'some channel' }
164
- let(:message) { { a: 'b' } }
165
-
166
- it 'subscribes ' do
167
- act = subject.start_subscriber('', message)
168
- act.should eq(nil)
169
- end
170
-
171
- it 'subscribes ' do
172
- subject.stubs(:add_subscriber_to_channel).with(channel, message)
173
- subject.websocket.expects(:<<).with(message.merge('client_action' => 'successful_subscription', 'channel' => channel).to_json)
174
- subject.start_subscriber(channel, message)
175
- end
176
-
177
- # it 'raises error' do
178
- # subject.stubs(:add_subscriber_to_channel).raises(StandardError)
179
- #
180
- # expect do
181
- # subject.start_subscriber(channel, message)
182
- # end.to raise_error(StandardError) { |e|
183
- # expect(e.message).to include(channel)
184
- # }
185
- # end
186
- end
187
-
188
- describe '#add_subscriber_to_channel' do
189
- let(:channel) { 'some channel' }
190
- let(:message) { { a: 'b' } }
191
-
192
- it 'adds subscribed' do
193
- CelluloidPubsub::Registry.channels.stubs(:include?).with(channel).returns(false)
194
- CelluloidPubsub::Registry.channels.expects(:<<).with(channel)
195
- server.subscribers[channel] = []
196
- server.subscribers[channel].expects(:<<).with(reactor: subject, message: message)
197
- subject.add_subscriber_to_channel(channel, message)
198
- subject.channels.should include(channel)
199
- end
200
- end
201
-
202
- describe '#unsubscribe_all' do
203
- let(:channel) { 'some channel' }
204
- let(:message) { { a: 'b' } }
205
-
206
- it 'adds subscribed' do
207
- CelluloidPubsub::Registry.stubs(:channels).returns([channel])
208
- server.subscribers.stubs(:[]).with(channel).returns([{ reactor: subject, message: message }])
209
- subject.websocket.expects(:close)
210
- subject.expects(:shutdown)
211
- subject.unsubscribe_all
212
- end
213
- end
214
- end
1
+ # encoding:utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe CelluloidPubsub::Reactor do
6
+ let(:websocket) { mock }
7
+ let(:server) { mock }
8
+
9
+ before(:each) do
10
+ subject.stubs(:async).returns(subject)
11
+ server.stubs(:debug_enabled?).returns(false)
12
+ server.stubs(:async).returns(server)
13
+ server.stubs(:handle_dispatched_message)
14
+ server.stubs(:subscribers).returns({})
15
+ websocket.stubs(:read)
16
+ subject.stubs(:inspect).returns(subject)
17
+ subject.stubs(:run)
18
+ subject.work(websocket, server)
19
+ end
20
+
21
+ describe '#work' do
22
+ it 'works ' do
23
+ subject.expects(:run)
24
+ subject.work(websocket, server)
25
+ subject.websocket.should eq websocket
26
+ subject.server.should eq server
27
+ subject.channels.should eq []
28
+ end
29
+ end
30
+
31
+ # describe '#rub' do
32
+ # let(:data) { 'some message' }
33
+ #
34
+ # it 'works ' do
35
+ # subject.unstub(:run)
36
+ # websocket.stubs(:read).returns(data)
37
+ # subject.expects(:handle_websocket_message).with(data)
38
+ # subject.run
39
+ # end
40
+ # end
41
+
42
+ describe '#parse_json_data' do
43
+ let(:data) { 'some message' }
44
+ let(:expected) { data.to_json }
45
+
46
+ it 'works with hash ' do
47
+ JSON.expects(:parse).with(data).returns(expected)
48
+ actual = subject.parse_json_data(data)
49
+ actual.should eq expected
50
+ end
51
+
52
+ it 'works with exception parsing ' do
53
+ JSON.expects(:parse).with(data).raises(StandardError)
54
+ actual = subject.parse_json_data(data)
55
+ actual.should eq data
56
+ end
57
+ end
58
+
59
+ describe '#handle_websocket_message' do
60
+ let(:data) { 'some message' }
61
+ let(:json_data) { { a: 'b' } }
62
+
63
+ it 'handle_websocket_message' do
64
+ subject.expects(:parse_json_data).with(data).returns(json_data)
65
+ subject.expects(:handle_parsed_websocket_message).with(json_data)
66
+ subject.handle_websocket_message(data)
67
+ end
68
+ end
69
+
70
+ describe '#handle_parsed_websocket_message' do
71
+ it 'handle_websocket_message with a hash' do
72
+ data = { 'client_action' => 'b' }
73
+ data.expects(:stringify_keys).returns(data)
74
+ subject.expects(:delegate_action).with(data)
75
+ subject.handle_parsed_websocket_message(data)
76
+ end
77
+
78
+ it 'handle_websocket_message with something else than a hash' do
79
+ data = 'some message'
80
+ subject.expects(:handle_unknown_action).with(data)
81
+ subject.handle_parsed_websocket_message(data)
82
+ end
83
+ end
84
+
85
+ describe '#delegate_action' do
86
+ it 'unsubscribes all' do
87
+ data = { 'client_action' => 'unsubscribe_all' }
88
+ subject.expects(:unsubscribe_all).returns('bla')
89
+ subject.delegate_action(data)
90
+ end
91
+
92
+ it 'unsubscribes all' do
93
+ data = { 'client_action' => 'unsubscribe', 'channel' => 'some channel' }
94
+ subject.expects(:unsubscribe).with(data['channel'])
95
+ subject.delegate_action(data)
96
+ end
97
+
98
+ it 'subscribes to channell' do
99
+ data = { 'client_action' => 'subscribe', 'channel' => 'some channel' }
100
+ subject.expects(:start_subscriber).with(data['channel'], data)
101
+ subject.delegate_action(data)
102
+ end
103
+
104
+ it 'publish' do
105
+ data = { 'client_action' => 'publish', 'channel' => 'some channel', 'data' => 'some data' }
106
+ server.expects(:publish_event).with(data['channel'], data['data'].to_json)
107
+ subject.delegate_action(data)
108
+ end
109
+
110
+ it 'handles unknown' do
111
+ data = { 'client_action' => 'some action', 'channel' => 'some channel' }
112
+ subject.expects(:handle_unknown_action).with(data)
113
+ subject.delegate_action(data)
114
+ end
115
+ end
116
+
117
+ describe '#handle_unknown_action' do
118
+ it 'handles unknown' do
119
+ data = 'some data'
120
+ server.expects(:handle_dispatched_message)
121
+ subject.handle_unknown_action(data)
122
+ end
123
+ end
124
+
125
+ describe '#unsubscribe_client' do
126
+ let(:channel) { 'some channel' }
127
+ it 'returns nil' do
128
+ act = subject.unsubscribe('')
129
+ act.should eq(nil)
130
+ end
131
+
132
+ it 'unsubscribes' do
133
+ subject.channels.stubs(:blank?).returns(false)
134
+ subject.channels.expects(:delete).with(channel)
135
+ act = subject.unsubscribe(channel)
136
+ act.should eq(nil)
137
+ end
138
+
139
+ it 'unsubscribes' do
140
+ subject.channels.stubs(:blank?).returns(true)
141
+ subject.websocket.expects(:close)
142
+ act = subject.unsubscribe(channel)
143
+ act.should eq(nil)
144
+ end
145
+
146
+ it 'unsubscribes' do
147
+ subject.channels.stubs(:blank?).returns(false)
148
+ subject.channels.stubs(:delete)
149
+ server.stubs(:subscribers).returns("#{channel}" => [{ reactor: subject }])
150
+ subject.unsubscribe(channel)
151
+ server.subscribers[channel].should eq([])
152
+ end
153
+ end
154
+
155
+ describe '#shutdown' do
156
+ it 'shutdowns' do
157
+ subject.expects(:terminate)
158
+ subject.shutdown
159
+ end
160
+ end
161
+
162
+ describe '#start_subscriber' do
163
+ let(:channel) { 'some channel' }
164
+ let(:message) { { a: 'b' } }
165
+
166
+ it 'subscribes ' do
167
+ act = subject.start_subscriber('', message)
168
+ act.should eq(nil)
169
+ end
170
+
171
+ it 'subscribes ' do
172
+ subject.stubs(:add_subscriber_to_channel).with(channel, message)
173
+ subject.websocket.expects(:<<).with(message.merge('client_action' => 'successful_subscription', 'channel' => channel).to_json)
174
+ subject.start_subscriber(channel, message)
175
+ end
176
+
177
+ # it 'raises error' do
178
+ # subject.stubs(:add_subscriber_to_channel).raises(StandardError)
179
+ #
180
+ # expect do
181
+ # subject.start_subscriber(channel, message)
182
+ # end.to raise_error(StandardError) { |e|
183
+ # expect(e.message).to include(channel)
184
+ # }
185
+ # end
186
+ end
187
+
188
+ describe '#add_subscriber_to_channel' do
189
+ let(:channel) { 'some channel' }
190
+ let(:message) { { a: 'b' } }
191
+
192
+ it 'adds subscribed' do
193
+ CelluloidPubsub::Registry.channels.stubs(:include?).with(channel).returns(false)
194
+ CelluloidPubsub::Registry.channels.expects(:<<).with(channel)
195
+ server.subscribers[channel] = []
196
+ server.subscribers[channel].expects(:<<).with(reactor: subject, message: message)
197
+ subject.add_subscriber_to_channel(channel, message)
198
+ subject.channels.should include(channel)
199
+ end
200
+ end
201
+
202
+ describe '#unsubscribe_all' do
203
+ let(:channel) { 'some channel' }
204
+ let(:message) { { a: 'b' } }
205
+
206
+ it 'adds subscribed' do
207
+ Celluloid::Actor.stubs(:kill).returns(true)
208
+ CelluloidPubsub::Registry.stubs(:channels).returns([channel])
209
+ server.subscribers.stubs(:[]).with(channel).returns([{ reactor: subject, message: message }])
210
+ subject.websocket.expects(:close)
211
+ subject.expects(:shutdown)
212
+ subject.unsubscribe_all
213
+ end
214
+ end
215
+ end
@@ -1,49 +1,49 @@
1
- # encoding:utf-8
1
+ # encoding:utf-8
2
2
 
3
- require 'spec_helper'
3
+ require 'spec_helper'
4
4
 
5
- describe CelluloidPubsub::WebServer do
6
- it 'should have host constant' do
7
- CelluloidPubsub::WebServer::HOST.should eq('0.0.0.0')
8
- end
5
+ describe CelluloidPubsub::WebServer do
6
+ it 'should have host constant' do
7
+ CelluloidPubsub::WebServer::HOST.should eq('0.0.0.0')
8
+ end
9
9
 
10
- it 'should have host constant' do
11
- CelluloidPubsub::WebServer::PORT.should eq(1234)
12
- end
10
+ it 'should have host constant' do
11
+ CelluloidPubsub::WebServer::PORT.should eq(1234)
12
+ end
13
13
 
14
- it 'should have host constant' do
15
- CelluloidPubsub::WebServer::PATH.should eq('/ws')
16
- end
17
- let(:options) { {} }
18
- let(:web_server) { mock }
14
+ it 'should have host constant' do
15
+ CelluloidPubsub::WebServer::PATH.should eq('/ws')
16
+ end
17
+ let(:options) { {} }
18
+ let(:web_server) { mock }
19
19
 
20
- before(:each) do
21
- CelluloidPubsub::WebServer.stubs(:new).returns(web_server)
22
- end
20
+ before(:each) do
21
+ CelluloidPubsub::WebServer.stubs(:new).returns(web_server)
22
+ end
23
23
 
24
- # it '#initialize with default values ' do
25
- # web_server.parse_options({})
26
- # web_server.hostname.should eq(CelluloidPubsub::WebServer::HOST)
27
- # web_server.port.should eq(CelluloidPubsub::WebServer::PORT)
28
- # web_server.path.should eq(CelluloidPubsub::WebServer::PATH)
29
- # web_server.backlog.should eq(1024)
30
- # web_server.spy.should eq(false)
31
- # end
32
- #
33
- # describe '#with custom values' do
34
- # let(:hostname) { '192.0.0.1' }
35
- # let(:port) { 13_456 }
36
- # let(:path) { '/pathy' }
37
- # let(:backlog) { 2048 }
38
- # let(:spy) { true }
39
- #
40
- # it '#initialize with custom values ' do
41
- # web_server.parse_options(hostname: hostname, port: port, path: path, spy: spy, backlog: backlog)
42
- # web_server.hostname.should eq(hostname)
43
- # web_server.port.should eq(port)
44
- # web_server.path.should eq(path)
45
- # web_server.backlog.should eq(backlog)
46
- # web_server.spy.should eq(spy)
47
- # end
48
- # end
49
- end
24
+ # it '#initialize with default values ' do
25
+ # web_server.parse_options({})
26
+ # web_server.hostname.should eq(CelluloidPubsub::WebServer::HOST)
27
+ # web_server.port.should eq(CelluloidPubsub::WebServer::PORT)
28
+ # web_server.path.should eq(CelluloidPubsub::WebServer::PATH)
29
+ # web_server.backlog.should eq(1024)
30
+ # web_server.spy.should eq(false)
31
+ # end
32
+ #
33
+ # describe '#with custom values' do
34
+ # let(:hostname) { '192.0.0.1' }
35
+ # let(:port) { 13_456 }
36
+ # let(:path) { '/pathy' }
37
+ # let(:backlog) { 2048 }
38
+ # let(:spy) { true }
39
+ #
40
+ # it '#initialize with custom values ' do
41
+ # web_server.parse_options(hostname: hostname, port: port, path: path, spy: spy, backlog: backlog)
42
+ # web_server.hostname.should eq(hostname)
43
+ # web_server.port.should eq(port)
44
+ # web_server.path.should eq(path)
45
+ # web_server.backlog.should eq(backlog)
46
+ # web_server.spy.should eq(spy)
47
+ # end
48
+ # end
49
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: celluloid_pubsub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - bogdanRada
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-15 00:00:00.000000000 Z
11
+ date: 2015-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: celluloid