mercury_amqp 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2fd7b0dd970c7160af30546770764c5fc2725bc6
4
- data.tar.gz: a702ad714aba8bd83308c19905841f0cec380430
3
+ metadata.gz: 6ce3a4a09a924946d240cfbabeea944dffba4ec7
4
+ data.tar.gz: 772194f0c84be6d336fc16b2821f0e728e8b1f4b
5
5
  SHA512:
6
- metadata.gz: ef2d9c3392044008379014cfe5a12d22849c5f36a0852a3f36dccc6a6677f8b1d65f3c51143dacf9607077164abfb10874f369dc0f62246edc5a2b04fb5c757c
7
- data.tar.gz: 687e95ee42e0e32aaf9290669ed11ba2e68f3b1b1acf43195990158a2724e285f708028e1ab43d20cfd5d5953c5bd1fbae4a98c75c31a33bc34f0c2123d64c98
6
+ metadata.gz: ab8452d14bdb64949877c748bd8eafbe37c5bd5ff652772950025f63b70f3db9d0754397606a9d223292d33142c2561d88f410cf4b9c9dd7acc416e0a7b1290b
7
+ data.tar.gz: b63c33292ee6299d6b5832d2c1157fc6a229b281266e6fc7489dec4fed6f8126b4d023fce49abe2f40132816022c3411230d1ec078547b7fe910a8b8837a1db0
@@ -8,8 +8,7 @@ class Mercury
8
8
  attr_reader :amqp, :channel, :logger
9
9
 
10
10
  def self.open(logger: Logatron, **kws, &k)
11
- @logger = logger
12
- new(**kws, &k)
11
+ new(logger: logger, **kws, &k)
13
12
  nil
14
13
  end
15
14
 
@@ -27,7 +26,9 @@ class Mercury
27
26
  parallelism: 1,
28
27
  on_error: nil,
29
28
  wait_for_publisher_confirms: true,
29
+ logger: logger,
30
30
  &k)
31
+ @logger = logger
31
32
  @on_error = on_error
32
33
  AMQP.connect(host: host, port: port, vhost: vhost, username: username, password: password,
33
34
  on_tcp_connection_failure: server_down_error_handler) do |amqp|
@@ -226,7 +227,7 @@ class Mercury
226
227
  # failure handlers are invoked from EventMachine's `ensure`.)
227
228
  current_exception = $!
228
229
  unless current_exception
229
- Logatron.error(msg)
230
+ @logger.error(msg)
230
231
  if @on_error.respond_to?(:call)
231
232
  @on_error.call(msg)
232
233
  else
@@ -1,6 +1,6 @@
1
1
  class Mercury
2
2
  class ReceivedMessage
3
- attr_reader :content, :metadata
3
+ attr_reader :content, :metadata, :action_taken
4
4
 
5
5
  def initialize(content, metadata, is_ackable: false)
6
6
  @content = content
@@ -17,18 +17,28 @@ class Mercury
17
17
  end
18
18
 
19
19
  def ack
20
- @is_ackable or raise 'This message is not ackable'
20
+ performing_action(:ack)
21
21
  metadata.ack
22
22
  end
23
23
 
24
24
  def reject
25
- @is_ackable or raise 'This message is not rejectable'
25
+ performing_action(:reject)
26
26
  metadata.reject(requeue: false)
27
27
  end
28
28
 
29
29
  def nack
30
- @is_ackable or raise 'This message is not nackable'
30
+ performing_action(:nack)
31
31
  metadata.reject(requeue: true)
32
32
  end
33
+
34
+ private
35
+
36
+ def performing_action(action)
37
+ @is_ackable or raise "This message is not #{action}able"
38
+ if @action_taken
39
+ raise "This message was already #{@action_taken}ed"
40
+ end
41
+ @action_taken = action
42
+ end
33
43
  end
34
44
  end
@@ -1,3 +1,3 @@
1
1
  class Mercury
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -274,19 +274,14 @@ describe Mercury::Monadic do
274
274
  end
275
275
 
276
276
  it 'raises when an error occurs' do
277
- # verify it registers a handler
278
- expect_any_instance_of(AMQP::Channel).to receive(:on_error) {|&b| @handler = b}
279
-
280
- # verify the handler raises an error
281
277
  expect do
282
278
  em do
283
- Mercury.open do
284
- ch = double
285
- info = double(reply_code: 'code', reply_text: 'text')
286
- @handler.call(ch, info)
279
+ Mercury.open do |m|
280
+ ch = m.instance_variable_get(:@channel)
281
+ ch.acknowledge(42) # force a channel error
287
282
  end
288
283
  end
289
- end.to raise_error 'An error occurred: code - text'
284
+ end.to raise_error 'An error occurred: 406 - PRECONDITION_FAILED - unknown delivery tag 42'
290
285
  end
291
286
 
292
287
  describe '#delete_source' do
@@ -0,0 +1,72 @@
1
+ require 'rspec'
2
+ require 'mercury/received_message'
3
+
4
+ describe Mercury::ReceivedMessage do
5
+
6
+ describe '#ack' do
7
+ it 'raises an error if the message is not actionable' do
8
+ expect{make_non_actionable.ack}.to raise_error /not ackable/
9
+ end
10
+ it 'raises an error if an action was already taken' do
11
+ msg = make_actionable
12
+ msg.reject
13
+ expect { msg.ack }.to raise_error /already rejected/
14
+ end
15
+ end
16
+
17
+ describe '#nack' do
18
+ it 'raises an error if the message is not actionable' do
19
+ expect{make_non_actionable.nack}.to raise_error /not nackable/
20
+ end
21
+ it 'raises an error if an action was already taken' do
22
+ msg = make_actionable
23
+ msg.ack
24
+ expect { msg.nack }.to raise_error /already acked/
25
+ end
26
+ end
27
+
28
+ describe '#reject' do
29
+ it 'raises an error if the message is not actionable' do
30
+ expect{make_non_actionable.reject}.to raise_error /not rejectable/
31
+ end
32
+ it 'raises an error if an action was already taken' do
33
+ msg = make_actionable
34
+ msg.nack
35
+ expect { msg.reject }.to raise_error /already nacked/
36
+ end
37
+ end
38
+
39
+ describe '#action_taken' do
40
+ it 'returns the action taken' do
41
+ a = make_actionable
42
+ expect(a.action_taken).to eql nil
43
+
44
+ b = make_actionable
45
+ b.ack
46
+ expect(b.action_taken).to eql :ack
47
+
48
+ c = make_actionable
49
+ c.nack
50
+ expect(c.action_taken).to eql :nack
51
+
52
+ d = make_actionable
53
+ d.reject
54
+ expect(d.action_taken).to eql :reject
55
+ end
56
+ end
57
+
58
+ def make_actionable
59
+ Mercury::ReceivedMessage.new('hello', make_metadata, is_ackable: true)
60
+ end
61
+
62
+ def make_non_actionable
63
+ Mercury::ReceivedMessage.new('hello', make_metadata, is_ackable: false)
64
+ end
65
+
66
+ def make_metadata
67
+ m = double
68
+ allow(m).to receive(:ack)
69
+ allow(m).to receive(:reject)
70
+ m
71
+ end
72
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mercury_amqp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Winton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-20 00:00:00.000000000 Z
11
+ date: 2016-04-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -213,6 +213,7 @@ files:
213
213
  - spec/lib/mercury/cps_spec.rb
214
214
  - spec/lib/mercury/mercury_spec.rb
215
215
  - spec/lib/mercury/monadic_spec.rb
216
+ - spec/lib/mercury/received_message_spec.rb
216
217
  - spec/lib/mercury/sync_spec.rb
217
218
  - spec/lib/mercury/utils_spec.rb
218
219
  - spec/lib/mercury/wire_serializer_spec.rb
@@ -245,6 +246,7 @@ test_files:
245
246
  - spec/lib/mercury/cps_spec.rb
246
247
  - spec/lib/mercury/mercury_spec.rb
247
248
  - spec/lib/mercury/monadic_spec.rb
249
+ - spec/lib/mercury/received_message_spec.rb
248
250
  - spec/lib/mercury/sync_spec.rb
249
251
  - spec/lib/mercury/utils_spec.rb
250
252
  - spec/lib/mercury/wire_serializer_spec.rb