freddy 0.5.5 → 0.5.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e94ba387f99174d17ab66ed7caacbcc342a11a5f
4
- data.tar.gz: debfccbc498a8262ad843b7f3dcb9af9e5e1dc12
3
+ metadata.gz: 7b44c437c95066c2386f67bc9077fb0194376958
4
+ data.tar.gz: 8e4dc3ae45c00ff027a7eccd10609c77f146b715
5
5
  SHA512:
6
- metadata.gz: 0c9c4661349d96dd99ec104c5e3737cdb8f818beaf3c49f4450efadead9db70c1b6ab6493b9e93f08e142ac0bf32d5d5ccebe5e73bf2ed1a223c85ed9828a6b5
7
- data.tar.gz: d2024c91c580911db393ddaca913db397f72a1bc7961256dd53451609840c12f8c1ef618d1c02861ed4801752658ae756364b60b239772fdac34c21eb3ad0625
6
+ metadata.gz: 8fc4ef697700812b814402e02892a5aea60348e6e8550f21de356fe68d22e2c354c785a5f09b18184c61de373b0e03bb203f6aa8f57c4e34d33c1c0dab2fbfcd
7
+ data.tar.gz: cd13ca536d3b50a2f650d9bfc6f32639b23273f3233de316ea1e87825aa73d6eebdeec816114bd149c245c9406238dcbb5458777b3bf4042e81be55e1d6bdd44
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  else
9
9
  spec.name = "freddy"
10
10
  end
11
- spec.version = '0.5.5'
11
+ spec.version = '0.5.6'
12
12
  spec.authors = ["Urmas Talimaa"]
13
13
  spec.email = ["urmas.talimaa@gmail.com"]
14
14
  spec.description = %q{Messaging API}
@@ -22,33 +22,18 @@ class Freddy
22
22
  @response_consumer.consume(@response_queue, &method(:handle_response))
23
23
  end
24
24
 
25
- def produce(destination, payload, properties)
26
- timeout_in_seconds = properties.fetch(:timeout_in_seconds)
27
- container = SyncResponseContainer.new
28
- async_request destination, payload, container, properties
29
- container.wait_for_response(timeout_in_seconds)
30
- end
31
-
32
- private
33
-
34
- def async_request(destination, payload, container, timeout_in_seconds:, delete_on_timeout:, **properties)
25
+ def produce(destination, payload, timeout_in_seconds:, delete_on_timeout:, **properties)
35
26
  correlation_id = SecureRandom.uuid
36
27
 
28
+ container = SyncResponseContainer.new(
29
+ on_timeout(correlation_id, destination, timeout_in_seconds)
30
+ )
31
+
37
32
  @request_manager.store(correlation_id,
38
33
  callback: container,
39
34
  destination: destination
40
35
  )
41
36
 
42
- container.on_timeout do
43
- @logger.warn "Request timed out waiting response from #{destination}, correlation id #{correlation_id}"
44
- Utils.notify 'RequestTimeout', "Request timed out waiting for response from #{destination}", {
45
- correlation_id: correlation_id,
46
- destination: destination,
47
- timeout_in_seconds: timeout_in_seconds
48
- }
49
- @request_manager.delete(correlation_id)
50
- end
51
-
52
37
  if delete_on_timeout
53
38
  properties[:expiration] = (timeout_in_seconds * 1000).to_i
54
39
  end
@@ -68,6 +53,8 @@ class Freddy
68
53
  # need to lock these.
69
54
  @topic_exchange.publish json_payload, properties.dup
70
55
  @exchange.publish json_payload, properties.dup
56
+
57
+ container.wait_for_response(timeout_in_seconds)
71
58
  end
72
59
 
73
60
  def handle_response(delivery)
@@ -88,6 +75,23 @@ class Freddy
88
75
  "with correlation_id #{delivery.correlation_id}"
89
76
  request[:callback].call(delivery.payload, delivery)
90
77
  end
78
+
79
+ def on_timeout(correlation_id, destination, timeout_in_seconds)
80
+ Proc.new do
81
+ @logger.warn "Request timed out waiting response from #{destination}"\
82
+ ", correlation id #{correlation_id}"
83
+
84
+ Utils.notify 'RequestTimeout',
85
+ "Request timed out waiting for response from #{destination}",
86
+ {
87
+ correlation_id: correlation_id,
88
+ destination: destination,
89
+ timeout_in_seconds: timeout_in_seconds
90
+ }
91
+
92
+ @request_manager.delete(correlation_id)
93
+ end
94
+ end
91
95
  end
92
96
  end
93
97
  end
@@ -3,32 +3,31 @@ require 'timeout'
3
3
 
4
4
  class Freddy
5
5
  class SyncResponseContainer
6
- def initialize
6
+ def initialize(on_timeout)
7
7
  @mutex = Mutex.new
8
8
  @resource = ConditionVariable.new
9
+ @on_timeout = on_timeout
9
10
  end
10
11
 
11
12
  def call(response, delivery)
13
+ if response.nil?
14
+ raise StandardError, 'unexpected nil value for response'
15
+ end
16
+
12
17
  @response = response
13
18
  @delivery = delivery
14
19
  @mutex.synchronize { @resource.signal }
15
20
  end
16
21
 
17
- def on_timeout(&block)
18
- @on_timeout = block
19
- end
20
-
21
22
  def wait_for_response(timeout)
22
- @mutex.synchronize { @resource.wait(@mutex, timeout) }
23
+ @mutex.synchronize { @response || @resource.wait(@mutex, timeout) }
23
24
 
24
- if !defined?(@response)
25
+ if !@response
25
26
  @on_timeout.call
26
27
  raise TimeoutError.new(
27
28
  error: 'RequestTimeout',
28
29
  message: 'Timed out waiting for response'
29
30
  )
30
- elsif @response.nil?
31
- raise StandardError, 'unexpected nil value for response'
32
31
  elsif !@delivery || @delivery.type == 'error'
33
32
  raise InvalidRequestError.new(@response)
34
33
  else
@@ -1,11 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Freddy::SyncResponseContainer do
4
- let(:container) { described_class.new }
5
-
6
- before do
7
- container.on_timeout {}
8
- end
4
+ let(:container) { described_class.new(on_timeout) }
5
+ let(:on_timeout) { Proc.new {} }
9
6
 
10
7
  context 'when timeout' do
11
8
  subject { container.wait_for_response(0.01) }
@@ -21,19 +18,12 @@ describe Freddy::SyncResponseContainer do
21
18
  end
22
19
  end
23
20
 
24
- context 'when nil resonse' do
21
+ context 'when nil response' do
25
22
  let(:delivery) { {} }
26
23
 
27
- before do
28
- Thread.new do
29
- default_sleep
30
- container.call(nil, delivery)
31
- end
32
- end
33
-
34
24
  it 'raises timeout error' do
35
25
  expect {
36
- container.wait_for_response(2)
26
+ container.call(nil, delivery)
37
27
  }.to raise_error(StandardError, 'unexpected nil value for response')
38
28
  end
39
29
  end
@@ -44,6 +34,8 @@ describe Freddy::SyncResponseContainer do
44
34
  let(:delivery) { OpenStruct.new(type: 'success') }
45
35
 
46
36
  context 'when called after #call' do
37
+ let(:max_wait_time_in_seconds) { 0.5 }
38
+
47
39
  before do
48
40
  container.call(response, delivery)
49
41
  end
@@ -51,6 +43,12 @@ describe Freddy::SyncResponseContainer do
51
43
  it 'returns response' do
52
44
  expect(container.wait_for_response(timeout)).to eq(response)
53
45
  end
46
+
47
+ it 'does not wait for timeout' do
48
+ expect {
49
+ container.wait_for_response(timeout)
50
+ }.to change(Time, :now).by_at_most(max_wait_time_in_seconds)
51
+ end
54
52
  end
55
53
  end
56
54
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: freddy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.5
4
+ version: 0.5.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Urmas Talimaa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-05 00:00:00.000000000 Z
11
+ date: 2016-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler