pwwka 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +44 -4
- data/lib/pwwka/test_handler.rb +57 -5
- data/lib/pwwka/version.rb +1 -1
- data/spec/transmitter_spec.rb +17 -8
- 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: d40cdf7ab32f4a3731ecba95fd75c16cca7c4c70
|
4
|
+
data.tar.gz: cfa26d01c9484c309b45d27f09c40a7390d1e10c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: addff24275e10692962c599fdb04bc5d9536e22abea010ba18d95f5b9dcb168215e376aae0c49408a8273e361066b8979db0b10100e317271292ba399387d090
|
7
|
+
data.tar.gz: f387dad318f3cf12262313ebfc7432cc8fe606d758c207f05453a32bcc4f9c7cd3f10c665bf72ae7e7951b3c6f61d8cbffc40f8e1b2d83f85b5d6b506e1e610a
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -39,11 +39,11 @@ And follow the instructions.
|
|
39
39
|
Add to your `Gemfile`:
|
40
40
|
|
41
41
|
```ruby
|
42
|
-
gem 'pwwka'
|
42
|
+
gem 'pwwka'
|
43
43
|
```
|
44
44
|
|
45
45
|
|
46
|
-
### Set up your
|
46
|
+
### Set up your pwwka configration
|
47
47
|
|
48
48
|
Connect to your RabbitMQ instance using the url and choose a name for your
|
49
49
|
topic exchange.
|
@@ -109,7 +109,7 @@ message_handler: rake message_handler:receive HANDLER_KLASS=ClientIndexMessageHa
|
|
109
109
|
You'll also need to bring the Rake task into your app. For Rails, you'll need to edit the top-level `Rakefile`:
|
110
110
|
|
111
111
|
```ruby
|
112
|
-
require '
|
112
|
+
require 'pwwka/tasks'
|
113
113
|
```
|
114
114
|
|
115
115
|
### Queues - what messages will your queue receive
|
@@ -177,7 +177,47 @@ RabbitMQ has a web interface for checking out the health of connections, channel
|
|
177
177
|
![RabbitMQ Management 3](docs/images/RabbitMQ_Management-3.png)
|
178
178
|
|
179
179
|
## Testing
|
180
|
-
|
180
|
+
|
181
|
+
This gem has test coverage of interacting with RabbitMQ, so for unit tests, your best
|
182
|
+
strategy is to simply mock calls to `Pwwka::Transmitter`.
|
183
|
+
|
184
|
+
For integration tests, however, you can examine the actual message bus by setting up
|
185
|
+
the provided `Pwwka::TestHandler` like so:
|
186
|
+
|
187
|
+
```ruby
|
188
|
+
require 'pwwka/test_handler'
|
189
|
+
|
190
|
+
describe "my integration test" do
|
191
|
+
|
192
|
+
before(:all) do
|
193
|
+
@test_handler = Pwwka::TestHandler.new
|
194
|
+
@test_handler.test_setup
|
195
|
+
end
|
196
|
+
|
197
|
+
after(:all) do
|
198
|
+
# this clears out any messages, so you have a clean test environment next time
|
199
|
+
@test_handler.test_teardown
|
200
|
+
end
|
201
|
+
|
202
|
+
it "uses the message bus" do
|
203
|
+
post "/items", item: { size: "L" }
|
204
|
+
|
205
|
+
message = @test_handler.pop_message
|
206
|
+
|
207
|
+
expect(message.delivery_info.routing_key).to eq("my-company.items.created")
|
208
|
+
expect(message.payload).to eq({ item: { id: 42, size: "L" } })
|
209
|
+
end
|
210
|
+
|
211
|
+
it "can splat the values as well" do
|
212
|
+
post "/items", item: { size: "L" }
|
213
|
+
|
214
|
+
delivery_info, payload = @test_handler.pop_message
|
215
|
+
|
216
|
+
expect(delivery_info.routing_key).to eq("my-company.items.created")
|
217
|
+
expect(payload).to eq({ item: { id: 42, size: "L" } })
|
218
|
+
end
|
219
|
+
end
|
220
|
+
```
|
181
221
|
|
182
222
|
## TODO
|
183
223
|
* automated monitoring
|
data/lib/pwwka/test_handler.rb
CHANGED
@@ -1,4 +1,12 @@
|
|
1
1
|
module Pwwka
|
2
|
+
# A handler you can use to examine messages your app sends during tests.
|
3
|
+
#
|
4
|
+
# To use this:
|
5
|
+
#
|
6
|
+
# 1. Create an instance and arrange for `test_setup` to be called when
|
7
|
+
# your tests are being setup (e.g.`def setup` or `before`)
|
8
|
+
# 2. Arrange for `test_teardown` to be called during teardown of your tests
|
9
|
+
# 3. Use the method `pop_message` to examine the message on the queue
|
2
10
|
class TestHandler
|
3
11
|
|
4
12
|
attr_reader :channel_connector
|
@@ -21,19 +29,35 @@ module Pwwka
|
|
21
29
|
def test_queue
|
22
30
|
@test_queue ||= begin
|
23
31
|
test_queue = channel.queue("test-queue", durable: true)
|
24
|
-
test_queue.bind(topic_exchange, routing_key: "
|
32
|
+
test_queue.bind(topic_exchange, routing_key: "#.#")
|
25
33
|
test_queue
|
26
34
|
end
|
27
35
|
end
|
28
36
|
|
37
|
+
# Get the message on the queue as TestHandler::Message.
|
38
|
+
def pop_message
|
39
|
+
delivery_info, properties, payload = test_queue.pop
|
40
|
+
Message.new(delivery_info,
|
41
|
+
properties,
|
42
|
+
payload)
|
43
|
+
end
|
44
|
+
|
29
45
|
def get_topic_message_payload_for_tests
|
30
|
-
|
31
|
-
|
46
|
+
deprecated!(:get_topic_message_payload_for_tests,
|
47
|
+
"Use `pop_message.payload` instead")
|
48
|
+
pop_message.payload
|
32
49
|
end
|
33
50
|
|
34
51
|
def get_topic_message_properties_for_tests
|
35
|
-
|
36
|
-
|
52
|
+
deprecated!(:get_topic_message_properties_for_tests,
|
53
|
+
"Use `pop_message.properties` instead")
|
54
|
+
pop_message.properties
|
55
|
+
end
|
56
|
+
|
57
|
+
def get_topic_message_delivery_info_for_tests
|
58
|
+
deprecated!(:get_topic_message_delivery_info_for_tests,
|
59
|
+
"Use `pop_message.delivery_info` instead")
|
60
|
+
pop_message.delivery_info
|
37
61
|
end
|
38
62
|
|
39
63
|
def purge_test_queue
|
@@ -46,5 +70,33 @@ module Pwwka
|
|
46
70
|
channel_connector.connection_close
|
47
71
|
end
|
48
72
|
|
73
|
+
# Simple class to hold a popped message.
|
74
|
+
#
|
75
|
+
# You can either access the message contents directly, or splat
|
76
|
+
# it for the most commonly-needed aspects:
|
77
|
+
#
|
78
|
+
# delivery_info, payload = @test_handler.pop_message
|
79
|
+
class Message
|
80
|
+
attr_reader :delivery_info, :properties, :payload
|
81
|
+
def initialize(delivery_info, properties, payload)
|
82
|
+
@delivery_info = delivery_info
|
83
|
+
@properties = properties
|
84
|
+
@raw_payload = payload
|
85
|
+
@payload = JSON.parse(@raw_payload)
|
86
|
+
end
|
87
|
+
|
88
|
+
# Returns the delivery_info, payload, properties, and raw_payload for splat
|
89
|
+
# magic.
|
90
|
+
def to_ary
|
91
|
+
[@delivery_info,@payload,@properties,@raw_payload]
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
private
|
96
|
+
|
97
|
+
def deprecated!(method,message)
|
98
|
+
warn "#{method} is deprecated: #{message}"
|
99
|
+
end
|
100
|
+
|
49
101
|
end
|
50
102
|
end
|
data/lib/pwwka/version.rb
CHANGED
data/spec/transmitter_spec.rb
CHANGED
@@ -10,15 +10,24 @@ describe Pwwka::Transmitter do
|
|
10
10
|
after(:all) { @test_handler.test_teardown }
|
11
11
|
|
12
12
|
let(:payload) { Hash[:this, "that"] }
|
13
|
-
let(:routing_key) { "this.that" }
|
13
|
+
let(:routing_key) { "this.that.and.theother" }
|
14
14
|
|
15
15
|
describe "#send_message!" do
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
17
|
+
context "happy path" do
|
18
|
+
it "should send the correct payload" do
|
19
|
+
success = Pwwka::Transmitter.new.send_message!(payload, routing_key)
|
20
|
+
expect(success).to be_truthy
|
21
|
+
received_payload = @test_handler.pop_message.payload
|
22
|
+
expect(received_payload["this"]).to eq("that")
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should delivery on the expected routing key" do
|
26
|
+
success = Pwwka::Transmitter.new.send_message!(payload, routing_key)
|
27
|
+
expect(success).to be_truthy
|
28
|
+
delivery_info = @test_handler.pop_message.delivery_info
|
29
|
+
expect(delivery_info.routing_key).to eq(routing_key)
|
30
|
+
end
|
22
31
|
end
|
23
32
|
|
24
33
|
it "should blow up if exception raised" do
|
@@ -34,7 +43,7 @@ describe Pwwka::Transmitter do
|
|
34
43
|
|
35
44
|
it "should send the correct payload" do
|
36
45
|
Pwwka::Transmitter.send_message!(payload, routing_key)
|
37
|
-
received_payload = @test_handler.
|
46
|
+
received_payload = @test_handler.pop_message.payload
|
38
47
|
expect(received_payload["this"]).to eq("that")
|
39
48
|
end
|
40
49
|
|
@@ -51,7 +60,7 @@ describe Pwwka::Transmitter do
|
|
51
60
|
|
52
61
|
it "should send the correct payload" do
|
53
62
|
Pwwka::Transmitter.send_message_safely(payload, routing_key)
|
54
|
-
received_payload = @test_handler.
|
63
|
+
received_payload = @test_handler.pop_message.payload
|
55
64
|
expect(received_payload["this"]).to eq("that")
|
56
65
|
end
|
57
66
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pwwka
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stitch Fix Engineering
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-09-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bunny
|