klomp 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,12 @@
1
1
  Klomp Changes
2
2
  --------------------------------------------------------------------------------
3
3
 
4
+ 1.0.1 (2012/10/5)
5
+ ================================================================================
6
+
7
+ - Make publish, subscribe, and unsubscribe methods behave like Klomp 0.0.x where
8
+ the return value is the frame object(s) that was created by the operation.
9
+
4
10
  1.0.0 (2012/10/3)
5
11
  ================================================================================
6
12
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "klomp"
5
- s.version = "1.0.0"
5
+ s.version = "1.0.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Nick Sieger"]
@@ -1,5 +1,5 @@
1
1
  class Klomp
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
 
4
4
  class Error < StandardError; end
5
5
 
@@ -24,11 +24,17 @@ class Klomp
24
24
  end
25
25
 
26
26
  def subscribe(queue, subscriber = nil, &block)
27
- connections.each {|conn| conn.subscribe(queue, subscriber, &block) }
27
+ connections.map {|conn| conn.subscribe(queue, subscriber, &block) }
28
28
  end
29
29
 
30
30
  def unsubscribe(queue)
31
- connections.each {|conn| conn.unsubscribe(queue) rescue nil }
31
+ if Array === queue
32
+ raise ArgumentError,
33
+ "wrong size array for #{connections.size} (#{queue.size})" unless connections.size == queue.size
34
+ connections.zip(queue).map {|conn,arg| conn.unsubscribe arg rescue nil }
35
+ else
36
+ connections.map {|conn| conn.unsubscribe(queue) rescue nil }
37
+ end
32
38
  end
33
39
 
34
40
  def connected?
@@ -36,8 +42,9 @@ class Klomp
36
42
  end
37
43
 
38
44
  def disconnect
39
- connections.each {|conn| conn.disconnect }
40
- @connections = []
45
+ connections.map {|conn| conn.disconnect }.tap do
46
+ @connections = []
47
+ end
41
48
  end
42
49
  end
43
50
 
@@ -40,12 +40,18 @@ class Klomp
40
40
  raise Klomp::Error, "subscriber does not respond to #call" if subscriber && !subscriber.respond_to?(:call)
41
41
  previous = subscriptions[queue]
42
42
  subscriptions[queue] = subscriber || block
43
- write Frames::Subscribe.new(queue) unless previous
43
+ frame = Frames::Subscribe.new(queue)
44
+ if previous
45
+ frame.previous_subscriber = previous
46
+ else
47
+ write frame
48
+ end
44
49
  start_subscriber_thread
45
- previous
50
+ frame
46
51
  end
47
52
 
48
53
  def unsubscribe(queue)
54
+ queue = queue.headers['destination'] if Frames::Subscribe === queue
49
55
  write Frames::Unsubscribe.new(queue) if subscriptions.delete queue
50
56
  end
51
57
 
@@ -55,9 +61,11 @@ class Klomp
55
61
  def disconnect
56
62
  close!
57
63
  stop_subscriber_thread
58
- write Frames::Disconnect.new rescue nil
64
+ frame = Frames::Disconnect.new
65
+ write frame rescue nil
59
66
  @socket.close rescue nil
60
67
  @socket = nil
68
+ frame
61
69
  end
62
70
 
63
71
  def reconnect
@@ -88,6 +96,7 @@ class Klomp
88
96
 
89
97
  @socket.write frame.to_s
90
98
  log_frame frame if logger
99
+ frame
91
100
  rescue Error
92
101
  raise
93
102
  rescue
@@ -89,6 +89,7 @@ class Klomp
89
89
  end
90
90
 
91
91
  class Subscribe < Frame
92
+ attr_accessor :previous_subscriber
92
93
  def initialize(queue)
93
94
  headers['id'] = queue
94
95
  headers['destination'] = queue
@@ -69,10 +69,12 @@ describe Klomp::Connection do
69
69
 
70
70
  Given(:connection) { Klomp::Connection.new server, options }
71
71
 
72
- When { connection.publish "/queue/greeting", "hello" }
72
+ When(:result) { connection.publish "/queue/greeting", "hello" }
73
73
 
74
74
  Then { socket.should have_received(:write).with(frame(:greeting)) }
75
75
 
76
+ Then { result.should be_instance_of(Klomp::Frames::Send) }
77
+
76
78
  context "logs when logger level is debug" do
77
79
 
78
80
  Given(:logger) { double("Logger").as_null_object.tap {|l| l.stub!(debug?: true) } }
@@ -89,10 +91,12 @@ describe Klomp::Connection do
89
91
 
90
92
  context "writes the subscribe message" do
91
93
 
92
- When { connection.subscribe "/queue/greeting", subscriber }
94
+ When(:result) { connection.subscribe "/queue/greeting", subscriber }
93
95
 
94
96
  Then { socket.should have_received(:write).with(frame(:subscribe)) }
95
97
 
98
+ Then { result.should be_instance_of(Klomp::Frames::Subscribe) }
99
+
96
100
  context "and logs when logger level is debug" do
97
101
 
98
102
  Given(:logger) { double("Logger").as_null_object.tap {|l| l.stub!(debug?: true) } }
@@ -201,26 +205,38 @@ describe Klomp::Connection do
201
205
  context "unsubscribe" do
202
206
 
203
207
  Given(:connection) { Klomp::Connection.new server, options }
208
+ Given(:arg) { "/queue/greeting" }
204
209
 
205
210
  Given { connection.subscriptions["/queue/greeting"] = double "subscribers" }
206
211
 
207
- When { connection.unsubscribe "/queue/greeting" }
212
+ When(:result) { connection.unsubscribe arg }
208
213
 
209
214
  Then { socket.should have_received(:write).with(frame(:unsubscribe)) }
210
215
 
216
+ Then { result.should be_instance_of(Klomp::Frames::Unsubscribe) }
217
+
218
+ context "can accept Subscribe frame" do
219
+
220
+ Given(:arg) { Klomp::Frames::Subscribe.new "/queue/greeting" }
221
+
222
+ Then { socket.should have_received(:write).with(frame(:unsubscribe)) }
223
+
224
+ end
211
225
  end
212
226
 
213
227
  context "disconnect" do
214
228
 
215
229
  Given!(:connection) { Klomp::Connection.new server, options }
216
230
 
217
- When { connection.disconnect }
231
+ When(:result) { connection.disconnect }
218
232
 
219
233
  Then do
220
234
  socket.should have_received(:write).with(frame(:disconnect))
221
235
  socket.should have_received(:close)
222
236
  end
223
237
 
238
+ Then { result.should be_instance_of(Klomp::Frames::Disconnect) }
239
+
224
240
  context "makes connection useless (raises error)" do
225
241
 
226
242
  Then { expect { connection.publish "/queue/greeting", "hello" }.to raise_error(Klomp::Error) }
@@ -43,22 +43,26 @@ describe Klomp do
43
43
 
44
44
  context "publish" do
45
45
 
46
+ Given(:frame) { double "send frame" }
47
+
46
48
  context "calls publish on one of the connections" do
47
49
 
48
50
  Given do
49
51
  connections.values.each do |conn|
50
52
  conn.stub!(:connected? => false)
51
- conn.stub!(:publish).and_return { conn.stub!(:connected? => true) }
53
+ conn.stub!(:publish).and_return { conn.stub!(:connected? => true); frame }
52
54
  end
53
55
  end
54
56
 
55
- When { klomp.publish "/queue/greeting", "hello" }
57
+ When(:result) { klomp.publish "/queue/greeting", "hello" }
56
58
 
57
59
  Then { connections.values.select {|conn| conn.connected? }.length.should == 1 }
58
60
 
59
61
  Then { connections.values.detect {|conn| conn.connected? }.
60
62
  should have_received(:publish).with("/queue/greeting", "hello", {}) }
61
63
 
64
+ Then { result.should == frame }
65
+
62
66
  end
63
67
 
64
68
  context "calls publish on another connection if the first one fails" do
@@ -70,6 +74,7 @@ describe Klomp do
70
74
  conn.stub!(:publish).and_return do
71
75
  if first_exception
72
76
  conn.stub!(:connected? => true)
77
+ frame
73
78
  else
74
79
  first_exception = true
75
80
  raise Klomp::Error.new
@@ -78,13 +83,15 @@ describe Klomp do
78
83
  end
79
84
  end
80
85
 
81
- When { klomp.publish "/queue/greeting", "hello" }
86
+ When(:result) { klomp.publish "/queue/greeting", "hello" }
82
87
 
83
88
  Then { connections.values.select {|conn| conn.connected? }.length.should == 1 }
84
89
 
85
90
  Then { connections.values.detect {|conn| conn.connected? }.
86
91
  should have_received(:publish).with("/queue/greeting", "hello", {}) }
87
92
 
93
+ Then { result.should == frame }
94
+
88
95
  end
89
96
 
90
97
  context "raises an exception if all connections failed" do
@@ -106,12 +113,18 @@ describe Klomp do
106
113
 
107
114
  context "calls subscribe on all of the servers" do
108
115
 
109
- Given { connections.values.each {|conn| conn.stub!(:subscribe) } }
116
+ Given { connections.values.each {|conn| conn.stub!(subscribe: 42) } }
110
117
 
111
- When { klomp.subscribe("/queue/greeting") { true } }
118
+ When(:result) { klomp.subscribe("/queue/greeting") { true } }
112
119
 
113
120
  Then { connections.values.each {|conn| conn.should have_received(:subscribe).with("/queue/greeting", nil) } }
114
121
 
122
+ context "and returns the results of all Connection#subscribe as an array" do
123
+
124
+ Then { result.should == Array.new(connections.size, 42) }
125
+
126
+ end
127
+
115
128
  end
116
129
 
117
130
  context "fails if any subscribe calls fail" do
@@ -130,12 +143,18 @@ describe Klomp do
130
143
 
131
144
  context "calls unsubscribe on all the servers" do
132
145
 
133
- Given { connections.values.each {|conn| conn.stub!(:unsubscribe) } }
146
+ Given { connections.values.each {|conn| conn.stub!(unsubscribe: 42) } }
134
147
 
135
- When { klomp.unsubscribe("/queue/greeting") }
148
+ When(:result) { klomp.unsubscribe("/queue/greeting") }
136
149
 
137
150
  Then { connections.values.each {|conn| conn.should have_received(:unsubscribe).with("/queue/greeting") } }
138
151
 
152
+ context "and returns the results of all Connection#unsubscribe as an array" do
153
+
154
+ Then { result.should == Array.new(connections.size, 42) }
155
+
156
+ end
157
+
139
158
  end
140
159
 
141
160
  context "calls unsubscribe on all the servers even if the unsubscribe errs" do
@@ -148,18 +167,48 @@ describe Klomp do
148
167
 
149
168
  end
150
169
 
170
+ context "with an array" do
171
+
172
+ context "of length matching the number of connections" do
173
+
174
+ Given(:arg) { Array.new(connections.size, 42) }
175
+
176
+ Given { connections.values.each {|conn| conn.stub!(:unsubscribe) } }
177
+
178
+ When { klomp.unsubscribe arg }
179
+
180
+ Then { connections.values.each {|conn| conn.should have_received(:unsubscribe).with(42) } }
181
+
182
+ end
183
+
184
+ context "of mismatched length" do
185
+
186
+ When(:expect_unsubscribe) { expect { klomp.unsubscribe([]) } }
187
+
188
+ Then { expect_unsubscribe.to raise_error(ArgumentError) }
189
+
190
+ end
191
+
192
+ end
193
+
151
194
  end
152
195
 
153
196
  context "disconnect" do
154
197
 
155
198
  context "disconnects all the servers" do
156
199
 
157
- Given { connections.values.each {|conn| conn.stub!(:disconnect) } }
200
+ Given { connections.values.each {|conn| conn.stub!(disconnect: 42) } }
158
201
 
159
- When { klomp.disconnect }
202
+ When(:result) { klomp.disconnect }
160
203
 
161
204
  Then { connections.values.each {|conn| conn.should have_received(:disconnect) } }
162
205
 
206
+ context "and returns the results of all Connection#disconnect as an array" do
207
+
208
+ Then { result.should == Array.new(connections.size, 42) }
209
+
210
+ end
211
+
163
212
  end
164
213
 
165
214
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: klomp
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-03 00:00:00.000000000 Z
12
+ date: 2012-10-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rdoc
@@ -224,7 +224,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
224
224
  version: '0'
225
225
  segments:
226
226
  - 0
227
- hash: -1626497956462206890
227
+ hash: -2110109472818919447
228
228
  required_rubygems_version: !ruby/object:Gem::Requirement
229
229
  none: false
230
230
  requirements: