combi 0.0.7 → 0.0.8

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: 38eced61f795b66ed4f2eafe663e8dd5a204b99a
4
- data.tar.gz: e0793e82d4b75ba93ddc5a835a9bea71d1453117
3
+ metadata.gz: bf7f73f8dc51e7b390cbdfd3d76bd26d74e95261
4
+ data.tar.gz: eed9f9840dd7e43cb8c72057bdb9688e49d94bf5
5
5
  SHA512:
6
- metadata.gz: c8bddb984ae14b2c28329dc152873cd580dd70e3bc13a641fb1da673d0edf6c2835b59d52ec87acc2552cab6124e79f33a4be8829eecba68e05ce6667b90e124
7
- data.tar.gz: 756143cca67900ca14a5c038ab14eda751ebda96a68c956634c8b5fcf3885f396bd6f88ec9e9b00ce7f4c2cbf8978344748079cf2b4145e9cac241f7fa4f5fff
6
+ metadata.gz: c5287c09cd3cceb88ca67b98ffe8641ae3f607d8d472a1048762d8318bbad8254a0d299555811a09d0e8ad998a45fa780b3449efed7a708a395f8f1d17f4ab18
7
+ data.tar.gz: 82a80a9e331d18d31ada7497e2a9ddde314a4b59874f1487baf95bb73d3ef77fd1766032f3569a692316e414c876cf81a088098ba9cf859f4305dcb8fd95e732
@@ -61,6 +61,8 @@ module Combi
61
61
  end
62
62
  {result: 'ok', response: response}
63
63
  end
64
+ else
65
+ {result: 'error', response: {error: true, message: 'unknown service'}}
64
66
  end
65
67
  end
66
68
 
@@ -6,30 +6,33 @@ module Combi
6
6
  def request(handler_name, kind, message, options = {})
7
7
  options[:timeout] ||= RPC_DEFAULT_TIMEOUT
8
8
  handler = memory_handlers[handler_name.to_s]
9
- return if handler.nil?
10
- service_instance = handler[:service_instance]
11
- message = JSON.parse(message.to_json)
12
- return unless service_instance.respond_to?(kind)
13
9
  waiter = EventMachine::DefaultDeferrable.new
14
- waiter.timeout(options[:timeout], RuntimeError.new(Timeout::Error))
15
- begin
16
- Timeout.timeout(options[:timeout]) do
17
- response = service_instance.send(kind, message)
18
- if response.respond_to? :succeed
19
- response.callback do |service_response|
20
- waiter.succeed service_response
10
+ if handler.nil?
11
+ waiter.fail('error' => true, 'message' => 'unknown service')
12
+ else
13
+ service_instance = handler[:service_instance]
14
+ message = JSON.parse(message.to_json)
15
+ return unless service_instance.respond_to?(kind)
16
+ waiter.timeout(options[:timeout], RuntimeError.new(Timeout::Error))
17
+ begin
18
+ Timeout.timeout(options[:timeout]) do
19
+ response = service_instance.send(kind, message)
20
+ if response.respond_to? :succeed
21
+ response.callback do |service_response|
22
+ waiter.succeed service_response
23
+ end
24
+ else
25
+ waiter.succeed response
21
26
  end
22
- else
23
- waiter.succeed response
24
27
  end
28
+ rescue Timeout::Error => e
29
+ log "ERROR"
30
+ waiter.fail RuntimeError.new(Timeout::Error)
31
+ rescue Exception => e
32
+ log "other ERROR"
33
+ log e.inspect
34
+ waiter.fail({'error' => true, 'message' => e.message})
25
35
  end
26
- rescue Timeout::Error => e
27
- log "ERROR"
28
- waiter.fail RuntimeError.new(Timeout::Error)
29
- rescue Exception => e
30
- log "other ERROR"
31
- log e.inspect
32
- waiter.fail({'error' => true, 'message' => e.message})
33
36
  end
34
37
  waiter
35
38
  end
@@ -201,6 +201,7 @@ module Combi
201
201
  else
202
202
  log "[WARNING] Service #{service_name} not found"
203
203
  log "[WARNING] handlers: #{handlers.keys.inspect}"
204
+ response = {error: true, message: 'unknown service'}
204
205
  end
205
206
  end
206
207
 
@@ -1,3 +1,3 @@
1
1
  module Combi
2
- VERSION = '0.0.7'
2
+ VERSION = '0.0.8'
3
3
  end
@@ -26,15 +26,35 @@ describe "in a multi bus environment" do
26
26
  def do_it(params)
27
27
  defer = EventMachine::DefaultDeferrable.new
28
28
  EM.synchrony do
29
- req = @other_client.request(:say_hello, :do_it, params, timeout: 3)
29
+ service_name = params ['service_name'] || :say_hello
30
+ req = @other_client.request(service_name, :do_it, params, timeout: 0.1)
30
31
  service_result = EM::Synchrony.sync req
31
- defer.succeed(service_result*2)
32
+ if service_result.is_a? RuntimeError
33
+ if @other_client.is_a?(Combi::Queue) && service_result.message == "Timeout::Error"
34
+ defer.succeed({'error' => true, 'message' => 'other service failed'})
35
+ else
36
+ defer.succeed({'error' => true, 'message' => 'unknown error'})
37
+ end
38
+ else
39
+ if service_result.respond_to?(:keys) && service_result['error'] == true
40
+ defer.succeed({'error' => true, 'message' => 'other service failed'})
41
+ else
42
+ defer.succeed(service_result*2)
43
+ end
44
+ end
32
45
  end
33
46
  defer
34
47
  end
35
48
  end
36
49
  end
37
50
 
51
+ Given(:broken_service) do
52
+ Module.new do
53
+ def actions; [:say_hello_if_you_can]; end
54
+ def do_it(params); puts "raising error"; raise "I can't talk" ; end
55
+ end
56
+ end
57
+
38
58
  Given(:amqp_config) do
39
59
  {
40
60
  :host => "127.0.0.1",
@@ -68,10 +88,8 @@ describe "in a multi bus environment" do
68
88
  RabbitmqServer.instance.stop! if ENV['CLEAN']
69
89
  }
70
90
 
71
- Given(:options) { {timeout: 3} }
91
+ Given(:options) { {timeout: 0.5} }
72
92
 
73
- Given(:service_for_main_bus) { main_bus_provider.add_service composed_service_class.new(internal_bus_consumer) }
74
- Given(:service_for_internal_bus) { internal_bus_provider.add_service boring_salutation_service }
75
93
  When(:params) { { who: 'world' } }
76
94
  When(:expected_result) { "Hello worldHello world" }
77
95
 
@@ -111,28 +129,78 @@ describe "in a multi bus environment" do
111
129
  end
112
130
  end
113
131
 
114
- Then do
115
- em do
116
- service_for_main_bus
117
- service_for_internal_bus
118
- main_bus_provider.start!
119
- internal_bus_provider.start!
120
- send("#{main}_server")
121
- send("#{internal}_server")
122
- main_bus_consumer.start!
123
- internal_bus_consumer.start!
124
-
125
- EM.synchrony do
126
- service_result = EM::Synchrony.sync main_bus_consumer.request(:repeat_with_me, :do_it, params, options)
127
- service_result.should eq expected_result
128
- done
129
- main_bus_provider.stop!
130
- internal_bus_provider.stop!
131
- main_bus_consumer.stop!
132
- internal_bus_consumer.stop!
132
+ Given(:prepare) do
133
+ service_for_main_bus
134
+ start_internal = service_for_internal_bus
135
+ main_bus_provider.start!
136
+ internal_bus_provider.start! if start_internal
137
+ send("#{main}_server")
138
+ send("#{internal}_server")
139
+ main_bus_consumer.start!
140
+ internal_bus_consumer.start! if start_internal
141
+ end
142
+
143
+ Given(:finalize) do
144
+ main_bus_provider.stop!
145
+ internal_bus_provider.stop!
146
+ main_bus_consumer.stop!
147
+ internal_bus_consumer.stop!
148
+ end
149
+
150
+ context "both services are working ok" do
151
+ Given(:service_for_main_bus) { main_bus_provider.add_service composed_service_class.new(internal_bus_consumer); true }
152
+ Given(:service_for_internal_bus) { internal_bus_provider.add_service boring_salutation_service; true }
153
+
154
+ Then do
155
+ em do
156
+ prepare
157
+ EM.synchrony do
158
+ service_result = EM::Synchrony.sync main_bus_consumer.request(:repeat_with_me, :do_it, params, options)
159
+ service_result.should eq expected_result
160
+ done
161
+ finalize
162
+ end
163
+ end
164
+ end
165
+ end
166
+
167
+ context "the external service raise an error" do
168
+ Given(:service_for_main_bus) { main_bus_provider.add_service broken_service; true }
169
+ Given(:service_for_internal_bus) { false }
170
+
171
+ Then do
172
+ em do
173
+ prepare
174
+ EM.synchrony do
175
+ service_result = EM::Synchrony.sync main_bus_consumer.request(:say_hello_if_you_can, :do_it, params, options)
176
+ service_result['error'].should be_true
177
+ service_result['message'].should eq "I can't talk"
178
+ done
179
+ finalize
180
+ end
133
181
  end
134
182
  end
135
183
  end
184
+
185
+ context "the internal service raise an error" do
186
+ Given(:service_for_main_bus) { main_bus_provider.add_service composed_service_class.new(internal_bus_consumer); true }
187
+ Given(:service_for_internal_bus) { main_bus_provider.add_service broken_service; true }
188
+
189
+ Then do
190
+ em do
191
+ prepare
192
+ EM.synchrony do
193
+ params.merge!('service_name' => 'say_hello_if_you_can')
194
+ service_result = EM::Synchrony.sync main_bus_consumer.request(:repeat_with_me, :do_it, params, options)
195
+ service_result['error'].should be_true
196
+ service_result['message'].should eq 'other service failed'
197
+ done
198
+ finalize
199
+ end
200
+ end
201
+ end
202
+ end
203
+
136
204
  end
137
205
  end
138
206
  end
@@ -151,4 +151,28 @@ shared_examples_for "standard_bus" do
151
151
  end
152
152
  end
153
153
  end
154
+
155
+ context 'return an error when requesting an unknown service' do
156
+ Given(:error_message) { 'unknown service' }
157
+ When(:service) { provider.add_service broken_service }
158
+ Then do
159
+ em do
160
+ prepare
161
+ EM.synchrony do
162
+ begin
163
+ service_result = EM::Synchrony.sync consumer.request(:some_not_service, :do_it, {}, { timeout: 0.1 })
164
+ if service_result.is_a? RuntimeError
165
+ consumer.class.should eq Combi::Queue
166
+ service_result.message.should eq "Timeout::Error"
167
+ else
168
+ service_result['error'].should be_true
169
+ service_result['message'].should eq error_message
170
+ end
171
+ done
172
+ finalize
173
+ end
174
+ end
175
+ end
176
+ end
177
+ end
154
178
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: combi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - German Del Zotto
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-05-24 00:00:00.000000000 Z
12
+ date: 2014-05-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: yajl-ruby