rjr 0.18.2 → 0.19.1
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 +4 -4
- data/Rakefile +2 -0
- data/bin/rjr-client +16 -9
- data/bin/rjr-server +2 -1
- data/examples/client.rb +21 -19
- data/examples/server.rb +1 -1
- data/examples/structured_server.rb +1 -0
- data/examples/tcp.rb +1 -0
- data/lib/rjr/common.rb +1 -226
- data/lib/rjr/core_ext.rb +63 -0
- data/lib/rjr/dispatcher.rb +75 -219
- data/lib/rjr/messages.rb +8 -0
- data/lib/rjr/messages/compressed.rb +264 -0
- data/lib/rjr/messages/notification.rb +95 -0
- data/lib/rjr/messages/request.rb +99 -0
- data/lib/rjr/messages/response.rb +128 -0
- data/lib/rjr/node.rb +100 -97
- data/lib/rjr/node_callback.rb +43 -0
- data/lib/rjr/nodes/amqp.rb +12 -11
- data/lib/rjr/nodes/easy.rb +4 -4
- data/lib/rjr/nodes/local.rb +13 -12
- data/lib/rjr/nodes/multi.rb +1 -1
- data/lib/rjr/nodes/tcp.rb +15 -13
- data/lib/rjr/nodes/template.rb +4 -4
- data/lib/rjr/nodes/unix.rb +15 -13
- data/lib/rjr/nodes/web.rb +15 -14
- data/lib/rjr/nodes/ws.rb +12 -11
- data/lib/rjr/request.rb +128 -0
- data/lib/rjr/result.rb +75 -0
- data/lib/rjr/util/args.rb +145 -0
- data/lib/rjr/{em_adapter.rb → util/em_adapter.rb} +0 -0
- data/lib/rjr/util/handles_methods.rb +115 -0
- data/lib/rjr/util/has_messages.rb +50 -0
- data/lib/rjr/{inspect.rb → util/inspect.rb} +1 -1
- data/lib/rjr/util/json_parser.rb +101 -0
- data/lib/rjr/util/logger.rb +128 -0
- data/lib/rjr/{thread_pool.rb → util/thread_pool.rb} +2 -0
- data/lib/rjr/version.rb +1 -1
- data/site/jrw.js +1 -1
- data/specs/args_spec.rb +144 -0
- data/specs/dispatcher_spec.rb +399 -211
- data/specs/em_adapter_spec.rb +31 -18
- data/specs/handles_methods_spec.rb +154 -0
- data/specs/has_messages_spec.rb +54 -0
- data/specs/inspect_spec.rb +1 -1
- data/specs/json_parser_spec.rb +169 -0
- data/specs/messages/notification_spec.rb +59 -0
- data/specs/messages/request_spec.rb +66 -0
- data/specs/messages/response_spec.rb +94 -0
- data/specs/node_callbacks_spec.rb +47 -0
- data/specs/node_spec.rb +465 -56
- data/specs/request_spec.rb +147 -0
- data/specs/result_spec.rb +144 -0
- data/specs/thread_pool_spec.rb +1 -1
- metadata +41 -11
- data/lib/rjr/errors.rb +0 -23
- data/lib/rjr/message.rb +0 -351
- data/lib/rjr/semaphore.rb +0 -58
- data/specs/message_spec.rb +0 -229
data/specs/dispatcher_spec.rb
CHANGED
@@ -1,293 +1,481 @@
|
|
1
1
|
require 'rjr/dispatcher'
|
2
2
|
|
3
3
|
module RJR
|
4
|
-
describe
|
5
|
-
describe "#
|
6
|
-
it "
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
it "stores error" do
|
20
|
-
result = Result.new :error_code => 123, :error_msg => 'abc',
|
21
|
-
:error_class => ArgumentError
|
22
|
-
result.error_code.should == 123
|
23
|
-
result.error_msg.should == 'abc'
|
24
|
-
result.error_class.should == ArgumentError
|
25
|
-
end
|
26
|
-
|
27
|
-
context "when an error code is not specified" do
|
28
|
-
it "should be marked as successful" do
|
29
|
-
result = Result.new
|
30
|
-
result.success.should == true
|
31
|
-
result.failed.should == false
|
4
|
+
describe Dispatcher do
|
5
|
+
describe "#requests" do
|
6
|
+
it "returns dispatcher requests" do
|
7
|
+
d = Dispatcher.new
|
8
|
+
d.instance_variable_set(:@requests, ['requests'])
|
9
|
+
d.requests.should == ['requests']
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#store_request" do
|
14
|
+
context "keep_requests is true" do
|
15
|
+
it "stores request locally" do
|
16
|
+
d = Dispatcher.new :keep_requests => true
|
17
|
+
d.store_request 'request'
|
18
|
+
d.requests.should == ['request']
|
32
19
|
end
|
33
20
|
end
|
34
21
|
|
35
|
-
context "
|
36
|
-
it "
|
37
|
-
|
38
|
-
|
39
|
-
|
22
|
+
context "keep_requests is false" do
|
23
|
+
it "does not store request locally" do
|
24
|
+
d = Dispatcher.new :keep_requests => false
|
25
|
+
d.store_request 'request'
|
26
|
+
d.requests.should == []
|
40
27
|
end
|
41
28
|
end
|
29
|
+
end
|
42
30
|
|
43
|
-
|
31
|
+
it "does not keep requests by default" do
|
32
|
+
d = Dispatcher.new
|
33
|
+
d.store_request 'request'
|
34
|
+
d.requests.should == []
|
35
|
+
end
|
44
36
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end # descirbe #==
|
37
|
+
it "initializes handlers" do
|
38
|
+
Dispatcher.new.handlers.should == {}
|
39
|
+
end
|
49
40
|
|
50
|
-
|
51
|
-
|
41
|
+
it "initializes environments" do
|
42
|
+
Dispatcher.new.environments.should == {}
|
43
|
+
end
|
52
44
|
|
53
|
-
|
54
|
-
|
55
|
-
it "should be convertable to json" do
|
56
|
-
req = Request.new :rjr_method => 'foobar',
|
57
|
-
:rjr_method_args => [:a, :b],
|
58
|
-
:rjr_headers => { :header1 => :val1 },
|
59
|
-
:rjr_node_type => :local,
|
60
|
-
:rjr_node_id => :loc1
|
61
|
-
|
62
|
-
res = RJR::Result.new :result => 42,
|
63
|
-
:error_code => 123,
|
64
|
-
:error_msg => 'error occurred',
|
65
|
-
:error_class => 'ArgumentError'
|
66
|
-
req.result = res
|
67
|
-
|
68
|
-
j = req.to_json()
|
69
|
-
j.should include('"json_class":"RJR::Request"')
|
70
|
-
j.should include('"rjr_method":"foobar"')
|
71
|
-
j.should include('"rjr_method_args":["a","b"]')
|
72
|
-
j.should include('"rjr_headers":{"header1":"val1"}')
|
73
|
-
j.should include('"rjr_node_type":"local"')
|
74
|
-
j.should include('"rjr_node_id":"loc1"')
|
75
|
-
j.should include('"result":42')
|
76
|
-
j.should include('"error_code":123')
|
77
|
-
j.should include('"error_msg":"error occurred"')
|
78
|
-
j.should include('"error_class":"ArgumentError"')
|
45
|
+
it "initializes requests" do
|
46
|
+
Dispatcher.new.requests.should == []
|
79
47
|
end
|
80
48
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
49
|
+
describe "#clear!" do
|
50
|
+
it "clears handlers" do
|
51
|
+
d = Dispatcher.new
|
52
|
+
d.handlers['foo'] = 'handler'
|
53
|
+
d.clear!
|
54
|
+
d.handlers.should == {}
|
55
|
+
end
|
56
|
+
|
57
|
+
it "clears environments" do
|
58
|
+
d = Dispatcher.new
|
59
|
+
d.environments['foo'] = 'envi'
|
60
|
+
d.clear!
|
61
|
+
d.environments.should == {}
|
62
|
+
end
|
63
|
+
|
64
|
+
it "clears requests" do
|
65
|
+
d = Dispatcher.new :keep_requests => true
|
66
|
+
d.store_request 'foo'
|
67
|
+
d.clear!
|
68
|
+
d.requests.should == []
|
69
|
+
end
|
92
70
|
end
|
93
71
|
|
94
|
-
|
95
|
-
it
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
}
|
115
|
-
|
116
|
-
request = Request.new :rjr_method => method,
|
117
|
-
:rjr_method_args => params,
|
118
|
-
:rjr_headers => headers,
|
119
|
-
:rjr_callback => cb,
|
120
|
-
:rjr_node_id => ni,
|
121
|
-
:rjr_node_type => nt,
|
122
|
-
:rjr_handler => handler
|
123
|
-
|
124
|
-
request.handle
|
125
|
-
invoked.should be_true
|
126
|
-
ip1.should == params[0]
|
127
|
-
ip2.should == params[1]
|
128
|
-
im.should == method
|
129
|
-
ini.should == ni
|
130
|
-
int.should == nt
|
131
|
-
icb.should == cb
|
132
|
-
ih.should == headers
|
72
|
+
describe "#add_module" do
|
73
|
+
it 'requires module' do
|
74
|
+
d = Dispatcher.new
|
75
|
+
d.should_receive(:require).with('module')
|
76
|
+
d.should_receive(:dispatch_module) # stub out
|
77
|
+
d.add_module('module')
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'invokes dispatch_module method' do
|
81
|
+
d = Dispatcher.new
|
82
|
+
d.should_receive(:require) # stub out
|
83
|
+
d.should_receive(:dispatch_module).with(d)
|
84
|
+
d.add_module('module')
|
85
|
+
end
|
86
|
+
|
87
|
+
it "returns self" do
|
88
|
+
d = Dispatcher.new
|
89
|
+
d.should_receive(:require) # stub out
|
90
|
+
d.should_receive(:dispatch_module) # stub out
|
91
|
+
d.add_module('module').should == d
|
133
92
|
end
|
134
93
|
end
|
135
|
-
end
|
136
|
-
end
|
137
94
|
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
95
|
+
describe "#handle" do
|
96
|
+
it "registers callback for specifed signature" do
|
97
|
+
d = Dispatcher.new
|
98
|
+
cb = proc {}
|
99
|
+
d.handle 'foobar', cb
|
100
|
+
d.handlers['foobar'].should == cb
|
101
|
+
end
|
142
102
|
|
143
|
-
it "
|
103
|
+
it "registers block for specifed signature" do
|
144
104
|
d = Dispatcher.new
|
145
|
-
|
146
|
-
d.handle
|
147
|
-
d.handlers['foobar'].should ==
|
148
|
-
d.handles?('foobar').should be_true
|
105
|
+
cb = proc {}
|
106
|
+
d.handle 'foobar', &cb
|
107
|
+
d.handlers['foobar'].should == cb
|
149
108
|
end
|
150
109
|
|
151
|
-
it "
|
110
|
+
it "registers callback for specifed signatures" do
|
152
111
|
d = Dispatcher.new
|
153
|
-
|
154
|
-
d.handle
|
155
|
-
d.handlers['foobar'].should ==
|
156
|
-
d.
|
112
|
+
cb = proc {}
|
113
|
+
d.handle ['foobar', 'barfoo'], cb
|
114
|
+
d.handlers['foobar'].should == cb
|
115
|
+
d.handlers['barfoo'].should == cb
|
157
116
|
end
|
158
117
|
|
159
|
-
it "
|
118
|
+
it "registers block for specifed signatures" do
|
160
119
|
d = Dispatcher.new
|
161
|
-
|
162
|
-
d.handle
|
163
|
-
d.handlers['foobar'].should ==
|
164
|
-
d.handlers['barfoo'].should ==
|
165
|
-
d.handles?('foobar').should be_true
|
166
|
-
d.handles?('barfoo').should be_true
|
120
|
+
cb = proc {}
|
121
|
+
d.handle ['foobar', 'barfoo'], &cb
|
122
|
+
d.handlers['foobar'].should == cb
|
123
|
+
d.handlers['barfoo'].should == cb
|
167
124
|
end
|
168
125
|
end
|
169
126
|
|
170
|
-
|
171
|
-
context "
|
172
|
-
it "
|
127
|
+
describe "#handler_for" do
|
128
|
+
context "dispatcher has handler" do
|
129
|
+
it "returns registered handler" do
|
173
130
|
d = Dispatcher.new
|
174
|
-
|
175
|
-
|
131
|
+
cb = proc {}
|
132
|
+
d.handle 'foobar', cb
|
133
|
+
d.handler_for('foobar').should == cb
|
176
134
|
end
|
177
135
|
end
|
178
136
|
|
179
|
-
context "
|
180
|
-
it "
|
181
|
-
invoked = false
|
137
|
+
context "dispatcher does not have handler" do
|
138
|
+
it "returns nil" do
|
182
139
|
d = Dispatcher.new
|
183
|
-
|
184
|
-
|
140
|
+
d.handler_for('foobar').should be_nil
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
it "matches regex signature" do
|
145
|
+
d = Dispatcher.new
|
146
|
+
cb = proc {}
|
147
|
+
d.handle /foobar.*/, cb
|
148
|
+
d.handler_for('foobar1').should == cb
|
149
|
+
d.handler_for('barfoo1').should be_nil
|
150
|
+
end
|
185
151
|
|
186
|
-
|
187
|
-
|
152
|
+
context "mulitple matches" do
|
153
|
+
it "returns exact match before regex" do
|
154
|
+
d = Dispatcher.new
|
155
|
+
cb1 = proc{ 1 }
|
156
|
+
cb2 = proc{ 2 }
|
157
|
+
d.handle /foobar.*/, cb1
|
158
|
+
d.handle "foobar1", cb2
|
159
|
+
d.handler_for("foobar1").should == cb2
|
188
160
|
end
|
161
|
+
end
|
162
|
+
end
|
189
163
|
|
190
|
-
|
191
|
-
|
164
|
+
describe "#handles?" do
|
165
|
+
context "dispatcher has handler" do
|
166
|
+
it "returns true" do
|
167
|
+
d = Dispatcher.new
|
168
|
+
cb = proc {}
|
169
|
+
d.handle 'foobar', cb
|
170
|
+
d.handles?('foobar').should be_true
|
192
171
|
end
|
172
|
+
end
|
193
173
|
|
194
|
-
|
195
|
-
|
174
|
+
context "dispatcher does not have handler" do
|
175
|
+
it "returns false" do
|
196
176
|
d = Dispatcher.new
|
197
|
-
|
198
|
-
|
177
|
+
d.handles?('foobar').should be_false
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
describe "#env" do
|
183
|
+
it "registers environment for specified signature" do
|
184
|
+
d = Dispatcher.new
|
185
|
+
d.env('foobar', 'fooenv')
|
186
|
+
d.environments['foobar'].should == 'fooenv'
|
187
|
+
end
|
188
|
+
|
189
|
+
it "registers environment for specified signatures" do
|
190
|
+
d = Dispatcher.new
|
191
|
+
d.env(['foobar', 'barfoo'], 'fooenv')
|
192
|
+
d.environments['foobar'].should == 'fooenv'
|
193
|
+
d.environments['barfoo'].should == 'fooenv'
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
describe "#env_for" do
|
198
|
+
context "dispatcher has environment" do
|
199
|
+
it "returns registered environment" do
|
200
|
+
d = Dispatcher.new
|
201
|
+
d.env('foobar', 'fooenv')
|
202
|
+
d.env_for('foobar').should == 'fooenv'
|
203
|
+
end
|
204
|
+
end
|
199
205
|
|
200
|
-
|
201
|
-
|
206
|
+
context "dispatcher does not have environment" do
|
207
|
+
it "returns nil" do
|
208
|
+
d = Dispatcher.new
|
209
|
+
d.env_for('foobar').should be_nil
|
202
210
|
end
|
211
|
+
end
|
203
212
|
|
204
|
-
|
213
|
+
it "matches regex signature" do
|
214
|
+
d = Dispatcher.new
|
215
|
+
d.env(/foobar.*/, 'fooenv')
|
216
|
+
d.env_for('foobar1').should == 'fooenv'
|
217
|
+
d.env_for('barfoo1').should be_nil
|
218
|
+
end
|
219
|
+
|
220
|
+
context "mulitple matches" do
|
221
|
+
it "returns exact match before regex" do
|
205
222
|
d = Dispatcher.new
|
206
|
-
|
207
|
-
d.
|
223
|
+
d.env /foobar.*/, 'foo'
|
224
|
+
d.env "foobar1", 'bar'
|
225
|
+
d.env_for("foobar1").should == 'bar'
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
describe "#dispatch" do
|
231
|
+
context "no handler registered for method" do
|
232
|
+
it "returns Result.method_not_found" do
|
233
|
+
expected = Result.method_not_found('foobar')
|
234
|
+
Dispatcher.new.dispatch(:rjr_method => 'foobar').should == expected
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
it "creates request with registered handler" do
|
239
|
+
handler = proc {}
|
240
|
+
d = Dispatcher.new
|
241
|
+
d.handle 'foobar', &handler
|
208
242
|
|
209
|
-
|
210
|
-
|
243
|
+
expected = {:rjr_method => 'foobar', :rjr_handler => handler}
|
244
|
+
Request.should_receive(:new).with(expected).and_call_original
|
245
|
+
d.dispatch :rjr_method => 'foobar'
|
246
|
+
end
|
247
|
+
|
248
|
+
it "extends request w/ method environment" do
|
249
|
+
d = Dispatcher.new
|
250
|
+
d.env 'foobar', 'fooenv'
|
251
|
+
d.handle 'foobar', proc {}
|
252
|
+
|
253
|
+
r = Request.new
|
254
|
+
Request.should_receive(:new).and_return(r)
|
255
|
+
r.should_receive(:extend).with('fooenv')
|
256
|
+
d.dispatch :rjr_method => 'foobar'
|
257
|
+
end
|
258
|
+
|
259
|
+
it "handles request" do
|
260
|
+
d = Dispatcher.new
|
261
|
+
d.handle 'foobar', proc {}
|
262
|
+
|
263
|
+
r = Request.new
|
264
|
+
Request.should_receive(:new).and_return(r)
|
265
|
+
r.should_receive(:handle).and_call_original
|
266
|
+
d.dispatch :rjr_method => 'foobar'
|
267
|
+
end
|
268
|
+
|
269
|
+
it "returns request result" do
|
270
|
+
d = Dispatcher.new
|
271
|
+
d.handle 'foobar', proc {}
|
272
|
+
|
273
|
+
r = Request.new
|
274
|
+
Request.should_receive(:new).and_return(r)
|
275
|
+
|
276
|
+
result = Result.new :result => 0
|
277
|
+
r.should_receive(:handle).and_return(0)
|
278
|
+
d.dispatch(:rjr_method => 'foobar').should == result
|
279
|
+
end
|
280
|
+
|
281
|
+
context "successful request" do
|
282
|
+
it "returns return value in result" do
|
283
|
+
d = Dispatcher.new
|
284
|
+
d.handle 'foobar', proc { 42 }
|
285
|
+
d.dispatch(:rjr_method => 'foobar').result.should == 42
|
211
286
|
end
|
287
|
+
end
|
212
288
|
|
213
|
-
|
289
|
+
context "exception during request" do
|
290
|
+
it "returns exception error in result" do
|
214
291
|
d = Dispatcher.new
|
215
|
-
|
292
|
+
d.handle 'foobar', proc { raise ArgumentError, "invalid" }
|
293
|
+
|
294
|
+
result = Result.new :error_code => -32000,
|
295
|
+
:error_msg => "invalid",
|
296
|
+
:error_class => ArgumentError
|
297
|
+
d.dispatch(:rjr_method => 'foobar').should == result
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
301
|
+
it "stores request" do
|
302
|
+
d = Dispatcher.new :keep_requests => true
|
303
|
+
d.handle 'foobar', proc { 42 }
|
304
|
+
d.should_receive(:store_request).and_call_original
|
305
|
+
d.dispatch(:rjr_method => 'foobar')
|
306
|
+
d.requests.size.should == 1
|
307
|
+
|
308
|
+
d.requests.first.rjr_method.should == 'foobar'
|
309
|
+
d.requests.first.result.result.should == 42
|
310
|
+
end
|
311
|
+
end
|
312
|
+
|
313
|
+
describe "#handle_response" do
|
314
|
+
context "error response" do
|
315
|
+
it "raises exception with response error" do
|
316
|
+
r = Result.new :error_code => -32000, :error_msg => "invalid"
|
317
|
+
d = Dispatcher.new
|
318
|
+
lambda{
|
319
|
+
d.handle_response(r)
|
320
|
+
}.should raise_error(Exception, "invalid")
|
321
|
+
end
|
322
|
+
end
|
323
|
+
|
324
|
+
context "successful response" do
|
325
|
+
it "returns result" do
|
326
|
+
r = Result.new :success => true, :result => 42
|
327
|
+
d = Dispatcher.new
|
328
|
+
d.handle_response(r).should == 42
|
329
|
+
end
|
330
|
+
end
|
331
|
+
end
|
332
|
+
|
333
|
+
describe "Behaviour" do
|
334
|
+
context "registering handlers" do
|
335
|
+
it "should register handler for method" do
|
336
|
+
d = Dispatcher.new
|
337
|
+
h = proc {}
|
338
|
+
d.handle('foobar', h)
|
339
|
+
d.handlers['foobar'].should == h
|
340
|
+
d.handles?('foobar').should be_true
|
341
|
+
end
|
342
|
+
|
343
|
+
it "should set handler from block param" do
|
344
|
+
d = Dispatcher.new
|
345
|
+
h = proc {}
|
216
346
|
d.handle('foobar', &h)
|
347
|
+
d.handlers['foobar'].should == h
|
348
|
+
d.handles?('foobar').should be_true
|
349
|
+
end
|
217
350
|
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
351
|
+
it "should register handler for multiple methods" do
|
352
|
+
d = Dispatcher.new
|
353
|
+
h = proc {}
|
354
|
+
d.handle(['foobar', 'barfoo'], &h)
|
355
|
+
d.handlers['foobar'].should == h
|
356
|
+
d.handlers['barfoo'].should == h
|
357
|
+
d.handles?('foobar').should be_true
|
358
|
+
d.handles?('barfoo').should be_true
|
222
359
|
end
|
360
|
+
end
|
223
361
|
|
224
|
-
|
225
|
-
|
362
|
+
context "dispatching requests" do
|
363
|
+
context "handler does not exist" do
|
364
|
+
it "should return method not found" do
|
226
365
|
d = Dispatcher.new
|
227
|
-
d.
|
228
|
-
|
229
|
-
d.dispatch :rjr_method => 'foobar'
|
230
|
-
d.requests.should be_empty
|
366
|
+
r = d.dispatch :rjr_method => 'foobar'
|
367
|
+
r.should == Result.method_not_found('foobar')
|
231
368
|
end
|
232
369
|
end
|
233
370
|
|
234
|
-
context "
|
235
|
-
it "should
|
236
|
-
|
237
|
-
|
371
|
+
context "handler is registered" do
|
372
|
+
it "should invoke handler" do
|
373
|
+
invoked = false
|
374
|
+
d = Dispatcher.new
|
375
|
+
h = proc { invoked = true }
|
238
376
|
d.handle('foobar', &h)
|
239
377
|
|
240
|
-
d.requests.size.should == 0
|
241
378
|
d.dispatch :rjr_method => 'foobar'
|
242
|
-
|
243
|
-
d.requests.first.rjr_method.should == 'foobar'
|
379
|
+
invoked.should be_true
|
244
380
|
end
|
245
381
|
|
246
|
-
it "should
|
382
|
+
it "should pass params to handler" do
|
383
|
+
param = nil
|
247
384
|
d = Dispatcher.new
|
248
|
-
|
249
|
-
h = proc { |p| }
|
385
|
+
h = proc { |p| param = p}
|
250
386
|
d.handle('foobar', &h)
|
251
387
|
|
252
388
|
d.dispatch(:rjr_method => 'foobar', :rjr_method_args => [42])
|
253
|
-
|
389
|
+
param.should == 42
|
254
390
|
end
|
255
391
|
|
256
|
-
it "should
|
392
|
+
it "should return request result" do
|
257
393
|
d = Dispatcher.new
|
258
|
-
d.keep_requests = true
|
259
394
|
h = proc { 42 }
|
260
395
|
d.handle('foobar', &h)
|
261
396
|
|
262
|
-
d.dispatch :rjr_method => 'foobar'
|
263
|
-
|
397
|
+
r = d.dispatch :rjr_method => 'foobar'
|
398
|
+
r.result.should == 42
|
399
|
+
end
|
400
|
+
|
401
|
+
it "should return request error" do
|
402
|
+
d = Dispatcher.new
|
403
|
+
h = proc { raise ArgumentError, "bah" }
|
404
|
+
d.handle('foobar', &h)
|
405
|
+
|
406
|
+
r = d.dispatch :rjr_method => 'foobar'
|
407
|
+
r.error_code.should == -32000
|
408
|
+
r.error_msg.should == "bah"
|
409
|
+
r.error_class.should == ArgumentError
|
410
|
+
end
|
411
|
+
|
412
|
+
context "keep_requests is false (default)" do
|
413
|
+
it "should not store requests" do
|
414
|
+
d = Dispatcher.new
|
415
|
+
d.handle('foobar') {}
|
416
|
+
d.requests.should be_empty
|
417
|
+
d.dispatch :rjr_method => 'foobar'
|
418
|
+
d.requests.should be_empty
|
419
|
+
end
|
420
|
+
end
|
421
|
+
|
422
|
+
context "keep_requests is true" do
|
423
|
+
it "should store request locally" do
|
424
|
+
d = Dispatcher.new :keep_requests => true
|
425
|
+
h = proc {}
|
426
|
+
d.handle('foobar', &h)
|
427
|
+
|
428
|
+
d.requests.size.should == 0
|
429
|
+
d.dispatch :rjr_method => 'foobar'
|
430
|
+
d.requests.size.should == 1
|
431
|
+
d.requests.first.rjr_method.should == 'foobar'
|
432
|
+
end
|
433
|
+
|
434
|
+
it "should set params on request" do
|
435
|
+
d = Dispatcher.new
|
436
|
+
d.keep_requests = true
|
437
|
+
h = proc { |p| }
|
438
|
+
d.handle('foobar', &h)
|
439
|
+
|
440
|
+
d.dispatch(:rjr_method => 'foobar', :rjr_method_args => [42])
|
441
|
+
d.requests.first.rjr_method_args.should == [42]
|
442
|
+
end
|
443
|
+
|
444
|
+
it "should set result on request" do
|
445
|
+
d = Dispatcher.new
|
446
|
+
d.keep_requests = true
|
447
|
+
h = proc { 42 }
|
448
|
+
d.handle('foobar', &h)
|
449
|
+
|
450
|
+
d.dispatch :rjr_method => 'foobar'
|
451
|
+
d.requests.first.result.result.should == 42
|
452
|
+
end
|
264
453
|
end
|
265
454
|
end
|
266
455
|
end
|
267
|
-
end
|
268
456
|
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
457
|
+
context "processing responses" do
|
458
|
+
context "successful response" do
|
459
|
+
it "should return result" do
|
460
|
+
r = Result.new :result => 'woot'
|
461
|
+
d = Dispatcher.new
|
462
|
+
p = d.handle_response(r)
|
463
|
+
p.should == "woot"
|
464
|
+
end
|
276
465
|
end
|
277
|
-
end
|
278
466
|
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
467
|
+
context "failed response" do
|
468
|
+
it "should raise error" do
|
469
|
+
r = Result.new :error_code => 123, :error_msg => "bah",
|
470
|
+
:error_class => ArgumentError
|
471
|
+
d = Dispatcher.new
|
472
|
+
lambda{
|
473
|
+
d.handle_response(r)
|
474
|
+
}.should raise_error(Exception, "bah")
|
475
|
+
#}.should raise_error(ArgumentError, "bah")
|
476
|
+
end
|
288
477
|
end
|
289
478
|
end
|
290
|
-
end
|
291
|
-
|
479
|
+
end # describe Behaviour
|
292
480
|
end # describe Dispatcher
|
293
481
|
end # module RJR
|