summaryrefslogtreecommitdiff
path: root/test/drb/test_drb.rb
blob: 437e2b4560fe72dc8275fb5d73fa523bd84cb392 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
require 'rubyunit'
require 'runit/cui/testrunner'
require 'rbconfig'
require 'drb/drb'
require 'drb/extservm'
require 'timeout'

class TestService
  @@scripts = %w(ut_drb.rb ut_array.rb ut_port.rb ut_large.rb ut_safe1.rb ut_eval.rb)

  def initialize(uri=nil, config={})
    ruby = Config::CONFIG["RUBY_INSTALL_NAME"]
    @manager = DRb::ExtServManager.new
    @@scripts.each do |nm|
      DRb::ExtServManager.command[nm] = "#{ruby} #{nm}"
    end
    @server = DRb::DRbServer.new(uri, @manager, config)
  end
  attr_reader :manager, :server
end

class Onecky
  include DRbUndumped
  def initialize(n)
    @num = n
  end

  def to_i
    @num.to_i
  end

  def sleep(n)
    Kernel.sleep(n)
    to_i
  end
end

class FailOnecky < Onecky
  class OneckyError < RuntimeError; end
  def to_i
    raise(OneckyError, @num.to_s)
  end
end

class XArray < Array
  def initialize(ary)
    ary.each do |x|
      self.push(x)
    end
  end
end

class DRbCoreTest < RUNIT::TestCase
  def setup
    @ext = $manager.service('ut_drb.rb')
    @there = @ext.front
  end

  def teardown
    @ext.stop_service
  end

  def test_00_DRbObject
    ro = DRbObject.new(nil, 'druby://localhost:12345')
    assert_equal('druby://localhost:12345', ro.__drburi)
    assert_equal(nil, ro.__drbref)
    
    ro = DRbObject.new_with_uri('druby://localhost:12345')
    assert_equal('druby://localhost:12345', ro.__drburi)
    assert_equal(nil, ro.__drbref)
    
    ro = DRbObject.new_with_uri('druby://localhost:12345?foobar')
    assert_equal('druby://localhost:12345', ro.__drburi)
    assert_equal(DRb::DRbURIOption.new('foobar'), ro.__drbref)
  end

  def test_01
    assert_equal("hello", @there.hello)
    onecky = Onecky.new('3')
    assert_equal(6, @there.sample(onecky, 1, 2))
    ary = @there.to_a
    assert_kind_of(DRb::DRbObject, ary)
  end

  def test_01_02_loop
    onecky = Onecky.new('3')
    50.times do 
      assert_equal(6, @there.sample(onecky, 1, 2))
      ary = @there.to_a
      assert_kind_of(DRb::DRbObject, ary)
    end
  end

  def test_02_unknown
    obj = @there.unknown_class
    assert_kind_of(DRb::DRbUnknown, obj)
    assert_equal('Unknown2', obj.name)

    obj = @there.unknown_module
    assert_kind_of(DRb::DRbUnknown, obj)
    if RUBY_VERSION >= '1.8'
      assert_equal('DRbEx::', obj.name)
    else
      assert_equal('DRbEx', obj.name)
    end

    assert_exception(DRb::DRbUnknownError) do
      @there.unknown_error
    end

    onecky = FailOnecky.new('3')

    assert_exception(FailOnecky::OneckyError) do
      @there.sample(onecky, 1, 2)
    end
  end

  def test_03
    assert_equal(8, @there.sum(1, 1, 1, 1, 1, 1, 1, 1))
    assert_exception(ArgumentError) do
      @there.sum(1, 1, 1, 1, 1, 1, 1, 1, 1)
    end
    assert_exception(DRb::DRbConnError) do
      @there.sum('1' * 2048)
    end
  end

  def test_04
    assert_respond_to('sum', @there)
    assert(!(@there.respond_to? "foobar"))
  end

  def test_05_eq
    a = @there.to_a[0]
    b = @there.to_a[0]
    assert(a.id != b.id)
    assert(a != b)
    assert(a.hash != b.hash)
    assert(! a.eql?(b))
    require 'drb/eq'
    assert(a == b)
    assert_equal(a, b)
    assert(a == @there)
    assert_equal(a.hash, b.hash)
    assert_equal(a.hash, @there.hash)
    assert(a.eql?(b))
    assert(a.eql?(@there))
  end

  def test_06_timeout
    ten = Onecky.new(10)
    assert_exception(TimeoutError) do
      @there.do_timeout(ten)
    end
    assert_exception(TimeoutError) do
      @there.do_timeout(ten)
    end
  end

  def test_07_public_private
    assert_no_exception() {
      begin
	@there.method_missing(:eval)
      rescue NameError
	assert_match($!.message, /^private method `eval'/)
      end
    }
    assert_no_exception() {
      begin
	@there.method_missing(:undefined_method_test)
      rescue NameError
	assert_match($!.message, /^undefined method `undefined_method_test'/)
      end
    }
    assert_exception(SecurityError) do
      @there.method_missing(:__send__, :to_s)
    end
  end

  def test_08_here
    ro = DRbObject.new(nil, DRb.uri)
    assert_kind_of(String, ro.to_s)
    
    ro = DRbObject.new_with_uri(DRb.uri)
    assert_kind_of(String, ro.to_s)
  end

  def uri_concat_option(uri, opt)
    "#{uri}?#{opt}"
  end

  def test_09_option
    uri = uri_concat_option(@there.__drburi, "foo")
    ro = DRbObject.new_with_uri(uri)
    assert_equal(ro.__drburi, @there.__drburi)
    assert_equal(3, ro.size)

    uri = uri_concat_option(@there.__drburi, "")
    ro = DRbObject.new_with_uri(uri)
    assert_equal(ro.__drburi, @there.__drburi)
    assert_equal(DRb::DRbURIOption.new(''), ro.__drbref)

    uri = uri_concat_option(@there.__drburi, "hello?world")
    ro = DRbObject.new_with_uri(uri)
    assert_equal(DRb::DRbURIOption.new('hello?world'), ro.__drbref)

    uri = uri_concat_option(@there.__drburi, "?hello?world")
    ro = DRbObject.new_with_uri(uri)
    assert_equal(DRb::DRbURIOption.new('?hello?world'), ro.__drbref)
  end

  def test_10_yield_undumped
    @there.xarray2_hash.each do |k, v|
      assert_kind_of(String, k)
      assert_kind_of(DRbObject, v)
    end
  end
end

class DRbYieldTest < RUNIT::TestCase
  def setup
    @ext = $manager.service('ut_drb.rb')
    @there = @ext.front
  end

  def teardown
    @ext.stop_service
  end

  def test_01_one
    one = nil
    @there.echo_yield_1([]) {|one|}
    assert_equal([], one)
    
    one = nil
    @there.echo_yield_1(1) {|one|}
    assert_equal(1, one)
    
    one = nil
    @there.echo_yield_1(nil) {|one|}
    assert_equal(nil, one)
  end

  def test_02_two
    one = two = nil
    @there.echo_yield_2([], []) {|one, two|}
    assert_equal([], one)
    assert_equal([], two)

    one = two = nil
    @there.echo_yield_2(1, 2) {|one, two|}
    assert_equal(1, one)
    assert_equal(2, two)

    one = two = nil
    @there.echo_yield_2(3, nil) {|one, two|}
    assert_equal(3, one)
    assert_equal(nil, two)
  end

  def test_03_many
    s = nil
    @there.echo_yield_0 {|*s|}
    assert_equal([], s)
    @there.echo_yield(nil) {|*s|}
    assert_equal([nil], s)
    @there.echo_yield(1) {|*s|}
    assert_equal([1], s)
    @there.echo_yield(1, 2) {|*s|}
    assert_equal([1, 2], s)
    @there.echo_yield(1, 2, 3) {|*s|}
    assert_equal([1, 2, 3], s)
    @there.echo_yield([], []) {|*s|}
    assert_equal([[], []], s)
    @there.echo_yield([]) {|*s|}
    if RUBY_VERSION >= '1.8'
      assert_equal([[]], s) # !
    else
      assert_equal([], s) # !
    end
  end

  def test_04_many_to_one
    s = nil
    @there.echo_yield_0 {|s|}
    assert_equal(nil, s)
    @there.echo_yield(nil) {|s|}
    assert_equal(nil, s)
    @there.echo_yield(1) {|s|}
    assert_equal(1, s)
    @there.echo_yield(1, 2) {|s|}
    assert_equal([1, 2], s)
    @there.echo_yield(1, 2, 3) {|s|}
    assert_equal([1, 2, 3], s)
    @there.echo_yield([], []) {|s|}
    assert_equal([[], []], s)
    @there.echo_yield([]) {|s|}
    assert_equal([], s)
  end

  def test_05_array_subclass
    @there.xarray_each {|x| assert_kind_of(XArray, x)}
    if RUBY_VERSION >= '1.8'
      @there.xarray_each {|*x| assert_kind_of(XArray, x[0])}
    end
  end
end

class RubyYieldTest < DRbYieldTest
  def echo_yield(*arg)
    yield(*arg)
  end

  def echo_yield_0
    yield
  end

  def echo_yield_1(a)
    yield(a)
  end

  def echo_yield_2(a, b)
    yield(a, b)
  end

  def xarray_each
    xary = [XArray.new([0])]
    xary.each do |x|
      yield(x)
    end
  end

  def setup
    @there = self
  end
  
  def teardown
  end
end

class Ruby18YieldTest < RubyYieldTest
  class YieldTest18
    def echo_yield(*arg, &proc)
      proc.call(*arg)
    end
    
    def echo_yield_0(&proc)
      proc.call
    end
    
    def echo_yield_1(a, &proc)
      proc.call(a)
    end
    
    def echo_yield_2(a, b, &proc)
      proc.call(a, b)
    end

    def xarray_each(&proc)
      xary = [XArray.new([0])]
      xary.each(&proc)
    end

  end

  def setup
    @there = YieldTest18.new
  end
end

class DRbAryTest < RUNIT::TestCase
  def setup
    @ext = $manager.service('ut_array.rb')
    @there = @ext.front
  end

  def teardown
    @ext.stop_service
  end

  def test_01
    assert_kind_of(DRb::DRbObject, @there)
  end

  def test_02_collect
    ary = @there.collect do |x| x + x end
    assert_kind_of(Array, ary)
    assert_equal([2, 4, 'IIIIII', 8, 'fivefive', 12], ary)
  end

  def test_03_redo
    ary = []
    count = 0
    @there.each do |x|
      count += 1
      ary.push x
      redo if count == 3
    end
    assert_equal([1, 2, 'III', 'III', 4, 'five', 6], ary)
  end

  def test_04_retry
    retried = false
    ary = []
    @there.each do |x|
      ary.push x
      if x == 4 && !retried
	retried = true
	retry
      end
    end
    assert_equal([1, 2, 'III', 4, 1, 2, 'III', 4, 'five', 6], ary)
  end

  def test_05_break
    ary = []
    @there.each do |x|
      ary.push x
      break if x == 4
    end
    assert_equal([1, 2, 'III', 4], ary)
  end

  def test_06_next
    ary = []
    @there.each do |x|
      next if String === x
      ary.push x
    end
    assert_equal([1, 2, 4, 6], ary)
  end

  if RUBY_VERSION >= '1.8'
    class_eval <<EOS
  def test_07_break_18
    ary = []
    result = @there.each do |x|
      ary.push x
      break(:done) if x == 4
    end
    assert_equal([1, 2, 'III', 4], ary)
    assert_equal(:done, result)
  end
EOS
  end

end

class DRbMServerTest < RUNIT::TestCase
  def setup
    @ext = $manager.service('ut_drb.rb')
    @there = @ext.front
    @server = (1..3).collect do |n|
      DRb::DRbServer.new(nil, Onecky.new(n.to_s))
    end
  end

  def teardown
    @server.each do |s|
      s.stop_service
    end
    @ext.stop_service
  end

  def test_01
    assert_equal(6, @there.sample(@server[0].front, @server[1].front, @server[2].front))
  end
end

class DRbReusePortTest < DRbAryTest
  def setup
    sleep 1
    @ext = $manager.service('ut_port.rb')
    @there = @ext.front
  end
end

class DRbSafe1Test < DRbAryTest
  def setup
    sleep 1
    @ext = $manager.service('ut_safe1.rb')
    @there = @ext.front
  end
end

class DRbEvalTest < RUNIT::TestCase
  def setup
    super
    sleep 1
    @ext = $manager.service('ut_eval.rb')
    @there = @ext.front
  end

  def teardown
    @ext.stop_service
  end
  
  def test_01_safe1_eval
    assert_exception(SecurityError) do
      @there.method_missing(:instance_eval, 'ENV.inspect')
    end

    assert_exception(SecurityError) do
      @there.method_missing(:send, :eval, 'ENV.inspect')
    end

    remote_class = @there.remote_class

    assert_exception(SecurityError) do
      remote_class.class_eval('ENV.inspect')
    end

    assert_exception(SecurityError) do
      remote_class.module_eval('ENV.inspect')
    end
  end
end

class DRbLargeTest < RUNIT::TestCase
  def setup
    sleep 1
    @ext = $manager.service('ut_large.rb')
    @there = @ext.front
  end

  def teardown
    @ext.stop_service
  end

  def test_01_large_ary
    ary = [2] * 10240
    assert_equal(10240, @there.size(ary))
    assert_equal(20480, @there.sum(ary))
  end

  def test_02_large_ary
    ary = ["Hello, World"] * 10240
    assert_equal(10240, @there.size(ary))
  end

  def test_03_large_ary
    ary = [Thread.current] * 10240
    assert_equal(10240, @there.size(ary))
  end

  def test_04_many_arg
    assert_exception(ArgumentError) {
      @there.arg_test(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
    }
  end

  def test_05_too_large_ary
    ary = ["Hello, World"] * 102400
    exception = nil
    begin
      @there.size(ary)      
    rescue StandardError
      exception = $!
    end
    assert_kind_of(StandardError, exception)
  end
end

if __FILE__ == $0
  $testservice = TestService.new
  $manager = $testservice.manager

  RUNIT::CUI::TestRunner.run(DRbCoreTest.suite)
  RUNIT::CUI::TestRunner.run(DRbEvalTest.suite)
  RUNIT::CUI::TestRunner.run(RubyYieldTest.suite)
  if RUBY_VERSION >= '1.8'
    RUNIT::CUI::TestRunner.run(Ruby18YieldTest.suite)
  end
  RUNIT::CUI::TestRunner.run(DRbYieldTest.suite)
  RUNIT::CUI::TestRunner.run(DRbAryTest.suite)
  RUNIT::CUI::TestRunner.run(DRbMServerTest.suite)
  RUNIT::CUI::TestRunner.run(DRbSafe1Test.suite)
  RUNIT::CUI::TestRunner.run(DRbReusePortTest.suite)
  RUNIT::CUI::TestRunner.run(DRbLargeTest.suite)
end