bunny 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  Google Group: [bunny-amqp](http://groups.google.com/group/bunny-amqp)
4
4
 
5
+ Mailing List: [bunny-amqp-devel](http://rubyforge.org/mailman/listinfo/bunny-amqp-devel)
6
+
7
+ Rubyforge: [bunny-amqp](http://bunny-amqp.rubyforge.org)
8
+
5
9
  ## Announcements
6
10
 
7
11
  **IMPORTANT**
data/lib/amqp.rb CHANGED
@@ -6,6 +6,7 @@ module AMQP
6
6
  # constants
7
7
  CONNECTED = 'CONNECTED'
8
8
  NOT_CONNECTED = 'NOT CONNECTED'
9
+ QUEUE_EMPTY = 'QUEUE EMPTY'
9
10
 
10
11
  # specific error definitions
11
12
  class ProtocolError < StandardError; end
@@ -1,54 +1,65 @@
1
- class Exchange
1
+ class Bunny
2
+ class Exchange
2
3
 
3
- include AMQP
4
+ include AMQP
4
5
 
5
- attr_reader :client, :type, :name, :opts, :key
6
+ attr_reader :client, :type, :name, :opts, :key
6
7
 
7
- def initialize(client, name, opts = {})
8
- # check connection to server
9
- raise 'Not connected to server' if client.status == NOT_CONNECTED
8
+ def initialize(client, name, opts = {})
9
+ # check connection to server
10
+ raise 'Not connected to server' if client.status == NOT_CONNECTED
10
11
 
11
- @client, @name, @opts = client, name, opts
12
- @type = opts[:type] || :direct
13
- opts.delete(:type) unless opts[:type].nil?
14
- @key = opts[:key]
15
- @client.exchanges[@name] ||= self
12
+ @client, @name, @opts = client, name, opts
13
+ @type = opts[:type] || :direct
14
+ opts.delete(:type) unless opts[:type].nil?
15
+ @key = opts[:key]
16
+ @client.exchanges[@name] ||= self
16
17
 
17
- unless name == "amq.#{type}" or name == ''
18
- client.send_frame(
19
- Protocol::Exchange::Declare.new(
20
- { :exchange => name, :type => type, :nowait => true }.merge(opts)
21
- )
22
- )
23
- end
24
- end
25
-
26
- def publish(data, opts = {})
27
- out = []
28
-
29
- out << Protocol::Basic::Publish.new(
30
- { :exchange => name, :routing_key => opts.delete(:key) || key }.merge(opts)
31
- )
32
- data = data.to_s
33
- out << Protocol::Header.new(
34
- Protocol::Basic,
35
- data.length, {
36
- :content_type => 'application/octet-stream',
37
- :delivery_mode => (opts.delete(:persistent) ? 2 : 1),
38
- :priority => 0
39
- }.merge(opts)
40
- )
41
- out << Frame::Body.new(data)
42
-
43
- client.send_frame(*out)
44
- end
45
-
46
- def delete(opts = {})
47
- client.send_frame(
48
- Protocol::Exchange::Delete.new({ :exchange => name, :nowait => true }.merge(opts))
49
- )
50
-
51
- client.exchanges.delete(name)
52
- end
18
+ unless name == "amq.#{type}" or name == ''
19
+ client.send_frame(
20
+ Protocol::Exchange::Declare.new(
21
+ { :exchange => name, :type => type, :nowait => false }.merge(opts)
22
+ )
23
+ )
53
24
 
25
+ raise ProtocolError,
26
+ "Error declaring exchange #{name}: type = #{type}" unless
27
+ client.next_method.is_a?(Protocol::Exchange::DeclareOk)
28
+ end
29
+ end
30
+
31
+ def publish(data, opts = {})
32
+ out = []
33
+
34
+ out << Protocol::Basic::Publish.new(
35
+ { :exchange => name, :routing_key => opts.delete(:key) || key }.merge(opts)
36
+ )
37
+ data = data.to_s
38
+ out << Protocol::Header.new(
39
+ Protocol::Basic,
40
+ data.length, {
41
+ :content_type => 'application/octet-stream',
42
+ :delivery_mode => (opts.delete(:persistent) ? 2 : 1),
43
+ :priority => 0
44
+ }.merge(opts)
45
+ )
46
+ out << Frame::Body.new(data)
47
+
48
+ client.send_frame(*out)
49
+ end
50
+
51
+ def delete(opts = {})
52
+ client.send_frame(
53
+ Protocol::Exchange::Delete.new({ :exchange => name, :nowait => false }.merge(opts))
54
+ )
55
+
56
+ raise ProtocolError,
57
+ "Error deleting exchange #{name}" unless
58
+ client.next_method.is_a?(Protocol::Exchange::DeleteOk)
59
+
60
+ client.exchanges.delete(name)
61
+ end
62
+
63
+ end
64
+
54
65
  end
data/lib/bunny/header.rb CHANGED
@@ -1,30 +1,32 @@
1
- class Header
1
+ class Bunny
2
+ class Header
2
3
 
3
- include AMQP
4
+ include AMQP
4
5
 
5
- attr_reader :client
6
+ attr_reader :client
6
7
 
7
- def initialize(client, header_obj)
8
- @client = client
9
- @header = header_obj
10
- end
8
+ def initialize(client, header_obj)
9
+ @client = client
10
+ @header = header_obj
11
+ end
11
12
 
12
- # Acknowledges the receipt of this message with the server.
13
- def ack
14
- client.send(Protocol::Basic::Ack.new(:delivery_tag => properties[:delivery_tag]))
15
- end
13
+ # Acknowledges the receipt of this message with the server.
14
+ def ack
15
+ client.send(Protocol::Basic::Ack.new(:delivery_tag => properties[:delivery_tag]))
16
+ end
16
17
 
17
- # Reject this message (XXX currently unimplemented in rabbitmq)
18
- # * :requeue => true | false (default false)
19
- def reject(opts = {})
20
- client.send(Protocol::Basic::Reject.new(opts.merge(:delivery_tag => properties[:delivery_tag])))
21
- end
18
+ # Reject this message (XXX currently unimplemented in rabbitmq)
19
+ # * :requeue => true | false (default false)
20
+ def reject(opts = {})
21
+ client.send(Protocol::Basic::Reject.new(opts.merge(:delivery_tag => properties[:delivery_tag])))
22
+ end
22
23
 
23
- def method_missing(meth, *args, &blk)
24
- @header.send(meth, *args, &blk)
25
- end
24
+ def method_missing(meth, *args, &blk)
25
+ @header.send(meth, *args, &blk)
26
+ end
26
27
 
27
- def inspect
28
- @header.inspect
29
- end
28
+ def inspect
29
+ @header.inspect
30
+ end
31
+ end
30
32
  end
data/lib/bunny/queue.rb CHANGED
@@ -1,3 +1,4 @@
1
+ class Bunny
1
2
  class Queue
2
3
 
3
4
  include AMQP
@@ -13,8 +14,10 @@
13
14
  @opts = opts
14
15
  @name = name
15
16
  client.send_frame(
16
- Protocol::Queue::Declare.new({ :queue => name, :nowait => true }.merge(opts))
17
+ Protocol::Queue::Declare.new({ :queue => name, :nowait => false }.merge(opts))
17
18
  )
19
+
20
+ raise ProtocolError, "Error declaring queue #{name}" unless client.next_method.is_a?(Protocol::Queue::DeclareOk)
18
21
  end
19
22
 
20
23
  def pop(opts = {})
@@ -29,11 +32,19 @@
29
32
  :no_ack => !opts.delete(:ack),
30
33
  :nowait => true }.merge(opts))
31
34
  )
32
- method = client.next_method
33
- return unless method.is_a?(Protocol::Basic::GetOk)
35
+
36
+ method = client.next_method
37
+
38
+ if method.is_a?(Protocol::Basic::GetEmpty) then
39
+ return QUEUE_EMPTY
40
+ elsif !method.is_a?(Protocol::Basic::GetOk)
41
+ raise ProtocolError, "Error getting message from queue #{name}"
42
+ end
34
43
 
35
44
  self.delivery_tag = method.delivery_tag
36
45
 
46
+ return QUEUE_EMPTY unless !self.delivery_tag.nil?
47
+
37
48
  header = client.next_payload
38
49
  msg = client.next_payload
39
50
  raise 'unexpected length' if msg.length < header.size
@@ -75,8 +86,12 @@
75
86
  Protocol::Queue::Bind.new({ :queue => name,
76
87
  :exchange => exchange,
77
88
  :routing_key => opts.delete(:key),
78
- :nowait => true }.merge(opts))
89
+ :nowait => false }.merge(opts))
79
90
  )
91
+
92
+ raise ProtocolError,
93
+ "Error binding queue #{name}" unless
94
+ client.next_method.is_a?(Protocol::Queue::BindOk)
80
95
  end
81
96
 
82
97
  def unbind(exchange, opts = {})
@@ -87,25 +102,35 @@
87
102
  Protocol::Queue::Unbind.new({ :queue => name,
88
103
  :exchange => exchange,
89
104
  :routing_key => opts.delete(:key),
90
- :nowait => true }.merge(opts)
105
+ :nowait => false }.merge(opts)
91
106
  )
92
107
  )
108
+
109
+ raise ProtocolError,
110
+ "Error unbinding queue #{name}" unless
111
+ client.next_method.is_a?(Protocol::Queue::UnbindOk)
93
112
  end
94
113
 
95
114
  def delete(opts = {})
96
115
  client.send_frame(
97
- Protocol::Queue::Delete.new({ :queue => name, :nowait => true }.merge(opts))
116
+ Protocol::Queue::Delete.new({ :queue => name, :nowait => false }.merge(opts))
98
117
  )
99
118
 
119
+ raise ProtocolError,
120
+ "Error deleting queue #{name}" unless
121
+ client.next_method.is_a?(Protocol::Queue::DeleteOk)
122
+
100
123
  client.queues.delete(name)
101
124
  end
102
125
 
103
126
  private
104
127
  def exchange
105
- @exchange ||= Exchange.new(client, '', {:type => :direct, :key => name})
128
+ @exchange ||= Bunny::Exchange.new(client, '', {:type => :direct, :key => name})
106
129
  end
107
130
 
108
131
  def bindings
109
132
  @bindings ||= {}
110
133
  end
111
- end
134
+ end
135
+
136
+ end
data/spec/bunny_spec.rb CHANGED
@@ -15,24 +15,20 @@ describe Bunny do
15
15
  @b.start
16
16
  end
17
17
 
18
- after(:each) do
19
- @b.stop
20
- end
21
-
22
18
  it "should connect to an AMQP server" do
23
19
  @b.status.should == 'CONNECTED'
24
20
  end
25
21
 
26
22
  it "should be able to create an exchange" do
27
23
  exch = @b.exchange('test_exchange')
28
- exch.should be_an_instance_of Exchange
24
+ exch.should be_an_instance_of Bunny::Exchange
29
25
  exch.name.should == 'test_exchange'
30
26
  @b.exchanges.has_key?('test_exchange').should be true
31
27
  end
32
28
 
33
29
  it "should be able to create a queue" do
34
30
  q = @b.queue('test1')
35
- q.should be_an_instance_of Queue
31
+ q.should be_an_instance_of Bunny::Queue
36
32
  q.name.should == 'test1'
37
33
  @b.queues.has_key?('test1').should be true
38
34
  end
@@ -8,20 +8,20 @@
8
8
 
9
9
  require File.expand_path(File.join(File.dirname(__FILE__), %w[.. lib bunny]))
10
10
 
11
- describe Exchange do
11
+ describe Bunny::Exchange do
12
12
 
13
13
  before(:each) do
14
14
  @b = Bunny.new
15
15
  @b.start
16
16
  end
17
-
18
- after(:each) do
19
- @b.stop
17
+
18
+ it "should raise an error if instantiated as non-existent type" do
19
+ lambda { @b.exchange('bogus_ex', :type => :bogus) }.should raise_error(AMQP::ProtocolError)
20
20
  end
21
21
 
22
22
  it "should be able to create a default direct exchange" do
23
23
  exch = @b.exchange('direct_defaultex')
24
- exch.should be_an_instance_of Exchange
24
+ exch.should be_an_instance_of Bunny::Exchange
25
25
  exch.name.should == 'direct_defaultex'
26
26
  exch.type.should == :direct
27
27
  @b.exchanges.has_key?('direct_defaultex').should be true
@@ -29,7 +29,7 @@ describe Exchange do
29
29
 
30
30
  it "should be able to be instantiated as a direct exchange" do
31
31
  exch = @b.exchange('direct_exchange', :type => :direct)
32
- exch.should be_an_instance_of Exchange
32
+ exch.should be_an_instance_of Bunny::Exchange
33
33
  exch.name.should == 'direct_exchange'
34
34
  exch.type.should == :direct
35
35
  @b.exchanges.has_key?('direct_exchange').should be true
@@ -37,7 +37,7 @@ describe Exchange do
37
37
 
38
38
  it "should be able to be instantiated as a topic exchange" do
39
39
  exch = @b.exchange('topic_exchange', :type => :topic)
40
- exch.should be_an_instance_of Exchange
40
+ exch.should be_an_instance_of Bunny::Exchange
41
41
  exch.name.should == 'topic_exchange'
42
42
  exch.type.should == :topic
43
43
  @b.exchanges.has_key?('topic_exchange').should be true
@@ -45,7 +45,7 @@ describe Exchange do
45
45
 
46
46
  it "should be able to be instantiated as a fanout exchange" do
47
47
  exch = @b.exchange('fanout_exchange', :type => :fanout)
48
- exch.should be_an_instance_of Exchange
48
+ exch.should be_an_instance_of Bunny::Exchange
49
49
  exch.name.should == 'fanout_exchange'
50
50
  exch.type.should == :fanout
51
51
  @b.exchanges.has_key?('fanout_exchange').should be true
data/spec/queue_spec.rb CHANGED
@@ -15,8 +15,16 @@ describe Bunny do
15
15
  @b.start
16
16
  end
17
17
 
18
- after(:each) do
19
- @b.stop
18
+ it "should be able to bind to an exchange" do
19
+ exch = @b.exchange('direct_exch')
20
+ q = @b.queue('test1')
21
+ q.bind(exch)
22
+ end
23
+
24
+ it "should be able to unbind from an exchange" do
25
+ exch = @b.exchange('direct_exch')
26
+ q = @b.queue('test1')
27
+ q.unbind(exch)
20
28
  end
21
29
 
22
30
  it "should be able to publish a message" do
@@ -41,6 +49,14 @@ describe Bunny do
41
49
  msg.should == 'This is another test message'
42
50
  q.message_count.should == 0
43
51
  end
52
+
53
+ it "should return an empty message when popping an empty queue" do
54
+ q = @b.queue('test1')
55
+ q.publish('This is another test message')
56
+ q.pop
57
+ msg = q.pop
58
+ msg.should == 'QUEUE EMPTY'
59
+ end
44
60
 
45
61
  it "should be able to be deleted" do
46
62
  q = @b.queue('test1')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bunny
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Duncan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-24 00:00:00 +01:00
12
+ date: 2009-04-25 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -64,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
64
  version:
65
65
  requirements: []
66
66
 
67
- rubyforge_project:
67
+ rubyforge_project: bunny-amqp
68
68
  rubygems_version: 1.3.1
69
69
  signing_key:
70
70
  specification_version: 2