liebre 0.1.5 → 0.1.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +3 -3
- data/lib/liebre/connection_manager.rb +34 -12
- data/lib/liebre/publisher.rb +29 -19
- data/lib/liebre/runner.rb +5 -5
- data/lib/liebre/runner/starter/consumer.rb +13 -13
- data/lib/liebre/runner/starter/consumer/handler.rb +7 -7
- data/lib/liebre/runner/starter/resources.rb +9 -3
- data/lib/liebre/runner/starter/resources/queue_builder.rb +10 -4
- data/lib/liebre/version.rb +3 -3
- data/spec/config/liebre.yml +4 -3
- data/spec/integration_spec.rb +29 -26
- data/spec/liebre/config_spec.rb +14 -13
- data/spec/liebre/connection_manager_spec.rb +5 -1
- data/spec/liebre/publisher_spec.rb +12 -7
- data/spec/liebre/runner_spec.rb +11 -7
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 359dbb271508fe21b7d58c5c0a5aa04d98821b98
|
4
|
+
data.tar.gz: 792514c9a04576605d85e66b2cf294b3619eb8de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f182c3482cfe14d0022065449731fd17cdffc20285a59828a0a4928133d84ace37aa1cfe724513519a4016ff226f443d3caf39376a23a53e172d5b157ed05fb6
|
7
|
+
data.tar.gz: bacd99b79ca2d22c1ae19e207bdef34906993f4393cd9a186aec5503b6b1bfd042edfd039226fb023afcf4aaaea2f0a4f51ed990e15e6ef869a0f4d97a3f3041
|
data/Gemfile.lock
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
liebre (0.1.
|
4
|
+
liebre (0.1.11)
|
5
5
|
bunny (~> 2.5, >= 2.5.1)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
10
|
amq-protocol (2.0.1)
|
11
|
-
bunny (2.
|
11
|
+
bunny (2.6.3)
|
12
12
|
amq-protocol (>= 2.0.1)
|
13
13
|
coderay (1.1.1)
|
14
14
|
diff-lcs (1.2.5)
|
@@ -44,4 +44,4 @@ DEPENDENCIES
|
|
44
44
|
rspec
|
45
45
|
|
46
46
|
BUNDLED WITH
|
47
|
-
1.
|
47
|
+
1.13.6
|
@@ -1,21 +1,33 @@
|
|
1
1
|
require "yaml"
|
2
2
|
require "bunny"
|
3
|
+
require "singleton"
|
3
4
|
|
4
5
|
module Liebre
|
5
6
|
class ConnectionManager
|
6
7
|
|
7
|
-
|
8
|
-
|
8
|
+
include Singleton
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@path = Liebre::Config.connection_path
|
9
12
|
@connections = {}
|
13
|
+
@channels = {}
|
14
|
+
@lock = Mutex.new
|
10
15
|
end
|
11
16
|
|
12
17
|
def start
|
13
|
-
|
14
|
-
|
15
|
-
bunny
|
18
|
+
@lock.synchronize do
|
19
|
+
initialize_connections
|
20
|
+
connections.each do |connection_name, bunny|
|
21
|
+
begin
|
22
|
+
bunny.start
|
23
|
+
rescue => e
|
24
|
+
$logger.error("#{self.class.name}: Can't connect to #{connection_name} instance")
|
25
|
+
$logger.error(e.message + "\n" + e.backtrace.join("\n"))
|
26
|
+
end
|
27
|
+
end
|
16
28
|
end
|
17
29
|
end
|
18
|
-
|
30
|
+
|
19
31
|
def ensure_started
|
20
32
|
all_open = !@connections.empty? and @connections.all? do |_, bunny|
|
21
33
|
bunny.open?
|
@@ -32,17 +44,27 @@ module Liebre
|
|
32
44
|
connections[connection_name.to_sym]
|
33
45
|
end
|
34
46
|
|
47
|
+
def channel_for connection_name, consumer_pool_size = 1
|
48
|
+
@lock.synchronize do
|
49
|
+
channels[connection_name] ||= begin
|
50
|
+
get(connection_name).create_channel nil, consumer_pool_size
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
35
55
|
def stop
|
36
|
-
|
37
|
-
|
38
|
-
bunny.
|
56
|
+
@lock.synchronize do
|
57
|
+
connections.each do |_, bunny|
|
58
|
+
if bunny and bunny.open?
|
59
|
+
bunny.close
|
60
|
+
end
|
39
61
|
end
|
62
|
+
connections.clear
|
40
63
|
end
|
41
|
-
connections.clear
|
42
64
|
end
|
43
65
|
|
44
66
|
private
|
45
|
-
|
67
|
+
|
46
68
|
def initialize_connections
|
47
69
|
config.each do |name, conf|
|
48
70
|
@connections[name.to_sym] = connection_for(conf)
|
@@ -58,7 +80,7 @@ module Liebre
|
|
58
80
|
Liebre.env ? result.fetch(Liebre.env) : result
|
59
81
|
end
|
60
82
|
|
61
|
-
attr_reader :path, :connections
|
83
|
+
attr_reader :path, :connections, :channels
|
62
84
|
|
63
85
|
end
|
64
86
|
end
|
data/lib/liebre/publisher.rb
CHANGED
@@ -7,16 +7,17 @@ module Liebre
|
|
7
7
|
|
8
8
|
def enqueue message, options = {}
|
9
9
|
with_connection do
|
10
|
-
|
10
|
+
exchange = exchange_for default_channel
|
11
|
+
logger.debug "Liebre: Publishing '#{message}' with '#{options}' to exchange: #{exchange.name}"
|
11
12
|
exchange.publish message, options
|
12
13
|
end
|
13
14
|
end
|
14
15
|
|
15
16
|
def enqueue_and_wait message, options = {}
|
16
17
|
result = nil
|
17
|
-
|
18
|
+
with_rpc_channel do |channel|
|
18
19
|
correlation_id = options[:correlation_id] ||= generate_uuid
|
19
|
-
reply_queue = reply_queue correlation_id
|
20
|
+
reply_queue = reply_queue channel, correlation_id
|
20
21
|
options[:reply_to] = reply_queue.name
|
21
22
|
reply_queue.subscribe(:block => false) do |delivery_info, meta, payload|
|
22
23
|
if meta[:correlation_id] == correlation_id
|
@@ -25,7 +26,8 @@ module Liebre
|
|
25
26
|
channel.consumers[delivery_info.consumer_tag].cancel
|
26
27
|
end
|
27
28
|
end
|
28
|
-
|
29
|
+
exchange = exchange_for channel
|
30
|
+
logger.debug "Liebre: Publishing '#{message}' with '#{options}' to exchange: #{exchange.name}"
|
29
31
|
exchange.publish message, options
|
30
32
|
begin
|
31
33
|
Timeout.timeout(Liebre.config.rpc_request_timeout) do
|
@@ -33,12 +35,6 @@ module Liebre
|
|
33
35
|
end
|
34
36
|
rescue Timeout::Error
|
35
37
|
#do nothing
|
36
|
-
ensure
|
37
|
-
begin
|
38
|
-
reply_queue.delete
|
39
|
-
rescue Timeout::Error
|
40
|
-
logger.error "error while trying to delete RPC exclusive queue"
|
41
|
-
end
|
42
38
|
end
|
43
39
|
end
|
44
40
|
result
|
@@ -50,20 +46,34 @@ module Liebre
|
|
50
46
|
|
51
47
|
def with_connection
|
52
48
|
connection_manager.ensure_started
|
53
|
-
|
49
|
+
begin
|
50
|
+
yield
|
51
|
+
rescue Bunny::Exception => e
|
52
|
+
logger.warn("#{self.class.name}: #{e.class} found restarting connection - #{e.message}")
|
53
|
+
connection_manager.restart
|
54
|
+
retry
|
55
|
+
end
|
54
56
|
end
|
55
57
|
|
56
|
-
def
|
57
|
-
|
58
|
-
|
58
|
+
def with_rpc_channel
|
59
|
+
with_connection do
|
60
|
+
channel = connection_manager.get(connection_name).create_channel
|
61
|
+
yield(channel)
|
62
|
+
channel.close
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def default_channel
|
67
|
+
@default_channel ||= connection_manager.channel_for(connection_name)
|
59
68
|
end
|
60
69
|
|
61
|
-
def
|
62
|
-
|
70
|
+
def reply_queue channel, correlation_id
|
71
|
+
queue_name = "#{publisher_name}_callback_#{correlation_id}"
|
72
|
+
channel.queue queue_name, :exclusive => true, :auto_delete => true
|
63
73
|
end
|
64
74
|
|
65
|
-
def channel
|
66
|
-
|
75
|
+
def exchange_for channel
|
76
|
+
Liebre::Common::Utils.create_exchange channel, exchange_config
|
67
77
|
end
|
68
78
|
|
69
79
|
def publishers
|
@@ -83,7 +93,7 @@ module Liebre
|
|
83
93
|
end
|
84
94
|
|
85
95
|
def connection_manager
|
86
|
-
@connection_manager ||= ConnectionManager.
|
96
|
+
@connection_manager ||= ConnectionManager.instance
|
87
97
|
end
|
88
98
|
|
89
99
|
def generate_uuid
|
data/lib/liebre/runner.rb
CHANGED
@@ -12,7 +12,7 @@ module Liebre
|
|
12
12
|
|
13
13
|
def start
|
14
14
|
setup_shutdown
|
15
|
-
|
15
|
+
connection_manager.restart
|
16
16
|
start_consumers
|
17
17
|
sleep
|
18
18
|
rescue StandardError => e
|
@@ -29,13 +29,13 @@ module Liebre
|
|
29
29
|
def do_shutdown
|
30
30
|
Thread.start do
|
31
31
|
logger.info("Liebre: Closing AMQP connection...")
|
32
|
-
|
32
|
+
connection_manager.stop
|
33
33
|
logger.info("Liebre: AMQP connection closed")
|
34
34
|
end.join
|
35
35
|
end
|
36
36
|
|
37
37
|
def start_consumers
|
38
|
-
consumers = Consumers.new(
|
38
|
+
consumers = Consumers.new(connection_manager)
|
39
39
|
consumers.start_all
|
40
40
|
end
|
41
41
|
|
@@ -49,8 +49,8 @@ module Liebre
|
|
49
49
|
Liebre.logger
|
50
50
|
end
|
51
51
|
|
52
|
-
def
|
53
|
-
@
|
52
|
+
def connection_manager
|
53
|
+
@connection_manager ||= ConnectionManager.instance
|
54
54
|
end
|
55
55
|
|
56
56
|
attr_reader :retry_interval
|
@@ -2,7 +2,7 @@ module Liebre
|
|
2
2
|
class Runner
|
3
3
|
class Starter
|
4
4
|
class Consumer
|
5
|
-
|
5
|
+
|
6
6
|
autoload :Handler, "liebre/runner/starter/consumer/handler"
|
7
7
|
|
8
8
|
def initialize connection, config
|
@@ -14,9 +14,9 @@ module Liebre
|
|
14
14
|
initialize_error_queue
|
15
15
|
initialize_queue
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
private
|
19
|
-
|
19
|
+
|
20
20
|
def initialize_queue
|
21
21
|
queue.subscribe(:manual_ack => true) do |info, meta, payload|
|
22
22
|
response = :reject
|
@@ -34,19 +34,19 @@ module Liebre
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
def initialize_error_queue
|
39
39
|
Resources.new(connection, error_config).queue
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
def klass
|
43
43
|
@klass ||= Kernel.const_get config.fetch("class_name")
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
def handler
|
47
47
|
@handler ||= Handler.new(channel)
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
def channel
|
51
51
|
resources.channel
|
52
52
|
end
|
@@ -62,32 +62,32 @@ module Liebre
|
|
62
62
|
def resources
|
63
63
|
@resources ||= Resources.new(connection, parse_config)
|
64
64
|
end
|
65
|
-
|
65
|
+
|
66
66
|
def parse_config
|
67
67
|
result = clone_hash config
|
68
68
|
result['queue']['opts']['arguments'] ||= {}
|
69
69
|
result['queue']['opts']['arguments']['x-dead-letter-exchange'] = result['exchange']['name'] + "-error"
|
70
70
|
result
|
71
71
|
end
|
72
|
-
|
72
|
+
|
73
73
|
def error_config
|
74
74
|
result = clone_hash config
|
75
75
|
result['exchange']['name'] += "-error"
|
76
76
|
result['queue']['name'] += "-error"
|
77
77
|
result
|
78
78
|
end
|
79
|
-
|
79
|
+
|
80
80
|
def logger
|
81
81
|
Liebre::Config.logger
|
82
82
|
end
|
83
|
-
|
83
|
+
|
84
84
|
def clone_hash hash
|
85
85
|
Marshal.load(Marshal.dump(hash))
|
86
86
|
end
|
87
87
|
|
88
88
|
attr_reader :connection, :config
|
89
|
-
|
89
|
+
|
90
90
|
end
|
91
91
|
end
|
92
92
|
end
|
93
|
-
end
|
93
|
+
end
|
@@ -7,29 +7,29 @@ module Liebre
|
|
7
7
|
def initialize channel
|
8
8
|
@channel = channel
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
def respond action, meta
|
12
12
|
send(action, meta.delivery_tag)
|
13
13
|
end
|
14
14
|
|
15
15
|
private
|
16
|
-
|
16
|
+
|
17
17
|
def ack delivery_tag
|
18
18
|
channel.acknowledge delivery_tag
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
def reject delivery_tag
|
22
22
|
channel.reject delivery_tag, true
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
def error delivery_tag
|
26
|
-
channel.reject delivery_tag, false
|
26
|
+
channel.reject delivery_tag, false
|
27
27
|
end
|
28
28
|
|
29
29
|
attr_reader :channel
|
30
|
-
|
30
|
+
|
31
31
|
end
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
35
|
-
end
|
35
|
+
end
|
@@ -18,15 +18,21 @@ module Liebre
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def channel
|
21
|
-
@channel ||= connection.create_channel(nil, pool_size)
|
21
|
+
@channel ||= connection.create_channel(nil, pool_size).tap do |channel|
|
22
|
+
channel.prefetch(prefetch_count)
|
23
|
+
end
|
22
24
|
end
|
23
25
|
|
24
26
|
private
|
25
|
-
|
27
|
+
|
26
28
|
def queue_builder
|
27
29
|
@queue_bilder ||= QueueBuilder.new(channel, config)
|
28
30
|
end
|
29
31
|
|
32
|
+
def prefetch_count
|
33
|
+
config.fetch("prefetch_count", 10)
|
34
|
+
end
|
35
|
+
|
30
36
|
def pool_size
|
31
37
|
config.fetch("pool_size", 1)
|
32
38
|
end
|
@@ -36,4 +42,4 @@ module Liebre
|
|
36
42
|
end
|
37
43
|
end
|
38
44
|
end
|
39
|
-
end
|
45
|
+
end
|
@@ -11,8 +11,12 @@ module Liebre
|
|
11
11
|
|
12
12
|
def queue
|
13
13
|
q = channel.queue(queue_name, queue_opts)
|
14
|
-
routing_keys.
|
15
|
-
|
14
|
+
if routing_keys.any?
|
15
|
+
routing_keys.each do |key|
|
16
|
+
q.bind(exchange, bind_opts.merge(:routing_key => key))
|
17
|
+
end
|
18
|
+
else
|
19
|
+
q.bind(exchange, bind_opts)
|
16
20
|
end
|
17
21
|
q
|
18
22
|
end
|
@@ -40,8 +44,10 @@ module Liebre
|
|
40
44
|
end
|
41
45
|
|
42
46
|
def routing_keys
|
43
|
-
|
44
|
-
|
47
|
+
@routing_keys ||= begin
|
48
|
+
bind_opts[:routing_key] = [*bind_opts[:routing_key]]
|
49
|
+
bind_opts.delete :routing_key
|
50
|
+
end
|
45
51
|
end
|
46
52
|
|
47
53
|
def bind_opts
|
data/lib/liebre/version.rb
CHANGED
data/spec/config/liebre.yml
CHANGED
@@ -6,6 +6,7 @@ consumers:
|
|
6
6
|
rpc: false
|
7
7
|
pool_size: 1
|
8
8
|
num_threads: 3
|
9
|
+
prefetch_count: 5
|
9
10
|
exchange:
|
10
11
|
name: "consumer_exchange"
|
11
12
|
type: "fanout"
|
@@ -15,7 +16,7 @@ consumers:
|
|
15
16
|
name: "consumer_queue"
|
16
17
|
opts:
|
17
18
|
durable: false
|
18
|
-
|
19
|
+
|
19
20
|
some_rpc:
|
20
21
|
class_name: MyRPC
|
21
22
|
rpc: true
|
@@ -30,7 +31,7 @@ consumers:
|
|
30
31
|
name: "rpc_queue"
|
31
32
|
opts:
|
32
33
|
durable: false
|
33
|
-
|
34
|
+
|
34
35
|
publishers:
|
35
36
|
some_publisher:
|
36
37
|
exchange:
|
@@ -44,4 +45,4 @@ publishers:
|
|
44
45
|
name: "rpc_exchange"
|
45
46
|
type: "fanout"
|
46
47
|
opts:
|
47
|
-
durable: false
|
48
|
+
durable: false
|
data/spec/integration_spec.rb
CHANGED
@@ -1,73 +1,76 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
RSpec.describe "Integration" do
|
4
|
-
|
4
|
+
|
5
5
|
class MyConsumer
|
6
|
-
|
6
|
+
|
7
7
|
def initialize payload, meta
|
8
8
|
@payload = payload
|
9
9
|
@meta = meta
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
class MyRPC
|
15
|
-
|
15
|
+
|
16
16
|
def initialize payload, meta, callback
|
17
17
|
@payload = payload
|
18
18
|
@meta = meta
|
19
19
|
@callback = callback
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
def call
|
23
23
|
@callback.call(@payload)
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
let :config_path do
|
29
29
|
File.expand_path("../config/liebre.yml" ,__FILE__)
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
let :connection_path do
|
33
33
|
File.expand_path("../config/rabbitmq.yml" ,__FILE__)
|
34
34
|
end
|
35
|
-
|
35
|
+
|
36
36
|
before do
|
37
|
+
Liebre::ConnectionManager.instance_variable_set :@singleton__instance__, nil
|
37
38
|
Liebre::Config.config_path = config_path
|
38
39
|
Liebre::Config.connection_path = connection_path
|
39
40
|
end
|
40
|
-
|
41
|
+
|
41
42
|
let(:consumer) { double 'consumer' }
|
42
|
-
|
43
|
+
|
43
44
|
it do
|
44
|
-
|
45
|
+
|
45
46
|
main_thread = Thread.new do
|
46
47
|
server = Liebre::Runner.new
|
47
48
|
server.start
|
48
49
|
end
|
49
|
-
|
50
|
+
|
51
|
+
sleep 0.1
|
52
|
+
|
50
53
|
publisher = Liebre::Publisher.new("some_publisher")
|
51
|
-
|
54
|
+
|
52
55
|
allow(MyConsumer).to receive(:new).with("hello", anything).and_return consumer
|
53
|
-
|
56
|
+
|
54
57
|
#the consumer returns first :ack, then :reject and the message gets requed, then :error, and the message turns dead-lettered
|
55
58
|
expect(consumer).to receive(:call).and_return :ack, :reject, :error
|
56
|
-
|
59
|
+
|
57
60
|
publisher.enqueue "hello", :routing_key => "consumer_queue" #:ack
|
58
61
|
publisher.enqueue "hello", :routing_key => "consumer_queue" #:reject then :error
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
+
|
63
|
+
|
64
|
+
|
62
65
|
rpc_publisher = Liebre::Publisher.new("rpc_publisher")
|
63
|
-
|
66
|
+
|
64
67
|
param = "return this string"
|
65
|
-
|
68
|
+
|
66
69
|
result = rpc_publisher.rpc param, :routing_key => "rpc_queue"
|
67
|
-
|
70
|
+
|
68
71
|
expect(result).to eq param
|
69
|
-
|
72
|
+
|
70
73
|
sleep 0.1
|
71
74
|
end
|
72
|
-
|
73
|
-
end
|
75
|
+
|
76
|
+
end
|
data/spec/liebre/config_spec.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
RSpec.describe Liebre::Config do
|
4
|
-
|
4
|
+
|
5
5
|
let :config_path do
|
6
6
|
File.expand_path("../../config/liebre.yml" ,__FILE__)
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
let :connection_path do
|
10
10
|
File.expand_path("../../config/rabbitmq.yml" ,__FILE__)
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
before do
|
14
14
|
described_class.config_path = config_path
|
15
15
|
described_class.connection_path = connection_path
|
@@ -26,37 +26,38 @@ RSpec.describe Liebre::Config do
|
|
26
26
|
expect(described_class.env).to eq "some_env"
|
27
27
|
end
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
describe '#consumers' do
|
31
|
-
|
31
|
+
|
32
32
|
let(:consumer_names) { %w{ some_consumer some_rpc } }
|
33
|
-
|
33
|
+
|
34
34
|
let(:consumer_config) do
|
35
35
|
{
|
36
36
|
'class_name' => "MyConsumer",
|
37
37
|
'rpc' => false
|
38
38
|
}
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
let(:rpc_config) do
|
42
42
|
{
|
43
43
|
'class_name' => "MyRPC",
|
44
44
|
'rpc' => true
|
45
45
|
}
|
46
46
|
end
|
47
|
-
|
47
|
+
|
48
48
|
it do
|
49
|
-
|
49
|
+
|
50
50
|
expect(subject.consumers.keys).to eq consumer_names
|
51
|
-
|
51
|
+
|
52
52
|
expect(subject.consumers['some_consumer']['class_name']).to eq consumer_config['class_name']
|
53
53
|
expect(subject.consumers['some_consumer']['rpc']).to eq consumer_config['rpc']
|
54
|
-
|
54
|
+
expect(subject.consumers['some_consumer']['prefetch_count']).to eq 5
|
55
|
+
|
55
56
|
expect(subject.consumers['some_rpc']['class_name']).to eq rpc_config['class_name']
|
56
57
|
expect(subject.consumers['some_rpc']['rpc']).to eq rpc_config['rpc']
|
57
|
-
|
58
|
+
|
58
59
|
end
|
59
|
-
|
60
|
+
|
60
61
|
end
|
61
62
|
|
62
63
|
end
|
@@ -5,8 +5,12 @@ RSpec.describe Liebre::ConnectionManager do
|
|
5
5
|
let :connection_path do
|
6
6
|
File.expand_path("../../config/rabbitmq.yml" ,__FILE__)
|
7
7
|
end
|
8
|
+
|
9
|
+
before do
|
10
|
+
Liebre::Config.connection_path = connection_path
|
11
|
+
end
|
8
12
|
|
9
|
-
subject { described_class.
|
13
|
+
subject { described_class.instance }
|
10
14
|
|
11
15
|
describe '.start and .get' do
|
12
16
|
|
@@ -22,15 +22,14 @@ RSpec.describe Liebre::Publisher do
|
|
22
22
|
end
|
23
23
|
|
24
24
|
let(:channel) { double 'channel' }
|
25
|
-
let(:
|
26
|
-
let(:
|
27
|
-
let(:exchange) { double 'exchange' }
|
25
|
+
let(:connection_manager) { double 'connection_manager' }
|
26
|
+
let(:exchange) { double 'exchange', :name => "exchange" }
|
28
27
|
|
29
28
|
before do
|
30
29
|
allow_any_instance_of(Liebre::Config).to receive(:publishers).and_return publishers_config
|
31
30
|
allow_any_instance_of(Liebre::Config).to receive(:rpc_request_timeout).and_return 10
|
32
31
|
|
33
|
-
|
32
|
+
allow(Liebre::ConnectionManager).to receive(:instance).and_return connection_manager
|
34
33
|
expect(connection_manager).to receive(:ensure_started)
|
35
34
|
|
36
35
|
expect(Liebre::Common::Utils).to receive(:create_exchange).
|
@@ -41,6 +40,10 @@ RSpec.describe Liebre::Publisher do
|
|
41
40
|
subject { described_class.new 'test_publisher' }
|
42
41
|
|
43
42
|
describe "#enqueue" do
|
43
|
+
before do
|
44
|
+
expect(connection_manager).to receive(:channel_for).with(:default).and_return channel
|
45
|
+
end
|
46
|
+
|
44
47
|
it do
|
45
48
|
message = "abc"
|
46
49
|
expect(exchange).to receive(:publish).with message, {}
|
@@ -56,11 +59,15 @@ RSpec.describe Liebre::Publisher do
|
|
56
59
|
let(:reply_queue) { double "reply_queue", :name => reply_queue_name }
|
57
60
|
let(:delivery_info) { double "delivery_info", :consumer_tag => "tag"}
|
58
61
|
let(:consumer) { double 'consumer' }
|
62
|
+
let(:connection) { double 'connection' }
|
59
63
|
let(:consumers) { {"tag" => consumer} }
|
60
64
|
|
61
65
|
before do
|
62
|
-
expect(
|
66
|
+
expect(connection_manager).to receive(:get).with(:default).and_return connection
|
67
|
+
expect(connection).to receive(:create_channel).and_return channel
|
68
|
+
expect(channel).to receive(:queue).with(reply_queue_name, :exclusive => true, :auto_delete => true).
|
63
69
|
and_return reply_queue
|
70
|
+
expect(channel).to receive(:close)
|
64
71
|
end
|
65
72
|
|
66
73
|
it do
|
@@ -75,8 +82,6 @@ RSpec.describe Liebre::Publisher do
|
|
75
82
|
|
76
83
|
block.call(delivery_info, {:correlation_id => correlation_id}, answer)
|
77
84
|
end
|
78
|
-
|
79
|
-
expect(reply_queue).to receive(:delete)
|
80
85
|
|
81
86
|
result = subject.enqueue_and_wait message, :correlation_id => correlation_id
|
82
87
|
expect(result).to eq answer
|
data/spec/liebre/runner_spec.rb
CHANGED
@@ -1,32 +1,36 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
RSpec.describe Liebre::Runner do
|
4
|
-
|
4
|
+
|
5
|
+
let :connection_path do
|
6
|
+
File.expand_path("../../config/rabbitmq.yml" ,__FILE__)
|
7
|
+
end
|
5
8
|
|
6
9
|
let(:interval) { 1234 }
|
7
10
|
|
8
11
|
subject { described_class.new(interval) }
|
9
12
|
let(:logger) { double 'logger' }
|
10
13
|
|
11
|
-
let(:
|
14
|
+
let(:connection_manager) { double 'connection_manager'}
|
12
15
|
|
13
16
|
let(:consumers) { double 'consumers' }
|
14
17
|
|
15
18
|
before do
|
19
|
+
Liebre::Config.connection_path = connection_path
|
16
20
|
|
17
21
|
allow(subject).to receive(:logger).
|
18
22
|
and_return(logger)
|
19
23
|
|
20
|
-
allow(Liebre::ConnectionManager).to receive(:
|
21
|
-
and_return(
|
24
|
+
allow(Liebre::ConnectionManager).to receive(:instance).
|
25
|
+
and_return(connection_manager)
|
22
26
|
|
23
27
|
allow(described_class::Consumers).to receive(:new).
|
24
|
-
with(
|
28
|
+
with(connection_manager).and_return(consumers)
|
25
29
|
end
|
26
30
|
|
27
31
|
describe '#run' do
|
28
32
|
it 'logs and retries after fail' do
|
29
|
-
expect(
|
33
|
+
expect(connection_manager).to receive(:restart) do
|
30
34
|
raise "some error"
|
31
35
|
end
|
32
36
|
|
@@ -38,7 +42,7 @@ RSpec.describe Liebre::Runner do
|
|
38
42
|
expect(message).to match /Retrying/
|
39
43
|
end
|
40
44
|
|
41
|
-
expect(
|
45
|
+
expect(connection_manager).to receive(:restart)
|
42
46
|
expect(consumers ).to receive(:start_all)
|
43
47
|
|
44
48
|
expect(subject).to receive(:sleep)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: liebre
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jcabotc
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2017-01-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bunny
|