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.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +2 -0
  3. data/bin/rjr-client +16 -9
  4. data/bin/rjr-server +2 -1
  5. data/examples/client.rb +21 -19
  6. data/examples/server.rb +1 -1
  7. data/examples/structured_server.rb +1 -0
  8. data/examples/tcp.rb +1 -0
  9. data/lib/rjr/common.rb +1 -226
  10. data/lib/rjr/core_ext.rb +63 -0
  11. data/lib/rjr/dispatcher.rb +75 -219
  12. data/lib/rjr/messages.rb +8 -0
  13. data/lib/rjr/messages/compressed.rb +264 -0
  14. data/lib/rjr/messages/notification.rb +95 -0
  15. data/lib/rjr/messages/request.rb +99 -0
  16. data/lib/rjr/messages/response.rb +128 -0
  17. data/lib/rjr/node.rb +100 -97
  18. data/lib/rjr/node_callback.rb +43 -0
  19. data/lib/rjr/nodes/amqp.rb +12 -11
  20. data/lib/rjr/nodes/easy.rb +4 -4
  21. data/lib/rjr/nodes/local.rb +13 -12
  22. data/lib/rjr/nodes/multi.rb +1 -1
  23. data/lib/rjr/nodes/tcp.rb +15 -13
  24. data/lib/rjr/nodes/template.rb +4 -4
  25. data/lib/rjr/nodes/unix.rb +15 -13
  26. data/lib/rjr/nodes/web.rb +15 -14
  27. data/lib/rjr/nodes/ws.rb +12 -11
  28. data/lib/rjr/request.rb +128 -0
  29. data/lib/rjr/result.rb +75 -0
  30. data/lib/rjr/util/args.rb +145 -0
  31. data/lib/rjr/{em_adapter.rb → util/em_adapter.rb} +0 -0
  32. data/lib/rjr/util/handles_methods.rb +115 -0
  33. data/lib/rjr/util/has_messages.rb +50 -0
  34. data/lib/rjr/{inspect.rb → util/inspect.rb} +1 -1
  35. data/lib/rjr/util/json_parser.rb +101 -0
  36. data/lib/rjr/util/logger.rb +128 -0
  37. data/lib/rjr/{thread_pool.rb → util/thread_pool.rb} +2 -0
  38. data/lib/rjr/version.rb +1 -1
  39. data/site/jrw.js +1 -1
  40. data/specs/args_spec.rb +144 -0
  41. data/specs/dispatcher_spec.rb +399 -211
  42. data/specs/em_adapter_spec.rb +31 -18
  43. data/specs/handles_methods_spec.rb +154 -0
  44. data/specs/has_messages_spec.rb +54 -0
  45. data/specs/inspect_spec.rb +1 -1
  46. data/specs/json_parser_spec.rb +169 -0
  47. data/specs/messages/notification_spec.rb +59 -0
  48. data/specs/messages/request_spec.rb +66 -0
  49. data/specs/messages/response_spec.rb +94 -0
  50. data/specs/node_callbacks_spec.rb +47 -0
  51. data/specs/node_spec.rb +465 -56
  52. data/specs/request_spec.rb +147 -0
  53. data/specs/result_spec.rb +144 -0
  54. data/specs/thread_pool_spec.rb +1 -1
  55. metadata +41 -11
  56. data/lib/rjr/errors.rb +0 -23
  57. data/lib/rjr/message.rb +0 -351
  58. data/lib/rjr/semaphore.rb +0 -58
  59. data/specs/message_spec.rb +0 -229
@@ -1,293 +1,481 @@
1
1
  require 'rjr/dispatcher'
2
2
 
3
3
  module RJR
4
- describe Result do
5
- describe "#initialize" do
6
- it "initializes default attributes" do
7
- result = Result.new
8
- result.result.should be_nil
9
- result.error_code.should be_nil
10
- result.error_msg.should be_nil
11
- result.error_class.should be_nil
12
- end
13
-
14
- it "stores result" do
15
- result = Result.new :result => 'foobar'
16
- result.result.should == 'foobar'
17
- end
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 "when an error code is specified" do
36
- it "should be marked as failed" do
37
- result = Result.new :error_code => 123
38
- result.success.should == false
39
- result.failed.should == true
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
- end # describe #initialize
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
- describe "#==" do
46
- it "return true for equal results"
47
- it "return false for inequal results"
48
- end # descirbe #==
37
+ it "initializes handlers" do
38
+ Dispatcher.new.handlers.should == {}
39
+ end
49
40
 
50
- end # describe Result
51
- end # module RJR
41
+ it "initializes environments" do
42
+ Dispatcher.new.environments.should == {}
43
+ end
52
44
 
53
- module RJR
54
- describe Request do
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
- it "should be convertable from json" do
82
- j = '{"json_class":"RJR::Request","data":{"request":{"rjr_method":"foobar","rjr_method_args":["a","b"],"rjr_headers":{"foo":"bar"},"rjr_node_type":"local","rjr_node_id":"loc1"},"result":{"result":42,"error_code":null,"error_msg":null,"error_class":null}}}'
83
- r = JSON.parse(j, :create_additions => true)
84
-
85
- r.class.should == RJR::Request
86
- r.rjr_method.should == 'foobar'
87
- r.rjr_method_args.should == ['a', 'b']
88
- r.rjr_headers.should == { 'foo' => 'bar' }
89
- r.rjr_node_type.should == 'local'
90
- r.rjr_node_id.should == 'loc1'
91
- r.result.result.should == 42
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
- context "handling" do
95
- it "should invoke handler in request context" do
96
- method = 'foobar'
97
- params = ['a', 1]
98
- ni = 'test'
99
- nt = 'test_type'
100
- cb = Object.new
101
- headers = {'header1' => 'val1'}
102
-
103
- invoked = ip1 = ip2 =
104
- icb = im = ini = int = ih = nil
105
- handler = proc { |p1, p2|
106
- invoked = true
107
- ip1 = p1
108
- ip2 = p2
109
- im = @rjr_method
110
- ini = @rjr_node_id
111
- int = @rjr_node_type
112
- icb = @rjr_callback
113
- ih = @rjr_headers
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
- module RJR
139
- describe Dispatcher do
140
- context "registering handlers" do
141
- it "should load module"
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 "should register handler for method" do
103
+ it "registers block for specifed signature" do
144
104
  d = Dispatcher.new
145
- h = proc {}
146
- d.handle('foobar', h)
147
- d.handlers['foobar'].should == h
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 "should set handler from block param" do
110
+ it "registers callback for specifed signatures" do
152
111
  d = Dispatcher.new
153
- h = proc {}
154
- d.handle('foobar', &h)
155
- d.handlers['foobar'].should == h
156
- d.handles?('foobar').should be_true
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 "should register handler for multiple methods" do
118
+ it "registers block for specifed signatures" do
160
119
  d = Dispatcher.new
161
- h = proc {}
162
- d.handle(['foobar', 'barfoo'], &h)
163
- d.handlers['foobar'].should == h
164
- d.handlers['barfoo'].should == h
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
- context "dispatching requests" do
171
- context "handler does not exist" do
172
- it "should return method not found" do
127
+ describe "#handler_for" do
128
+ context "dispatcher has handler" do
129
+ it "returns registered handler" do
173
130
  d = Dispatcher.new
174
- r = d.dispatch :rjr_method => 'foobar'
175
- r.should == Result.method_not_found('foobar')
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 "handler is registered" do
180
- it "should invoke handler" do
181
- invoked = false
137
+ context "dispatcher does not have handler" do
138
+ it "returns nil" do
182
139
  d = Dispatcher.new
183
- h = proc { invoked = true }
184
- d.handle('foobar', &h)
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
- d.dispatch :rjr_method => 'foobar'
187
- invoked.should be_true
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
- context "handler is regex" do
191
- it "should match method"
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
- it "should pass params to handler" do
195
- param = nil
174
+ context "dispatcher does not have handler" do
175
+ it "returns false" do
196
176
  d = Dispatcher.new
197
- h = proc { |p| param = p}
198
- d.handle('foobar', &h)
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
- d.dispatch(:rjr_method => 'foobar', :rjr_method_args => [42])
201
- param.should == 42
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
- it "should return request result" do
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
- h = proc { 42 }
207
- d.handle('foobar', &h)
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
- r = d.dispatch :rjr_method => 'foobar'
210
- r.result.should == 42
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
- it "should return request error" do
289
+ context "exception during request" do
290
+ it "returns exception error in result" do
214
291
  d = Dispatcher.new
215
- h = proc { raise ArgumentError, "bah" }
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
- r = d.dispatch :rjr_method => 'foobar'
219
- r.error_code.should == -32000
220
- r.error_msg.should == "bah"
221
- r.error_class.should == ArgumentError
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
- context "keep_requests is false (default)" do
225
- it "should not store requests" do
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.handle('foobar') {}
228
- d.requests.should be_empty
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 "keep_requests is true" do
235
- it "should store request locally" do
236
- d = Dispatcher.new :keep_requests => true
237
- h = proc {}
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
- d.requests.size.should == 1
243
- d.requests.first.rjr_method.should == 'foobar'
379
+ invoked.should be_true
244
380
  end
245
381
 
246
- it "should set params on request" do
382
+ it "should pass params to handler" do
383
+ param = nil
247
384
  d = Dispatcher.new
248
- d.keep_requests = true
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
- d.requests.first.rjr_method_args.should == [42]
389
+ param.should == 42
254
390
  end
255
391
 
256
- it "should set result on request" do
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
- d.requests.first.result.result.should == 42
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
- context "processing responses" do
270
- context "successful response" do
271
- it "should return result" do
272
- r = Result.new :result => 'woot'
273
- d = Dispatcher.new
274
- p = d.handle_response(r)
275
- p.should == "woot"
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
- context "failed response" do
280
- it "should raise error" do
281
- r = Result.new :error_code => 123, :error_msg => "bah",
282
- :error_class => ArgumentError
283
- d = Dispatcher.new
284
- lambda{
285
- d.handle_response(r)
286
- }.should raise_error(Exception, "bah")
287
- #}.should raise_error(ArgumentError, "bah")
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