sparqcode_bunny 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. data/.gitignore +10 -0
  2. data/.rspec +3 -0
  3. data/.travis.yml +15 -0
  4. data/.yardopts +9 -0
  5. data/CHANGELOG +27 -0
  6. data/Gemfile +39 -0
  7. data/LICENSE +21 -0
  8. data/README.textile +82 -0
  9. data/Rakefile +14 -0
  10. data/bunny.gemspec +43 -0
  11. data/examples/simple_08.rb +32 -0
  12. data/examples/simple_09.rb +32 -0
  13. data/examples/simple_ack_08.rb +35 -0
  14. data/examples/simple_ack_09.rb +35 -0
  15. data/examples/simple_consumer_08.rb +55 -0
  16. data/examples/simple_consumer_09.rb +55 -0
  17. data/examples/simple_fanout_08.rb +41 -0
  18. data/examples/simple_fanout_09.rb +41 -0
  19. data/examples/simple_headers_08.rb +42 -0
  20. data/examples/simple_headers_09.rb +42 -0
  21. data/examples/simple_publisher_08.rb +29 -0
  22. data/examples/simple_publisher_09.rb +29 -0
  23. data/examples/simple_topic_08.rb +61 -0
  24. data/examples/simple_topic_09.rb +61 -0
  25. data/ext/amqp-0.8.json +616 -0
  26. data/ext/amqp-0.9.1.json +388 -0
  27. data/ext/config.yml +4 -0
  28. data/ext/qparser.rb +469 -0
  29. data/lib/bunny/channel08.rb +39 -0
  30. data/lib/bunny/channel09.rb +39 -0
  31. data/lib/bunny/client08.rb +472 -0
  32. data/lib/bunny/client09.rb +374 -0
  33. data/lib/bunny/consumer.rb +35 -0
  34. data/lib/bunny/exchange08.rb +171 -0
  35. data/lib/bunny/exchange09.rb +159 -0
  36. data/lib/bunny/queue08.rb +403 -0
  37. data/lib/bunny/queue09.rb +325 -0
  38. data/lib/bunny/subscription08.rb +87 -0
  39. data/lib/bunny/subscription09.rb +87 -0
  40. data/lib/bunny/system_timer.rb +14 -0
  41. data/lib/bunny/version.rb +5 -0
  42. data/lib/bunny.rb +109 -0
  43. data/lib/qrack/amq-client-url.rb +165 -0
  44. data/lib/qrack/channel.rb +20 -0
  45. data/lib/qrack/client.rb +235 -0
  46. data/lib/qrack/errors.rb +5 -0
  47. data/lib/qrack/protocol/protocol08.rb +134 -0
  48. data/lib/qrack/protocol/protocol09.rb +135 -0
  49. data/lib/qrack/protocol/spec08.rb +828 -0
  50. data/lib/qrack/protocol/spec09.rb +524 -0
  51. data/lib/qrack/qrack08.rb +20 -0
  52. data/lib/qrack/qrack09.rb +20 -0
  53. data/lib/qrack/queue.rb +40 -0
  54. data/lib/qrack/subscription.rb +112 -0
  55. data/lib/qrack/transport/buffer08.rb +278 -0
  56. data/lib/qrack/transport/buffer09.rb +280 -0
  57. data/lib/qrack/transport/frame08.rb +117 -0
  58. data/lib/qrack/transport/frame09.rb +97 -0
  59. data/spec/spec_08/bunny_spec.rb +77 -0
  60. data/spec/spec_08/connection_spec.rb +25 -0
  61. data/spec/spec_08/exchange_spec.rb +173 -0
  62. data/spec/spec_08/queue_spec.rb +235 -0
  63. data/spec/spec_09/amqp_url_spec.rb +19 -0
  64. data/spec/spec_09/bunny_spec.rb +76 -0
  65. data/spec/spec_09/connection_spec.rb +29 -0
  66. data/spec/spec_09/exchange_spec.rb +173 -0
  67. data/spec/spec_09/queue_spec.rb +248 -0
  68. metadata +151 -0
@@ -0,0 +1,97 @@
1
+
2
+ # encoding: utf-8
3
+
4
+
5
+ #:stopdoc:
6
+ # this file was autogenerated on 2011-07-21 07:07:06 +0100
7
+ #
8
+ # DO NOT EDIT! (edit ext/qparser.rb and config.yml instead, and run 'ruby qparser.rb')
9
+
10
+ module Qrack
11
+ module Transport09
12
+ class Frame
13
+
14
+ FOOTER = 206
15
+ ID = 0
16
+
17
+ @types = {
18
+ 1 => 'Method',
19
+ 2 => 'Header',
20
+ 3 => 'Body',
21
+ 8 => 'Heartbeat',
22
+ }
23
+
24
+ attr_accessor :channel, :payload
25
+
26
+ def initialize payload = nil, channel = 0
27
+ @channel, @payload = channel, payload
28
+ end
29
+
30
+ def id
31
+ self.class::ID
32
+ end
33
+
34
+ def to_binary
35
+ buf = Transport09::Buffer.new
36
+ buf.write :octet, id
37
+ buf.write :short, channel
38
+ buf.write :longstr, payload
39
+ buf.write :octet, FOOTER
40
+ buf.rewind
41
+ buf
42
+ end
43
+
44
+ def to_s
45
+ to_binary.to_s
46
+ end
47
+
48
+ def == frame
49
+ [ :id, :channel, :payload ].inject(true) do |eql, field|
50
+ eql and __send__(field) == frame.__send__(field)
51
+ end
52
+ end
53
+
54
+ def self.parse buf
55
+ buf = Transport09::Buffer.new(buf) unless buf.is_a? Transport09::Buffer
56
+ buf.extract do
57
+ id, channel, payload, footer = buf.read(:octet, :short, :longstr, :octet)
58
+ Qrack::Transport09.const_get(@types[id]).new(payload, channel) if footer == FOOTER
59
+ end
60
+ end
61
+
62
+ end
63
+
64
+ class Method < Frame
65
+
66
+ ID = 1
67
+
68
+ def initialize payload = nil, channel = 0
69
+ super
70
+ unless @payload.is_a? Protocol09::Class::Method or @payload.nil?
71
+ @payload = Protocol09.parse(@payload)
72
+ end
73
+ end
74
+ end
75
+
76
+ class Header < Frame
77
+
78
+ ID = 2
79
+
80
+ def initialize payload = nil, channel = 0
81
+ super
82
+ unless @payload.is_a? Protocol09::Header or @payload.nil?
83
+ @payload = Protocol09::Header.new(@payload)
84
+ end
85
+ end
86
+ end
87
+
88
+ class Body < Frame
89
+ ID = 3
90
+ end
91
+
92
+ class Heartbeat < Frame
93
+ ID = 8
94
+ end
95
+
96
+ end
97
+ end
@@ -0,0 +1,77 @@
1
+ # encoding: utf-8
2
+
3
+ # bunny_spec.rb
4
+
5
+ # Assumes that target message broker/server has a user called 'guest' with a password 'guest'
6
+ # and that it is running on 'localhost'.
7
+
8
+ # If this is not the case, please change the 'Bunny.new' call below to include
9
+ # the relevant arguments e.g. @b = Bunny.new(:user => 'john', :pass => 'doe', :host => 'foobar')
10
+
11
+ require File.expand_path(File.join(File.dirname(__FILE__), %w[.. .. lib bunny]))
12
+
13
+ describe Bunny do
14
+
15
+ before(:each) do
16
+ @b = Bunny.new
17
+ @b.start
18
+ end
19
+
20
+ after(:each) do
21
+ begin
22
+ @b.stop
23
+ rescue Exception
24
+ ensure
25
+ @b = nil
26
+ end
27
+ end
28
+
29
+ it "should connect to an AMQP server" do
30
+ @b.status.should == :connected
31
+ end
32
+
33
+ it "should be able to create and open a new channel" do
34
+ c = @b.create_channel
35
+ c.number.should == 2
36
+ c.should be_an_instance_of(Bunny::Channel)
37
+ @b.channels.size.should == 3
38
+ c.open.should == :open_ok
39
+ @b.channel.number.should == 2
40
+ end
41
+
42
+ it "should be able to switch between channels" do
43
+ @b.channel.number.should == 1
44
+ @b.switch_channel(0)
45
+ @b.channel.number.should == 0
46
+ end
47
+
48
+ it "should raise an error if trying to switch to a non-existent channel" do
49
+ lambda { @b.switch_channel(5)}.should raise_error(RuntimeError)
50
+ end
51
+
52
+ it "should be able to create an exchange" do
53
+ exch = @b.exchange('test_exchange')
54
+ exch.should be_an_instance_of(Bunny::Exchange)
55
+ exch.name.should == 'test_exchange'
56
+ @b.exchanges.has_key?('test_exchange').should be(true)
57
+ end
58
+
59
+ it "should be able to create a queue" do
60
+ q = @b.queue('test1')
61
+ q.should be_an_instance_of(Bunny::Queue)
62
+ q.name.should == 'test1'
63
+ @b.queues.has_key?('test1').should be(true)
64
+ end
65
+
66
+ # Current RabbitMQ has not implemented some functionality
67
+ it "should raise an error if setting of QoS fails" do
68
+ lambda { @b.qos(:global => true) }.should raise_error(Bunny::ForcedConnectionCloseError)
69
+ @b.status.should == :not_connected
70
+ end
71
+
72
+ it "should be able to set QoS" do
73
+ @b.qos.should == :qos_ok
74
+ end
75
+
76
+ end
77
+
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+
3
+ # connection_spec.rb
4
+
5
+ require File.expand_path(File.join(File.dirname(__FILE__), %w[.. .. lib bunny]))
6
+
7
+ describe Bunny do
8
+
9
+ it "should raise an error if the wrong user name or password is used" do
10
+ b = Bunny.new(:user => 'wrong')
11
+ lambda { b.start}.should raise_error(Bunny::ProtocolError)
12
+ end
13
+
14
+ it "should be able to open a TCPSocket with a timeout" do
15
+ b = Bunny.new(:spec => "0.8")
16
+ connect_timeout = 5
17
+ lambda {
18
+ Bunny::Timer::timeout(connect_timeout, Qrack::ConnectionTimeout) do
19
+ TCPSocket.new(b.host, b.port)
20
+ end
21
+ }.should_not raise_error(Exception)
22
+ end
23
+
24
+ end
25
+
@@ -0,0 +1,173 @@
1
+ # encoding: utf-8
2
+
3
+ # exchange_spec.rb
4
+
5
+ # Assumes that target message broker/server has a user called 'guest' with a password 'guest'
6
+ # and that it is running on 'localhost'.
7
+
8
+ # If this is not the case, please change the 'Bunny.new' call below to include
9
+ # the relevant arguments e.g. @b = Bunny.new(:user => 'john', :pass => 'doe', :host => 'foobar')
10
+
11
+ require File.expand_path(File.join(File.dirname(__FILE__), %w[.. .. lib bunny]))
12
+
13
+ describe 'Exchange' do
14
+
15
+ before(:each) do
16
+ @b = Bunny.new
17
+ @b.start
18
+ end
19
+
20
+ after(:each) do
21
+ begin
22
+ @b.stop
23
+ rescue Exception
24
+ ensure
25
+ @b = nil
26
+ end
27
+ end
28
+
29
+ it "should raise an error if instantiated as non-existent type" do
30
+ lambda { @b.exchange('bogus_ex', :type => :bogus) }.should raise_error(Bunny::ForcedConnectionCloseError)
31
+ @b.status.should == :not_connected
32
+ end
33
+
34
+ it "should allow a default direct exchange to be instantiated by specifying :type" do
35
+ exch = @b.exchange('amq.direct', :type => :direct)
36
+ exch.should be_an_instance_of(Bunny::Exchange)
37
+ exch.name.should == 'amq.direct'
38
+ exch.type.should == :direct
39
+ @b.exchanges.has_key?('amq.direct').should be(true)
40
+ end
41
+
42
+ it "should allow a default direct exchange to be instantiated without specifying :type" do
43
+ exch = @b.exchange('amq.direct')
44
+ exch.should be_an_instance_of(Bunny::Exchange)
45
+ exch.name.should == 'amq.direct'
46
+ exch.type.should == :direct
47
+ @b.exchanges.has_key?('amq.direct').should be(true)
48
+ end
49
+
50
+ it "should allow a default fanout exchange to be instantiated without specifying :type" do
51
+ exch = @b.exchange('amq.fanout')
52
+ exch.should be_an_instance_of(Bunny::Exchange)
53
+ exch.name.should == 'amq.fanout'
54
+ exch.type.should == :fanout
55
+ @b.exchanges.has_key?('amq.fanout').should be(true)
56
+ end
57
+
58
+ it "should allow a default topic exchange to be instantiated without specifying :type" do
59
+ exch = @b.exchange('amq.topic')
60
+ exch.should be_an_instance_of(Bunny::Exchange)
61
+ exch.name.should == 'amq.topic'
62
+ exch.type.should == :topic
63
+ @b.exchanges.has_key?('amq.topic').should be(true)
64
+ end
65
+
66
+ it "should allow a default headers (amq.match) exchange to be instantiated without specifying :type" do
67
+ exch = @b.exchange('amq.match')
68
+ exch.should be_an_instance_of(Bunny::Exchange)
69
+ exch.name.should == 'amq.match'
70
+ exch.type.should == :headers
71
+ @b.exchanges.has_key?('amq.match').should be(true)
72
+ end
73
+
74
+ it "should allow a default headers (amq.headers) exchange to be instantiated without specifying :type" do
75
+ exch = @b.exchange('amq.headers')
76
+ exch.should be_an_instance_of(Bunny::Exchange)
77
+ exch.name.should == 'amq.headers'
78
+ exch.type.should == :headers
79
+ @b.exchanges.has_key?('amq.headers').should be(true)
80
+ end
81
+
82
+ it "should create an exchange as direct by default" do
83
+ exch = @b.exchange('direct_defaultex')
84
+ exch.should be_an_instance_of(Bunny::Exchange)
85
+ exch.name.should == 'direct_defaultex'
86
+ exch.type.should == :direct
87
+ @b.exchanges.has_key?('direct_defaultex').should be(true)
88
+ end
89
+
90
+ it "should be able to be instantiated as a direct exchange" do
91
+ exch = @b.exchange('direct_exchange', :type => :direct)
92
+ exch.should be_an_instance_of(Bunny::Exchange)
93
+ exch.name.should == 'direct_exchange'
94
+ exch.type.should == :direct
95
+ @b.exchanges.has_key?('direct_exchange').should be(true)
96
+ end
97
+
98
+ it "should be able to be instantiated as a topic exchange" do
99
+ exch = @b.exchange('topic_exchange', :type => :topic)
100
+ exch.should be_an_instance_of(Bunny::Exchange)
101
+ exch.name.should == 'topic_exchange'
102
+ exch.type.should == :topic
103
+ @b.exchanges.has_key?('topic_exchange').should be(true)
104
+ end
105
+
106
+ it "should be able to be instantiated as a fanout exchange" do
107
+ exch = @b.exchange('fanout_exchange', :type => :fanout)
108
+ exch.should be_an_instance_of(Bunny::Exchange)
109
+ exch.name.should == 'fanout_exchange'
110
+ exch.type.should == :fanout
111
+ @b.exchanges.has_key?('fanout_exchange').should be(true)
112
+ end
113
+
114
+ it "should be able to be instantiated as a headers exchange" do
115
+ exch = @b.exchange('headers_exchange', :type => :headers)
116
+ exch.should be_an_instance_of(Bunny::Exchange)
117
+ exch.name.should == 'headers_exchange'
118
+ exch.type.should == :headers
119
+ @b.exchanges.has_key?('headers_exchange').should be(true)
120
+ end
121
+
122
+ it "should ignore the :nowait option when instantiated" do
123
+ exch = @b.exchange('direct2_exchange', :nowait => true)
124
+ end
125
+
126
+ it "should be able to publish a message" do
127
+ exch = @b.exchange('direct_exchange')
128
+ exch.publish('This is a published message')
129
+ end
130
+
131
+ it "should not modify the passed options hash when publishing a message" do
132
+ exch = @b.exchange('direct_exchange')
133
+ opts = {:key => 'a', :persistent => true}
134
+ exch.publish('', opts)
135
+ opts.should == {:key => 'a', :persistent => true}
136
+ end
137
+
138
+ it "should be able to return an undeliverable message" do
139
+ exch = @b.exchange('return_exch')
140
+ exch.publish('This message should be undeliverable', :immediate => true)
141
+ ret_msg = @b.returned_message
142
+ ret_msg.should be_an_instance_of(Hash)
143
+ ret_msg[:payload].should == 'This message should be undeliverable'
144
+ end
145
+
146
+ it "should be able to return a message that exceeds maximum frame size" do
147
+ exch = @b.exchange('return_exch')
148
+ lg_msg = 'z' * 142000
149
+ exch.publish(lg_msg, :immediate => true)
150
+ ret_msg = @b.returned_message
151
+ ret_msg.should be_an_instance_of(Hash)
152
+ ret_msg[:payload].should == lg_msg
153
+ end
154
+
155
+ it "should report an error if delete fails" do
156
+ exch = @b.exchange('direct_exchange')
157
+ lambda { exch.delete(:exchange => 'bogus_ex') }.should raise_error(Bunny::ForcedChannelCloseError)
158
+ @b.channel.active.should == false
159
+ end
160
+
161
+ it "should be able to be deleted" do
162
+ exch = @b.exchange('direct_exchange')
163
+ res = exch.delete
164
+ res.should == :delete_ok
165
+ @b.exchanges.has_key?('direct_exchange').should be(false)
166
+ end
167
+
168
+ it "should ignore the :nowait option when deleted" do
169
+ exch = @b.exchange('direct2_exchange')
170
+ exch.delete(:nowait => true)
171
+ end
172
+
173
+ end
@@ -0,0 +1,235 @@
1
+ # encoding: utf-8
2
+
3
+ # queue_spec.rb
4
+
5
+ # Assumes that target message broker/server has a user called 'guest' with a password 'guest'
6
+ # and that it is running on 'localhost'.
7
+
8
+ # If this is not the case, please change the 'Bunny.new' call below to include
9
+ # the relevant arguments e.g. @b = Bunny.new(:user => 'john', :pass => 'doe', :host => 'foobar')
10
+
11
+ require File.expand_path(File.join(File.dirname(__FILE__), %w[.. .. lib bunny]))
12
+
13
+ describe 'Queue' do
14
+
15
+ def expect_deprecation_warning_for_publishing_on_queue(q, n=1)
16
+ Bunny.should_receive(:deprecation_warning).with("Qrack::Queue#publish", "0.8", anything).exactly(n).times
17
+ end
18
+
19
+ def message_count(queue, sleep_time = 0.1)
20
+ sleep sleep_time
21
+ queue.message_count
22
+ end
23
+
24
+ before(:each) do
25
+ @b = Bunny.new
26
+ @b.start
27
+ end
28
+
29
+ after(:each) do
30
+ begin
31
+ @b.stop
32
+ rescue Exception
33
+ ensure
34
+ @b = nil
35
+ end
36
+ end
37
+
38
+ it "should ignore the :nowait option when instantiated" do
39
+ q = @b.queue('test0', :nowait => true)
40
+ end
41
+
42
+ it "should ignore the :nowait option when binding to an exchange" do
43
+ exch = @b.exchange('direct_exch')
44
+ q = @b.queue('test0')
45
+ q.bind(exch, :nowait => true).should == :bind_ok
46
+ end
47
+
48
+ it "should raise an error when trying to bind to a non-existent exchange" do
49
+ q = @b.queue('test1')
50
+ lambda {q.bind('bogus')}.should raise_error(Bunny::ForcedChannelCloseError)
51
+ @b.channel.active.should == false
52
+ end
53
+
54
+ it "should be able to bind to an existing exchange" do
55
+ exch = @b.exchange('direct_exch')
56
+ q = @b.queue('test1')
57
+ q.bind(exch).should == :bind_ok
58
+ end
59
+
60
+ it "should ignore the :nowait option when unbinding from an exchange" do
61
+ exch = @b.exchange('direct_exch')
62
+ q = @b.queue('test0')
63
+ q.unbind(exch, :nowait => true).should == :unbind_ok
64
+ end
65
+
66
+ it "should raise an error if unbinding from a non-existent exchange" do
67
+ q = @b.queue('test1')
68
+ lambda {q.unbind('bogus')}.should raise_error(Bunny::ForcedChannelCloseError)
69
+ @b.channel.active.should == false
70
+ end
71
+
72
+ it "should be able to unbind from an existing exchange" do
73
+ exch = @b.exchange('direct_exch')
74
+ q = @b.queue('test1')
75
+ q.unbind(exch).should == :unbind_ok
76
+ end
77
+
78
+ it "should be able to publish a message" do
79
+ q = @b.queue('test1')
80
+ expect_deprecation_warning_for_publishing_on_queue(q)
81
+ q.publish('This is a test message')
82
+ message_count(q).should == 1
83
+ end
84
+
85
+ it "should be able to pop a message complete with header and delivery details" do
86
+ q = @b.queue('test1')
87
+ msg = q.pop()
88
+ msg.should be_an_instance_of(Hash)
89
+ msg[:header].should be_an_instance_of(Bunny::Protocol::Header)
90
+ msg[:payload].should == 'This is a test message'
91
+ msg[:delivery_details].should be_an_instance_of(Hash)
92
+ message_count(q).should == 0
93
+ end
94
+
95
+ it "should be able to pop a message and just get the payload" do
96
+ q = @b.queue('test1')
97
+ expect_deprecation_warning_for_publishing_on_queue(q)
98
+ q.publish('This is another test message')
99
+ msg = q.pop[:payload]
100
+ msg.should == 'This is another test message'
101
+ message_count(q).should == 0
102
+ end
103
+
104
+ it "should be able to pop a message where body length exceeds max frame size" do
105
+ q = @b.queue('test1')
106
+ lg_msg = 'z' * 142000
107
+ expect_deprecation_warning_for_publishing_on_queue(q)
108
+ q.publish(lg_msg)
109
+ msg = q.pop[:payload]
110
+ msg.should == lg_msg
111
+ end
112
+
113
+ it "should be able call a block when popping a message" do
114
+ q = @b.queue('test1')
115
+ expect_deprecation_warning_for_publishing_on_queue(q)
116
+ q.publish('This is another test message')
117
+ q.pop { |msg| msg[:payload].should == 'This is another test message' }
118
+ q.pop { |msg| msg[:payload].should == :queue_empty }
119
+ end
120
+
121
+ it "should raise an error if purge fails" do
122
+ q = @b.queue('test1')
123
+ expect_deprecation_warning_for_publishing_on_queue(q, 5)
124
+ 5.times {q.publish('This is another test message')}
125
+ message_count(q).should == 5
126
+ lambda {q.purge(:queue => 'bogus')}.should raise_error(Bunny::ForcedChannelCloseError)
127
+ end
128
+
129
+ it "should be able to be purged to remove all of its messages" do
130
+ q = @b.queue('test1')
131
+ message_count(q).should == 5
132
+ q.purge.should == :purge_ok
133
+ message_count(q).should == 0
134
+ end
135
+
136
+ it "should return an empty message when popping an empty queue" do
137
+ q = @b.queue('test1')
138
+ expect_deprecation_warning_for_publishing_on_queue(q)
139
+ q.publish('This is another test message')
140
+ q.pop
141
+ msg = q.pop[:payload]
142
+ msg.should == :queue_empty
143
+ end
144
+
145
+ it "should stop subscription without processing messages if max specified is 0" do
146
+ q = @b.queue('test1')
147
+ expect_deprecation_warning_for_publishing_on_queue(q, 5)
148
+ 5.times {q.publish('Yet another test message')}
149
+ message_count(q).should == 5
150
+ q.subscribe(:message_max => 0)
151
+ message_count(q).should == 5
152
+ q.purge.should == :purge_ok
153
+ end
154
+
155
+ it "should stop subscription after processing number of messages specified > 0" do
156
+ q = @b.queue('test1')
157
+ expect_deprecation_warning_for_publishing_on_queue(q, 5)
158
+ 5.times {q.publish('Yet another test message')}
159
+ message_count(q).should == 5
160
+ q.subscribe(:message_max => 5)
161
+ end
162
+
163
+ it "should stop subscription after processing message_max messages < total in queue" do
164
+ q = @b.queue('test1')
165
+ @b.qos()
166
+ expect_deprecation_warning_for_publishing_on_queue(q, 10)
167
+ 10.times {q.publish('Yet another test message')}
168
+ message_count(q).should == 10
169
+ q.subscribe(:message_max => 5, :ack => true)
170
+ message_count(q).should == 5
171
+ q.purge.should == :purge_ok
172
+ end
173
+
174
+ it "should raise an error when delete fails" do
175
+ q = @b.queue('test1')
176
+ lambda {q.delete(:queue => 'bogus')}.should raise_error(Bunny::ForcedChannelCloseError)
177
+ @b.channel.active.should == false
178
+ end
179
+
180
+ it "should pass correct block parameters through on subscribe" do
181
+ q = @b.queue('test1')
182
+ expect_deprecation_warning_for_publishing_on_queue(q)
183
+ q.publish("messages pop\'n")
184
+
185
+ q.subscribe do |msg|
186
+ msg[:header].should be_an_instance_of Qrack::Protocol::Header
187
+ msg[:payload].should == "messages pop'n"
188
+ msg[:delivery_details].should_not be_nil
189
+
190
+ q.unsubscribe
191
+ break
192
+ end
193
+ end
194
+
195
+ it "should finish processing subscription messages if break is called in block" do
196
+ q = @b.queue('test1')
197
+ expect_deprecation_warning_for_publishing_on_queue(q, 6)
198
+ q.publish('messages in my quezen')
199
+
200
+ q.subscribe do |msg|
201
+ msg[:payload].should == 'messages in my quezen'
202
+ q.unsubscribe
203
+ break
204
+ end
205
+
206
+ 5.times {|i| q.publish("#{i}")}
207
+ q.subscribe do |msg|
208
+ if msg[:payload] == '4'
209
+ q.unsubscribe
210
+ break
211
+ end
212
+ end
213
+ end
214
+
215
+ it "should be able to be deleted" do
216
+ q = @b.queue('test1')
217
+ res = q.delete
218
+ res.should == :delete_ok
219
+ @b.queues.has_key?('test1').should be(false)
220
+ end
221
+
222
+ it "should ignore the :nowait option when deleted" do
223
+ q = @b.queue('test0')
224
+ q.delete(:nowait => true)
225
+ end
226
+
227
+ it "should support server named queues" do
228
+ q = @b.queue
229
+ q.name.should_not == nil
230
+
231
+ @b.queue(q.name).should == q
232
+ q.delete
233
+ end
234
+
235
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+
3
+ # Assumes that target message broker/server has a user called 'guest' with a password 'guest'
4
+ # and that it is running on 'localhost'.
5
+
6
+ # If this is not the case, please change the 'Bunny.new' call below to include
7
+ # the relevant arguments e.g. @b = Bunny.new(:user => 'john', :pass => 'doe', :host => 'foobar')
8
+
9
+ require "bunny"
10
+
11
+ describe Bunny do
12
+ context "AMQP URL parsing" do
13
+ it "handles port properly" do
14
+ bunny = Bunny.new("amqp://dev.rabbitmq.com:1212")
15
+ bunny.port.should eql(1212)
16
+ bunny.stop
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,76 @@
1
+ # encoding: utf-8
2
+
3
+ # bunny_spec.rb
4
+
5
+ # Assumes that target message broker/server has a user called 'guest' with a password 'guest'
6
+ # and that it is running on 'localhost'.
7
+
8
+ # If this is not the case, please change the 'Bunny.new' call below to include
9
+ # the relevant arguments e.g. @b = Bunny.new(:user => 'john', :pass => 'doe', :host => 'foobar')
10
+
11
+ require "bunny"
12
+
13
+ describe Bunny do
14
+
15
+ before(:each) do
16
+ @b = Bunny.new(:spec => '09')
17
+ @b.start
18
+ end
19
+
20
+ after(:each) do
21
+ begin
22
+ @b.stop
23
+ rescue Exception
24
+ ensure
25
+ @b = nil
26
+ end
27
+ end
28
+
29
+ it "should connect to an AMQP server" do
30
+ @b.status.should == :connected
31
+ end
32
+
33
+ it "should be able to create and open a new channel" do
34
+ c = @b.create_channel
35
+ c.number.should == 2
36
+ c.should be_an_instance_of(Bunny::Channel09)
37
+ @b.channels.size.should == 3
38
+ c.open.should == :open_ok
39
+ @b.channel.number.should == 2
40
+ end
41
+
42
+ it "should be able to switch between channels" do
43
+ @b.channel.number.should == 1
44
+ @b.switch_channel(0)
45
+ @b.channel.number.should == 0
46
+ end
47
+
48
+ it "should raise an error if trying to switch to a non-existent channel" do
49
+ lambda { @b.switch_channel(5) }.should raise_error(RuntimeError)
50
+ end
51
+
52
+ it "should be able to create an exchange" do
53
+ exch = @b.exchange('test_exchange')
54
+ exch.should be_an_instance_of(Bunny::Exchange09)
55
+ exch.name.should == 'test_exchange'
56
+ @b.exchanges.has_key?('test_exchange').should be(true)
57
+ end
58
+
59
+ it "should be able to create a queue" do
60
+ q = @b.queue('test1')
61
+ q.should be_an_instance_of(Bunny::Queue09)
62
+ q.name.should == 'test1'
63
+ @b.queues.has_key?('test1').should be(true)
64
+ end
65
+
66
+ # Current RabbitMQ has not implemented some functionality
67
+ it "should raise an error if setting of QoS fails" do
68
+ lambda { @b.qos(:global => true) }.should raise_error(Bunny::ForcedConnectionCloseError)
69
+ @b.status.should == :not_connected
70
+ end
71
+
72
+ it "should be able to set QoS" do
73
+ @b.qos.should == :qos_ok
74
+ end
75
+
76
+ end