message_queue 0.0.1 → 0.0.2
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 +4 -4
- data/Gemfile +3 -0
- data/README.md +32 -2
- data/Rakefile +36 -0
- data/examples/consumer.rb +20 -0
- data/examples/publisher.rb +15 -0
- data/lib/message_queue.rb +77 -1
- data/lib/message_queue/adapter.rb +15 -0
- data/lib/message_queue/adapters/bunny.rb +21 -0
- data/lib/message_queue/adapters/bunny/connection.rb +64 -0
- data/lib/message_queue/adapters/bunny/consumer.rb +61 -0
- data/lib/message_queue/adapters/bunny/publisher.rb +40 -0
- data/lib/message_queue/serializer.rb +19 -0
- data/lib/message_queue/serializers/message_pack.rb +15 -0
- data/lib/message_queue/serializers/plain.rb +13 -0
- data/lib/message_queue/version.rb +1 -1
- data/message_queue.gemspec +3 -4
- data/test/adapters/bunny_test.rb +82 -0
- data/test/message_queue_test.rb +30 -0
- data/test/serializers/message_pack_test.rb +10 -0
- data/test/serializers/plain_test.rb +10 -0
- data/test/support/message_queue.yml +2 -0
- data/test/test_helper.rb +2 -0
- metadata +28 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e139a22eb92dc83672337ded0224ee8ae245ddad
|
4
|
+
data.tar.gz: c78ce34bfdd40ddcde86691b35e599610621b5c4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a2e5be7579e33fd0ac8bdef724eedeb8a78151128e0c7c5ec175b0e64833d56887308d5345c53c070cfdf2e8352ef097e535aefa41bc2dd5af728eb9eb77cd5
|
7
|
+
data.tar.gz: 804f366204e4bf1b979a2938e8bab9a760cac1085f6827986ae71da6a751f2d3e53bea12885fa218a41b523fe990dff4a561493ad2b06d6484bd658ea56974c6
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# MessageQueue
|
2
2
|
|
3
|
-
|
3
|
+
A common interface to multiple message queues libraries.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -18,7 +18,37 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
```ruby
|
22
|
+
MessageQueue.with_connection(:adapter => :bunny, :serializer => :message_pack) do |conn|
|
23
|
+
publisher = conn.new_publisher(
|
24
|
+
:exchange => {
|
25
|
+
:name => "time",
|
26
|
+
:type => :topic
|
27
|
+
},
|
28
|
+
:message => {
|
29
|
+
:routing_key => "time.now"
|
30
|
+
}
|
31
|
+
)
|
32
|
+
|
33
|
+
consumer = conn.new_consumer(
|
34
|
+
:queue => {
|
35
|
+
:name => "print_time_now"
|
36
|
+
},
|
37
|
+
:exchange => {
|
38
|
+
:name => "time",
|
39
|
+
:routing_key => "time.#"
|
40
|
+
}
|
41
|
+
)
|
42
|
+
|
43
|
+
consumer.subscribe do |delivery_info, metadata, payload|
|
44
|
+
puts "Received message: #{payload}"
|
45
|
+
end
|
46
|
+
|
47
|
+
publisher.publish Time.now.to_s
|
48
|
+
|
49
|
+
sleep 1
|
50
|
+
end
|
51
|
+
```
|
22
52
|
|
23
53
|
## Contributing
|
24
54
|
|
data/Rakefile
CHANGED
@@ -1 +1,37 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
#############################################################################
|
4
|
+
#
|
5
|
+
# Helper functions
|
6
|
+
#
|
7
|
+
#############################################################################
|
8
|
+
|
9
|
+
def name
|
10
|
+
@name ||= Dir['*.gemspec'].first.split('.').first
|
11
|
+
end
|
12
|
+
|
13
|
+
#############################################################################
|
14
|
+
#
|
15
|
+
# Standard tasks
|
16
|
+
#
|
17
|
+
#############################################################################
|
18
|
+
|
19
|
+
task :default => :test
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/*_test.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
desc "Open an irb session preloaded with this library"
|
29
|
+
task :console do
|
30
|
+
sh "irb -rubygems -r ./lib/#{name}.rb"
|
31
|
+
end
|
32
|
+
|
33
|
+
#############################################################################
|
34
|
+
#
|
35
|
+
# Custom tasks (add your own tasks here)
|
36
|
+
#
|
37
|
+
#############################################################################
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require_relative "../lib/message_queue"
|
2
|
+
|
3
|
+
MessageQueue.with_connection(:adapter => :bunny, :serializer => :message_pack) do |conn|
|
4
|
+
consumer = conn.new_consumer(
|
5
|
+
:queue => {
|
6
|
+
:name => "print_time_now"
|
7
|
+
},
|
8
|
+
:exchange => {
|
9
|
+
:name => "time",
|
10
|
+
:routing_key => "time.#"
|
11
|
+
},
|
12
|
+
:subscribe => {
|
13
|
+
:block => true
|
14
|
+
}
|
15
|
+
)
|
16
|
+
|
17
|
+
consumer.subscribe do |delivery_info, metadata, payload|
|
18
|
+
puts "Received message: #{payload}"
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require_relative "../lib/message_queue"
|
2
|
+
|
3
|
+
MessageQueue.with_connection(:adapter => :bunny, :serializer => :message_pack) do |conn|
|
4
|
+
publisher = conn.new_publisher(
|
5
|
+
:exchange => {
|
6
|
+
:name => "time",
|
7
|
+
:type => :topic
|
8
|
+
},
|
9
|
+
:message => {
|
10
|
+
:routing_key => "time.now"
|
11
|
+
}
|
12
|
+
)
|
13
|
+
|
14
|
+
publisher.publish Time.now.to_s
|
15
|
+
end
|
data/lib/message_queue.rb
CHANGED
@@ -1,5 +1,81 @@
|
|
1
1
|
require "message_queue/version"
|
2
|
+
require "message_queue/adapter"
|
3
|
+
require "message_queue/serializer"
|
2
4
|
|
3
5
|
module MessageQueue
|
4
|
-
|
6
|
+
extend self
|
7
|
+
|
8
|
+
ADAPTERS = [:bunny]
|
9
|
+
SERIALIZERS = [:plain, :message_pack]
|
10
|
+
|
11
|
+
# Public: Initialize a connection to a message queue.
|
12
|
+
#
|
13
|
+
# options - The Hash options used to initialize a connection
|
14
|
+
# :adapter - The Symbol adapter, currently only :bunny is supported.
|
15
|
+
# Detailed options see individual adapter implementation.
|
16
|
+
# :serializer - The Symbol serializer for serialization.
|
17
|
+
#
|
18
|
+
# Returns the connection for the specified message queue.
|
19
|
+
# Raises a RuntimeError if an adapter can't be found.
|
20
|
+
def new_connection(options = {})
|
21
|
+
adapter = load_adapter(options[:adapter])
|
22
|
+
raise "Missing adapter #{options[:adapter]}" unless adapter
|
23
|
+
|
24
|
+
serializer = load_serializer(options[:serializer])
|
25
|
+
raise "Missing serializer #{options[:serializer]}" unless serializer
|
26
|
+
|
27
|
+
adapter.new_connection(serializer, options)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Public: Initialize a connection to a message queue, connect and execute code in a block.
|
31
|
+
#
|
32
|
+
# options - The Hash options used to initialize a connection
|
33
|
+
# :adapter - The Symbol adapter, currently only :bunny is supported.
|
34
|
+
# Detailed options see individual adapter implementation.
|
35
|
+
# :serializer - The Symbol serializer for serialization.
|
36
|
+
# block - The code to execute. The connection object with be passed in.
|
37
|
+
#
|
38
|
+
# Returns nothing
|
39
|
+
# Raises a RuntimeError if an adapter can't be found.
|
40
|
+
def with_connection(options = {}, &block)
|
41
|
+
connection = new_connection(options)
|
42
|
+
connection.with_connection(&block)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Internal: Load an adapter by name
|
46
|
+
#
|
47
|
+
# Returns the adapter or nil if it can't find it
|
48
|
+
def load_adapter(name)
|
49
|
+
ADAPTERS.each do |a|
|
50
|
+
if a.to_s == name.to_s
|
51
|
+
require_relative "message_queue/adapters/#{name}"
|
52
|
+
klass_name = klass_name_for(name)
|
53
|
+
return MessageQueue::Adapters.const_get(klass_name)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
nil
|
58
|
+
end
|
59
|
+
|
60
|
+
# Internal: Load a serializer by name
|
61
|
+
#
|
62
|
+
# Returns the serializer or nil if it can't find it
|
63
|
+
def load_serializer(name)
|
64
|
+
SERIALIZERS.each do |s|
|
65
|
+
if s.to_s == name.to_s
|
66
|
+
require_relative "message_queue/serializers/#{name}"
|
67
|
+
klass_name = klass_name_for(name)
|
68
|
+
return MessageQueue::Serializers.const_get(klass_name)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
nil
|
73
|
+
end
|
74
|
+
|
75
|
+
# Internal: Return the class name for name
|
76
|
+
#
|
77
|
+
# Returns the class name for specified string
|
78
|
+
def klass_name_for(name)
|
79
|
+
name.to_s.split("_").map(&:capitalize) * ""
|
80
|
+
end
|
5
81
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "bunny"
|
2
|
+
|
3
|
+
module MessageQueue
|
4
|
+
module Adapters
|
5
|
+
class Bunny < Adapter
|
6
|
+
# Public: Initialize a RabbitMQ connection.
|
7
|
+
#
|
8
|
+
# options - The Hash options used to initialize a connection.
|
9
|
+
# :uri - The String URI described in http://rubybunny.info/articles/connecting.html.
|
10
|
+
#
|
11
|
+
# Returns MessageQueue::Adapters::Bunny::Connection if the options are valid.
|
12
|
+
# Raises ArgumentError when connection URI schema is not amqp or amqps, or the path contains multiple segments.
|
13
|
+
def new_connection(serializer, options = {})
|
14
|
+
settings = options[:uri] ? AMQ::Settings.parse_amqp_url(options[:uri]).merge(options) : options
|
15
|
+
Connection.new(serializer, settings)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
require "message_queue/adapters/bunny/connection"
|
@@ -0,0 +1,64 @@
|
|
1
|
+
class MessageQueue::Adapters::Bunny::Connection
|
2
|
+
attr_reader :serializer, :settings, :connection
|
3
|
+
|
4
|
+
# Public: Initialize a new Bunny connection.
|
5
|
+
#
|
6
|
+
# serializer - The Serializer for dumping and loading payload.
|
7
|
+
#
|
8
|
+
# settings - The Hash settings used to connect with Bunny.
|
9
|
+
# Details in http://rubybunny.info/articles/connecting.html.
|
10
|
+
#
|
11
|
+
# Returns a Connection wrapper for Bunny.
|
12
|
+
def initialize(serializer, settings)
|
13
|
+
@serializer = serializer
|
14
|
+
@settings = settings
|
15
|
+
end
|
16
|
+
|
17
|
+
# Public: Connect to RabbitMQ
|
18
|
+
#
|
19
|
+
# Returns the Bunny instance
|
20
|
+
def connect
|
21
|
+
@connection ||= begin
|
22
|
+
bunny = ::Bunny.new(settings)
|
23
|
+
bunny.start
|
24
|
+
bunny
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Public: Disconnect from RabbitMQ
|
29
|
+
#
|
30
|
+
# Returns nothing
|
31
|
+
def disconnect
|
32
|
+
if @connection
|
33
|
+
@connection.close if @connection.open?
|
34
|
+
@connection = nil
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Public: Connect to RabbitMQ, execute the block and disconnect
|
39
|
+
#
|
40
|
+
# Returns nothing
|
41
|
+
def with_connection(&block)
|
42
|
+
begin
|
43
|
+
connect
|
44
|
+
block.call(self)
|
45
|
+
ensure
|
46
|
+
disconnect
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def new_publisher(options)
|
51
|
+
raise "No connection to RabbitMQ" unless connection
|
52
|
+
|
53
|
+
Publisher.new(self, options)
|
54
|
+
end
|
55
|
+
|
56
|
+
def new_consumer(options)
|
57
|
+
raise "No connection to RabbitMQ" unless connection
|
58
|
+
|
59
|
+
Consumer.new(self, options)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
require "message_queue/adapters/bunny/publisher"
|
64
|
+
require "message_queue/adapters/bunny/consumer"
|
@@ -0,0 +1,61 @@
|
|
1
|
+
class MessageQueue::Adapters::Bunny::Connection::Consumer
|
2
|
+
attr_reader :connection
|
3
|
+
attr_reader :queue_options, :queue_name
|
4
|
+
attr_reader :exchange_options, :exchange_name, :exchange_routing_key
|
5
|
+
attr_reader :subscribe_options
|
6
|
+
|
7
|
+
# Public: Initialize a new Bunny consumer.
|
8
|
+
#
|
9
|
+
# connection - The Bunny Connection.
|
10
|
+
# options - The Hash options used to initialize the exchange
|
11
|
+
# of a consumer:
|
12
|
+
# :queue -
|
13
|
+
# :name - The String queue name.
|
14
|
+
# :durable - The Boolean queue durability.
|
15
|
+
# :exchange -
|
16
|
+
# :name - The String exchange name.
|
17
|
+
# :routing_key - The String exchange routing key.
|
18
|
+
# :subscribe -
|
19
|
+
# :ack - The Boolean indicate if it acks.
|
20
|
+
# :block - The Boolean indicate if it blocks.
|
21
|
+
# Detailed options see
|
22
|
+
# https://github.com/ruby-amqp/bunny/blob/master/lib/bunny/queue.rb
|
23
|
+
# and
|
24
|
+
# https://github.com/ruby-amqp/bunny/blob/master/lib/bunny/exchange.rb.
|
25
|
+
#
|
26
|
+
# Returns a Consumer.
|
27
|
+
def initialize(connection, options = {})
|
28
|
+
@connection = connection
|
29
|
+
|
30
|
+
options = options.dup
|
31
|
+
|
32
|
+
@queue_options = options.fetch(:queue)
|
33
|
+
@queue_name = queue_options.delete(:name) || (raise "Missing queue name")
|
34
|
+
|
35
|
+
@exchange_options = options.fetch(:exchange)
|
36
|
+
@exchange_name = exchange_options.delete(:name) || (raise "Missing exchange name")
|
37
|
+
@exchange_routing_key = exchange_options.delete(:routing_key) || queue_name
|
38
|
+
|
39
|
+
@subscribe_options = options.fetch(:subscribe, {})
|
40
|
+
end
|
41
|
+
|
42
|
+
def subscribe(options = {}, &block)
|
43
|
+
@subscription = queue.subscribe(subscribe_options.merge(options)) do |delivery_info, metadata, payload|
|
44
|
+
block.call(delivery_info, metadata, connection.serializer.load(payload))
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def unsubscribe
|
49
|
+
@subscription.cancel if @subscription
|
50
|
+
end
|
51
|
+
|
52
|
+
def queue
|
53
|
+
@queue ||= channel.queue(queue_name, queue_options).bind(exchange_name, :routing_key => exchange_routing_key)
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def channel
|
59
|
+
connection.connection.create_channel
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
class MessageQueue::Adapters::Bunny::Connection::Publisher
|
2
|
+
attr_reader :connection, :exchange
|
3
|
+
attr_reader :exchange_options, :exchange_name, :exchange_type
|
4
|
+
attr_reader :message_options
|
5
|
+
|
6
|
+
# Public: Initialize a new Bunny publisher.
|
7
|
+
#
|
8
|
+
# connection - The Bunny Connection.
|
9
|
+
# options - The Hash options used to initialize the exchange
|
10
|
+
# of a publisher:
|
11
|
+
# :exchange -
|
12
|
+
# :name - The String exchange name.
|
13
|
+
# :type - The Symbol exchange type.
|
14
|
+
# :durable - The Boolean exchange durability.
|
15
|
+
# :message -
|
16
|
+
# :routing_key - The String message routing key.
|
17
|
+
# :persistent - The Boolean indicate if the
|
18
|
+
# message persisted to disk .
|
19
|
+
# Detailed options see
|
20
|
+
# https://github.com/ruby-amqp/bunny/blob/master/lib/bunny/exchange.rb.
|
21
|
+
#
|
22
|
+
# Returns a Publisher.
|
23
|
+
def initialize(connection, options = {})
|
24
|
+
@connection = connection
|
25
|
+
|
26
|
+
options = options.dup
|
27
|
+
|
28
|
+
@exchange_options = options.fetch(:exchange)
|
29
|
+
@exchange_name = exchange_options.delete(:name) || (raise "Missing exchange name")
|
30
|
+
@exchange_type = exchange_options.delete(:type) || (raise "Missing exchange type")
|
31
|
+
|
32
|
+
@message_options = options.fetch(:message)
|
33
|
+
|
34
|
+
@exchange = connection.connection.default_channel.send(exchange_type, exchange_name, exchange_options)
|
35
|
+
end
|
36
|
+
|
37
|
+
def publish(payload, options = {})
|
38
|
+
exchange.publish(connection.serializer.dump(payload), message_options.merge(options))
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "singleton"
|
2
|
+
|
3
|
+
module MessageQueue
|
4
|
+
module Serializers
|
5
|
+
class Serializer
|
6
|
+
include Singleton
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def load(string, options = {})
|
10
|
+
instance.load(string, options)
|
11
|
+
end
|
12
|
+
|
13
|
+
def dump(object, options = {})
|
14
|
+
instance.dump(object, options)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "msgpack"
|
2
|
+
|
3
|
+
module MessageQueue
|
4
|
+
module Serializers
|
5
|
+
class MessagePack < Serializer
|
6
|
+
def load(string, options = {})
|
7
|
+
::MessagePack.unpack(string)
|
8
|
+
end
|
9
|
+
|
10
|
+
def dump(object, options = {})
|
11
|
+
::MessagePack.pack(object)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/message_queue.gemspec
CHANGED
@@ -8,9 +8,9 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = MessageQueue::VERSION
|
9
9
|
spec.authors = ["Jingwen Owen Ou"]
|
10
10
|
spec.email = ["jingweno@gmail.com"]
|
11
|
-
spec.description = %q{A
|
12
|
-
spec.summary = %q{A
|
13
|
-
spec.homepage = ""
|
11
|
+
spec.description = %q{A common interface to multiple message queues libraries.}
|
12
|
+
spec.summary = %q{A common interface to multiple message queues libraries.}
|
13
|
+
spec.homepage = "https://github.com/jingweno/message_queue"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
@@ -18,6 +18,5 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_development_dependency "bundler", "~> 1.3"
|
22
21
|
spec.add_development_dependency "rake"
|
23
22
|
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require_relative "../test_helper"
|
2
|
+
require_relative "../../lib/message_queue/serializers/plain"
|
3
|
+
require_relative "../../lib/message_queue/adapters/bunny"
|
4
|
+
|
5
|
+
class BunnyTest < Test::Unit::TestCase
|
6
|
+
def test_new_connection
|
7
|
+
connection = MessageQueue::Adapters::Bunny.new_connection(
|
8
|
+
MessageQueue::Serializers::Plain,
|
9
|
+
:uri => "amqp://user:pass@host/vhost",
|
10
|
+
:tls_certificates => ["path"])
|
11
|
+
assert_equal "amqp", connection.settings[:scheme]
|
12
|
+
assert_equal ["path"], connection.settings[:tls_certificates]
|
13
|
+
|
14
|
+
connection = MessageQueue::Adapters::Bunny.new_connection MessageQueue::Serializers::Plain
|
15
|
+
bunny = connection.connect
|
16
|
+
assert bunny.open?
|
17
|
+
|
18
|
+
connection.disconnect
|
19
|
+
assert bunny.closed?
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_new_publisher
|
23
|
+
connection = MessageQueue::Adapters::Bunny.new_connection MessageQueue::Serializers::Plain
|
24
|
+
connection.with_connection do |conn|
|
25
|
+
publisher = conn.new_publisher(
|
26
|
+
:exchange => {
|
27
|
+
:name => "test",
|
28
|
+
:type => :direct
|
29
|
+
},
|
30
|
+
:message => {
|
31
|
+
:routing_key => "test"
|
32
|
+
}
|
33
|
+
)
|
34
|
+
|
35
|
+
assert_equal "test", publisher.exchange_name
|
36
|
+
assert_equal :direct, publisher.exchange_type
|
37
|
+
assert_equal "test", publisher.message_options[:routing_key]
|
38
|
+
|
39
|
+
msg = Time.now.to_s
|
40
|
+
publisher.publish msg
|
41
|
+
|
42
|
+
ch = connection.connection.create_channel
|
43
|
+
queue = ch.queue("test")
|
44
|
+
_, _, m = queue.pop
|
45
|
+
|
46
|
+
assert_equal msg, m
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_new_consumer
|
51
|
+
connection = MessageQueue::Adapters::Bunny.new_connection MessageQueue::Serializers::Plain
|
52
|
+
connection.with_connection do |conn|
|
53
|
+
consumer = conn.new_consumer(
|
54
|
+
:queue => {
|
55
|
+
:name => "test"
|
56
|
+
},
|
57
|
+
:exchange => {
|
58
|
+
:name => "test"
|
59
|
+
}
|
60
|
+
)
|
61
|
+
|
62
|
+
assert_equal "test", consumer.queue_name
|
63
|
+
assert_equal "test", consumer.exchange_name
|
64
|
+
|
65
|
+
publisher = conn.new_publisher(
|
66
|
+
:exchange => {
|
67
|
+
:name => "test",
|
68
|
+
:type => :direct
|
69
|
+
},
|
70
|
+
:message => {
|
71
|
+
:routing_key => "test"
|
72
|
+
}
|
73
|
+
)
|
74
|
+
|
75
|
+
msg = Time.now.to_s
|
76
|
+
publisher.publish msg
|
77
|
+
|
78
|
+
_, _, m = consumer.queue.pop
|
79
|
+
assert_equal msg, m
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
|
3
|
+
class MessageQueueTest < Test::Unit::TestCase
|
4
|
+
def test_load_adapter
|
5
|
+
adapter = MessageQueue.load_adapter(:bunny)
|
6
|
+
assert_equal "MessageQueue::Adapters::Bunny", adapter.name
|
7
|
+
|
8
|
+
adapter = MessageQueue.load_adapter(:foo)
|
9
|
+
assert_nil adapter
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_load_serializer
|
13
|
+
serializer = MessageQueue.load_serializer(:message_pack)
|
14
|
+
assert_equal "MessageQueue::Serializers::MessagePack", serializer.name
|
15
|
+
|
16
|
+
serializer = MessageQueue.load_serializer(:foo)
|
17
|
+
assert_nil serializer
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_new_connection
|
21
|
+
assert_raises RuntimeError do
|
22
|
+
MessageQueue.new_connection(:adapter => :foo)
|
23
|
+
end
|
24
|
+
|
25
|
+
connection = MessageQueue.new_connection(:adapter => :bunny,
|
26
|
+
:serializer => :message_pack,
|
27
|
+
:uri => "amqp://user:pass@host/vhost")
|
28
|
+
assert_equal "MessageQueue::Adapters::Bunny::Connection", connection.class.to_s
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require_relative "../test_helper"
|
2
|
+
require_relative "../../lib/message_queue/serializers/message_pack"
|
3
|
+
|
4
|
+
class MessagePackTest < Test::Unit::TestCase
|
5
|
+
def test_dump_and_load
|
6
|
+
dump = MessageQueue::Serializers::MessagePack.dump "foo" => "bar"
|
7
|
+
loaded = MessageQueue::Serializers::MessagePack.load dump
|
8
|
+
assert_equal "bar", loaded["foo"]
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require_relative "../test_helper"
|
2
|
+
require_relative "../../lib/message_queue/serializers/plain"
|
3
|
+
|
4
|
+
class PlainTest < Test::Unit::TestCase
|
5
|
+
def test_dump_and_load
|
6
|
+
dump = MessageQueue::Serializers::Plain.dump "foo" => "bar"
|
7
|
+
loaded = MessageQueue::Serializers::Plain.load dump
|
8
|
+
assert_equal "bar", loaded["foo"]
|
9
|
+
end
|
10
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: message_queue
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jingwen Owen Ou
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-09-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: bundler
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ~>
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '1.3'
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ~>
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '1.3'
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: rake
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,7 +24,7 @@ dependencies:
|
|
38
24
|
- - '>='
|
39
25
|
- !ruby/object:Gem::Version
|
40
26
|
version: '0'
|
41
|
-
description: A
|
27
|
+
description: A common interface to multiple message queues libraries.
|
42
28
|
email:
|
43
29
|
- jingweno@gmail.com
|
44
30
|
executables: []
|
@@ -50,10 +36,26 @@ files:
|
|
50
36
|
- LICENSE.txt
|
51
37
|
- README.md
|
52
38
|
- Rakefile
|
39
|
+
- examples/consumer.rb
|
40
|
+
- examples/publisher.rb
|
53
41
|
- lib/message_queue.rb
|
42
|
+
- lib/message_queue/adapter.rb
|
43
|
+
- lib/message_queue/adapters/bunny.rb
|
44
|
+
- lib/message_queue/adapters/bunny/connection.rb
|
45
|
+
- lib/message_queue/adapters/bunny/consumer.rb
|
46
|
+
- lib/message_queue/adapters/bunny/publisher.rb
|
47
|
+
- lib/message_queue/serializer.rb
|
48
|
+
- lib/message_queue/serializers/message_pack.rb
|
49
|
+
- lib/message_queue/serializers/plain.rb
|
54
50
|
- lib/message_queue/version.rb
|
55
51
|
- message_queue.gemspec
|
56
|
-
|
52
|
+
- test/adapters/bunny_test.rb
|
53
|
+
- test/message_queue_test.rb
|
54
|
+
- test/serializers/message_pack_test.rb
|
55
|
+
- test/serializers/plain_test.rb
|
56
|
+
- test/support/message_queue.yml
|
57
|
+
- test/test_helper.rb
|
58
|
+
homepage: https://github.com/jingweno/message_queue
|
57
59
|
licenses:
|
58
60
|
- MIT
|
59
61
|
metadata: {}
|
@@ -76,5 +78,11 @@ rubyforge_project:
|
|
76
78
|
rubygems_version: 2.0.3
|
77
79
|
signing_key:
|
78
80
|
specification_version: 4
|
79
|
-
summary: A
|
80
|
-
test_files:
|
81
|
+
summary: A common interface to multiple message queues libraries.
|
82
|
+
test_files:
|
83
|
+
- test/adapters/bunny_test.rb
|
84
|
+
- test/message_queue_test.rb
|
85
|
+
- test/serializers/message_pack_test.rb
|
86
|
+
- test/serializers/plain_test.rb
|
87
|
+
- test/support/message_queue.yml
|
88
|
+
- test/test_helper.rb
|