mimi-messaging 0.1.7 → 0.1.8
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 +5 -5
- data/lib/mimi/messaging/mock.rb +12 -0
- data/lib/mimi/messaging/mock/connection.rb +153 -0
- data/lib/mimi/messaging/mock/request_processor.rb +92 -0
- data/lib/mimi/messaging/version.rb +1 -1
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 571b0eb49b07f329f5c1564a600eb56b833b94d7
|
4
|
+
data.tar.gz: 52e5a08ec30524364c9ed211f8093e23d5541e38
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 131f811a3bdf75e7c59ba1aeed43a04a7fbf77eb74a45615b89007761ffa8ef38fe4454146d97b96a9a0be67c40c0de45be1d7df9c3b7a504b168cf7ab63d63b
|
7
|
+
data.tar.gz: 00f4f9f5caf0fd17e85ef317ff93a1f0d8daf945abffc9238fa16ecda1d2e9965a773bc768c4edf06823f5922aa19e61937b955e9fe2b041556e69a531e4bc90
|
@@ -0,0 +1,12 @@
|
|
1
|
+
#
|
2
|
+
# Replaces real Connection and Channel with MockConnection and MockChannel
|
3
|
+
# that stubs connections to RabbitMQ.
|
4
|
+
#
|
5
|
+
# Replaces RequestProcessor with MockRequestProcessor
|
6
|
+
#
|
7
|
+
|
8
|
+
require_relative 'mock/connection'
|
9
|
+
require_relative 'mock/request_processor'
|
10
|
+
|
11
|
+
Mimi::Messaging.send :remove_const, :Connection
|
12
|
+
Mimi::Messaging::Connection = Mimi::Messaging::MockConnection
|
@@ -0,0 +1,153 @@
|
|
1
|
+
require 'bunny'
|
2
|
+
|
3
|
+
module Mimi
|
4
|
+
module Messaging
|
5
|
+
class MockConnection
|
6
|
+
attr_reader :queue_prefix
|
7
|
+
|
8
|
+
# Creates a Connection with given connection params
|
9
|
+
#
|
10
|
+
# @param params [Hash] Connection params as accepted by Bunny
|
11
|
+
# @param params[:queue_prefix] [String] (optional) Use this connection for all communication
|
12
|
+
# related to queues, having names starting with given
|
13
|
+
# prefix
|
14
|
+
#
|
15
|
+
def initialize(params = {})
|
16
|
+
@queue_prefix = params[:queue_prefix]
|
17
|
+
@channel_pool = {}
|
18
|
+
bunny_params = {
|
19
|
+
host: params[:mq_host],
|
20
|
+
port: params[:mq_port],
|
21
|
+
username: params[:mq_username],
|
22
|
+
password: params[:mq_password],
|
23
|
+
vhost: params[:mq_vhost]
|
24
|
+
}
|
25
|
+
@connection = { mock_connection: bunny_params }
|
26
|
+
end
|
27
|
+
|
28
|
+
# Starts the connection, opening actual connection to RabbitMQ
|
29
|
+
#
|
30
|
+
def start
|
31
|
+
@started = true
|
32
|
+
end
|
33
|
+
|
34
|
+
# Stops the connection
|
35
|
+
#
|
36
|
+
def stop
|
37
|
+
@channel_pool = {}
|
38
|
+
@started = false
|
39
|
+
end
|
40
|
+
|
41
|
+
def started?
|
42
|
+
@started
|
43
|
+
end
|
44
|
+
|
45
|
+
def channel
|
46
|
+
raise ConnectionError unless started?
|
47
|
+
@channel_pool[Thread.current.object_id] ||= create_channel
|
48
|
+
end
|
49
|
+
|
50
|
+
def create_channel(opts = {})
|
51
|
+
MockChannel.new(@connection, opts)
|
52
|
+
end
|
53
|
+
|
54
|
+
def reply_queue
|
55
|
+
raise ConnectionError unless started?
|
56
|
+
channel.reply_queue
|
57
|
+
end
|
58
|
+
|
59
|
+
def post(queue_name, raw_message, params = {})
|
60
|
+
channel.post(queue_name, raw_message, params)
|
61
|
+
end
|
62
|
+
|
63
|
+
def get(queue_name, raw_message, params = {})
|
64
|
+
channel.get(queue_name, raw_message, params)
|
65
|
+
end
|
66
|
+
|
67
|
+
def broadcast(queue_name, raw_message, params = {})
|
68
|
+
channel.broadcast(queue_name, raw_message, params)
|
69
|
+
end
|
70
|
+
|
71
|
+
class MockQueue
|
72
|
+
def initialize(*)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
class MockChannel
|
77
|
+
attr_reader :options, :connection
|
78
|
+
|
79
|
+
DEFAULT_OPTIONS = {
|
80
|
+
concurrency: 1
|
81
|
+
}
|
82
|
+
DEFAULT_GET_TIMEOUT = 60 # seconds
|
83
|
+
|
84
|
+
def initialize(connection, opts = {})
|
85
|
+
@connection = connection
|
86
|
+
@options = DEFAULT_OPTIONS.merge(opts)
|
87
|
+
@channel = { mock_channel: @connection, opts: options[:concurrency] }
|
88
|
+
@mutex = Mutex.new
|
89
|
+
end
|
90
|
+
|
91
|
+
def create_queue(name, opts = {})
|
92
|
+
MockQueue.new(name, opts)
|
93
|
+
end
|
94
|
+
|
95
|
+
def reply_queue
|
96
|
+
@reply_queue ||= create_queue('', exclusive: true)
|
97
|
+
end
|
98
|
+
|
99
|
+
def ack(tag)
|
100
|
+
raise "Not implemented"
|
101
|
+
end
|
102
|
+
|
103
|
+
def fanout(name)
|
104
|
+
raise "Not implemented"
|
105
|
+
end
|
106
|
+
|
107
|
+
def active?
|
108
|
+
true
|
109
|
+
end
|
110
|
+
|
111
|
+
# Sends a raw RabbitMQ message to a given direct exchange
|
112
|
+
#
|
113
|
+
# @param queue_name [String] Queue name to send the message to
|
114
|
+
# @param raw_message [String]
|
115
|
+
# @param params [Hash] Message params (metadata)
|
116
|
+
#
|
117
|
+
def post(queue_name, raw_message, params = {})
|
118
|
+
true
|
119
|
+
end
|
120
|
+
|
121
|
+
# Sends a raw RabbitMQ message to a given direct exchange and listens for response
|
122
|
+
#
|
123
|
+
# @param queue_name [String] Queue name to send the message to
|
124
|
+
# @param raw_message [String]
|
125
|
+
# @param params [Hash] Message params (metadata)
|
126
|
+
#
|
127
|
+
# @param params[:timeout] [Integer] (optional) Timeout in seconds
|
128
|
+
#
|
129
|
+
# @return [nil,Array]
|
130
|
+
#
|
131
|
+
def get(queue_name, raw_message, params = {})
|
132
|
+
nil
|
133
|
+
end
|
134
|
+
|
135
|
+
# Sends a raw RabbitMQ message to a given fanout exchange
|
136
|
+
#
|
137
|
+
# @param fanout_name [String] Fanout exchange name to send the message to
|
138
|
+
# @param raw_message [String]
|
139
|
+
# @param params [Hash] Message params (metadata)
|
140
|
+
#
|
141
|
+
def broadcast(fanout_name, raw_message, params = {})
|
142
|
+
true
|
143
|
+
end
|
144
|
+
|
145
|
+
private
|
146
|
+
|
147
|
+
def publish(exchange, raw_message, params = {})
|
148
|
+
true
|
149
|
+
end
|
150
|
+
end # class Channel
|
151
|
+
end # class Connection
|
152
|
+
end # module Messaging
|
153
|
+
end # module Mimi
|
@@ -0,0 +1,92 @@
|
|
1
|
+
module Mimi
|
2
|
+
module Messaging
|
3
|
+
class RequestProcessor
|
4
|
+
attr_reader :result
|
5
|
+
|
6
|
+
def self.started?
|
7
|
+
!@consumer.nil?
|
8
|
+
end
|
9
|
+
|
10
|
+
# Mock start
|
11
|
+
#
|
12
|
+
def self.start
|
13
|
+
return if abstract?
|
14
|
+
raise "#{name} already started" if started?
|
15
|
+
logger.debug "#{self} starting to serve '#{resource_name}' (#{exposed_methods})"
|
16
|
+
@queue = construct_queue
|
17
|
+
@consumer_mutex = Mutex.new
|
18
|
+
@consumer_mutex.synchronize do
|
19
|
+
@consumer = true
|
20
|
+
end
|
21
|
+
# consumer created, mutex released
|
22
|
+
end
|
23
|
+
|
24
|
+
# Mock stop
|
25
|
+
#
|
26
|
+
def self.stop
|
27
|
+
return if abstract?
|
28
|
+
raise "#{name} already stopped" unless started?
|
29
|
+
@consumer_mutex.synchronize do
|
30
|
+
@consumer = nil
|
31
|
+
end
|
32
|
+
@consumer = nil
|
33
|
+
@queue = nil
|
34
|
+
@channel = nil
|
35
|
+
@connection = nil
|
36
|
+
end
|
37
|
+
|
38
|
+
# MockRequestProcessor helper GET methods
|
39
|
+
#
|
40
|
+
# @param method_name [Symbol]
|
41
|
+
# @param message [Hash]
|
42
|
+
#
|
43
|
+
# @return [Mimi::Messaging::Message]
|
44
|
+
#
|
45
|
+
def self.get(method_name, message)
|
46
|
+
metadata = Mimi::Messaging::Message.new(
|
47
|
+
correlation_id: 1,
|
48
|
+
reply_to: 'mock_client',
|
49
|
+
headers: {
|
50
|
+
'method_name' => method_name.to_s,
|
51
|
+
'c' => 'mock-context'
|
52
|
+
}
|
53
|
+
)
|
54
|
+
d = Mimi::Messaging::Message.new()
|
55
|
+
raw_message = Mimi::Messaging::Message.new(message).to_msgpack
|
56
|
+
request_processor = new(d, metadata, raw_message)
|
57
|
+
request_processor.result
|
58
|
+
end
|
59
|
+
|
60
|
+
# MockRequestProcessor helper POST method
|
61
|
+
#
|
62
|
+
# @param method_name [Symbol]
|
63
|
+
# @param message [Hash]
|
64
|
+
#
|
65
|
+
# @return [Mimi::Messaging::Message]
|
66
|
+
#
|
67
|
+
def self.post(method_name, message)
|
68
|
+
metadata = Mimi::Messaging::Message.new(
|
69
|
+
headers: {
|
70
|
+
'method_name' => method_name.to_s,
|
71
|
+
'c' => 'context-id'
|
72
|
+
}
|
73
|
+
)
|
74
|
+
d = Mimi::Messaging::Message.new()
|
75
|
+
raw_message = Mimi::Messaging::Message.new(message)
|
76
|
+
request_processor = new(d, metadata, raw_message)
|
77
|
+
nil
|
78
|
+
end
|
79
|
+
|
80
|
+
# MockRequestProcessor helper BROADCAST method
|
81
|
+
#
|
82
|
+
# @param method_name [Symbol]
|
83
|
+
# @param message [Hash]
|
84
|
+
#
|
85
|
+
# @return [Mimi::Messaging::Message]
|
86
|
+
#
|
87
|
+
def self.broadcast(method_name, message)
|
88
|
+
post(method_name, message)
|
89
|
+
end
|
90
|
+
end # class RequestProcessor
|
91
|
+
end # module Messaging
|
92
|
+
end # module Mimi
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mimi-messaging
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Kukushkin
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-05-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bunny
|
@@ -150,6 +150,9 @@ files:
|
|
150
150
|
- lib/mimi/messaging/errors.rb
|
151
151
|
- lib/mimi/messaging/listener.rb
|
152
152
|
- lib/mimi/messaging/message.rb
|
153
|
+
- lib/mimi/messaging/mock.rb
|
154
|
+
- lib/mimi/messaging/mock/connection.rb
|
155
|
+
- lib/mimi/messaging/mock/request_processor.rb
|
153
156
|
- lib/mimi/messaging/model.rb
|
154
157
|
- lib/mimi/messaging/model_provider.rb
|
155
158
|
- lib/mimi/messaging/msgpack/msgpack_ext.rb
|
@@ -185,7 +188,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
185
188
|
version: '0'
|
186
189
|
requirements: []
|
187
190
|
rubyforge_project:
|
188
|
-
rubygems_version: 2.
|
191
|
+
rubygems_version: 2.6.14.1
|
189
192
|
signing_key:
|
190
193
|
specification_version: 4
|
191
194
|
summary: Communications via RabbitMQ for mimi
|