mercury_amqp 0.1.4 → 0.1.5

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: 046bcdd150e6875d732e623389624a2d26abb08c
4
- data.tar.gz: 1d5bc9696f0bd8ee418ba99c3326c4c6cfb5fa2d
3
+ metadata.gz: 95133530de1763cec95808562a5e7fc63051357c
4
+ data.tar.gz: 0c67f736b817ae297846b28daee0ecaba4b5db44
5
5
  SHA512:
6
- metadata.gz: 98d86ee54503071de7bec6037b6539f6931e15fef44428ad85ba6ed32ed7e38ab9a735973c3e039b6268b4dec39caaf13cb764b408c0b41988c045eb690d1cb9
7
- data.tar.gz: 9bbe848304af07e201adbeadbe0452826694a0b89e862d4958461069c284584c00b15408a12682b068edf7386b39e56708f9c56b38611e961b4f09e6b5ed1747
6
+ metadata.gz: 6d41a7d999b7f16ba2a19cd5a1bf625bf0899cffadfa08ae0d4a7d0db636ee37786ab752747a2e0e59a5a609d22be36bbf2ef1921aaa01edcb0c58a20ae49427
7
+ data.tar.gz: 31cee609bd834fc298f7879fb8ef26d65f3cd6cbe5219825c409a638049a9747fbacd9f01711d2d603d96182e611ab7f0280869d31d805bb3526b1e08d7b810a
@@ -27,13 +27,13 @@ class Mercury
27
27
 
28
28
  def ack_or_reject_message(msg)
29
29
  msgs.delete(msg) or raise 'tried to delete message that was not in queue!!'
30
- msg.subscriber.busy = false
30
+ msg.subscriber.handle_capacity += 1
31
31
  deliver # a subscriber just freed up
32
32
  end
33
33
 
34
34
  def nack(msg)
35
35
  msg.delivered = false
36
- msg.subscriber.busy = false
36
+ msg.subscriber.handle_capacity += 1
37
37
  deliver
38
38
  end
39
39
 
@@ -57,7 +57,7 @@ class Mercury
57
57
  subscriber = idle_subscribers.sample
58
58
  if @require_ack
59
59
  msg.delivered = true
60
- subscriber.busy = true
60
+ subscriber.handle_capacity -= 1
61
61
  else
62
62
  msgs.delete(msg)
63
63
  end
@@ -73,7 +73,7 @@ class Mercury
73
73
  end
74
74
 
75
75
  def idle_subscribers
76
- subscribers.reject(&:busy)
76
+ subscribers.reject { |s| s.handle_capacity == 0 }
77
77
  end
78
78
  end
79
79
  end
@@ -2,11 +2,11 @@ class Mercury
2
2
  class Fake
3
3
  class Subscriber
4
4
  attr_reader :handler
5
- attr_accessor :busy
5
+ attr_accessor :handle_capacity
6
6
 
7
- def initialize(handler)
7
+ def initialize(handler, handle_capacity)
8
8
  @handler = handler
9
- @busy = false
9
+ @handle_capacity = handle_capacity
10
10
  end
11
11
  end
12
12
  end
data/lib/mercury/fake.rb CHANGED
@@ -15,8 +15,9 @@ require 'mercury/fake/subscriber'
15
15
  # broken sockets, etc.
16
16
  class Mercury
17
17
  class Fake
18
- def initialize(domain=:default)
18
+ def initialize(domain=:default, parallelism: 1)
19
19
  @domain = Fake.domains[domain]
20
+ @parallelism = parallelism
20
21
  end
21
22
 
22
23
  def self.domains
@@ -46,7 +47,7 @@ class Mercury
46
47
  assert_not_closed
47
48
  q = ensure_queue(source_name, tag_filter, !!worker_group, worker_group)
48
49
  ret(k) # it's important we show the "start" operation finishing before delivery starts (in add_subscriber)
49
- q.add_subscriber(Subscriber.new(handler))
50
+ q.add_subscriber(Subscriber.new(handler, @parallelism))
50
51
  end
51
52
  private :start_worker_or_listener
52
53
 
@@ -24,10 +24,11 @@ class Mercury
24
24
  vhost: '/',
25
25
  username: 'guest',
26
26
  password: 'guest',
27
+ parallelism: 1,
27
28
  &k)
28
29
  AMQP.connect(host: host, port: port, vhost: vhost, username: username, password: password) do |amqp|
29
30
  @amqp = amqp
30
- @channel = AMQP::Channel.new(amqp, prefetch: 1) do
31
+ @channel = AMQP::Channel.new(amqp, prefetch: parallelism) do
31
32
  @channel.confirm_select
32
33
  install_default_error_handler
33
34
  k.call(self)
@@ -1,3 +1,3 @@
1
1
  class Mercury
2
- VERSION = '0.1.4'
2
+ VERSION = '0.1.5'
3
3
  end
@@ -48,6 +48,46 @@ describe Mercury::Monadic do
48
48
  end
49
49
  end
50
50
 
51
+ itt 'broadcasts messages to listeners' do
52
+ test_with_mercury do |m|
53
+ msgs1 = []
54
+ msgs2 = []
55
+ seql do
56
+ and_then { m.start_listener(source1, &msgs1.method(:push)) }
57
+ and_then { m.start_listener(source1, &msgs2.method(:push)) }
58
+ and_then { m.publish(source1, msg1) }
59
+ and_then { wait_until { msgs1.size == 1 && msgs2.size == 1 } }
60
+ and_lift do
61
+ expect(msgs1[0].content).to eql(msg1)
62
+ expect(msgs2[0].content).to eql(msg1)
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ itt 'it receives messages in parallel' do
69
+ test_with_mercury(parallelism: 2) do |m|
70
+ events = []
71
+ handle_msg = proc do |msg|
72
+ content = msg.content
73
+ events.push "received #{content['id']}"
74
+ EventMachine.add_timer(content['sleep_seconds']) do
75
+ events.push "finished #{content['id']}"
76
+ msg.ack
77
+ end
78
+ end
79
+ seql do
80
+ and_then { m.start_worker('worker1', source1, handle_msg) }
81
+ and_then { m.publish(source1, {'id' => 1, 'sleep_seconds' => 0.01}) }
82
+ and_then { m.publish(source1, {'id' => 2, 'sleep_seconds' => 0.01}) }
83
+ and_then { wait_until { events.size == 4 } }
84
+ and_lift do
85
+ expect(events).to eql ['received 1', 'received 2', 'finished 1', 'finished 2']
86
+ end
87
+ end
88
+ end
89
+ end
90
+
51
91
  itt 'sends and receives tagged messages' do
52
92
  test_with_mercury do |m|
53
93
  msgs = []
@@ -338,10 +378,10 @@ describe Mercury::Monadic do
338
378
  end
339
379
 
340
380
  # the block must return a Cps
341
- def test_with_mercury(&block)
381
+ def test_with_mercury(parallelism: 1, &block)
342
382
  sources = [source1, source2]
343
383
  queues = [queue1, queue2]
344
- test_with_mercury_cps(sources, queues, &block)
384
+ test_with_mercury_cps(sources, queues, parallelism: parallelism, &block)
345
385
  end
346
386
  end
347
387
 
data/spec/spec_helper.rb CHANGED
@@ -4,10 +4,10 @@ require 'mercury/fake'
4
4
  include Mercury::TestUtils
5
5
 
6
6
  # the block must return a Cps
7
- def test_with_mercury_cps(sources, queues, &block)
7
+ def test_with_mercury_cps(sources, queues, parallelism: 1, &block)
8
8
  em do
9
9
  seql do
10
- let(:m) { Mercury::Monadic.open }
10
+ let(:m) { Mercury::Monadic.open(parallelism: parallelism) }
11
11
  and_then { delete_sources_and_queues_cps(sources, queues) }
12
12
  and_then { block.call(m) }
13
13
  and_then { delete_sources_and_queues_cps(sources, queues) }
@@ -28,8 +28,8 @@ module MercuryFakeSpec
28
28
  it(name, &block)
29
29
  context 'with Mercury::Fake' do
30
30
  before :each do
31
- allow(Mercury).to receive(:open) do |&k|
32
- EM.next_tick { k.call(Mercury::Fake.new) }
31
+ allow(Mercury).to receive(:open) do |parallelism:1, &k|
32
+ EM.next_tick { k.call(Mercury::Fake.new(parallelism: parallelism)) }
33
33
  end
34
34
  end
35
35
  it(name, &block)
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.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Winton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-17 00:00:00.000000000 Z
11
+ date: 2016-02-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler