2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2003-10-21 11:04:39 -04:00
|
|
|
require 'test/unit'
|
|
|
|
require 'drb/drb'
|
|
|
|
require 'drb/extservm'
|
|
|
|
require 'timeout'
|
|
|
|
|
2014-05-31 23:07:58 -04:00
|
|
|
module DRbTests
|
|
|
|
|
2003-10-21 11:04:39 -04:00
|
|
|
class DRbService
|
|
|
|
@@manager = DRb::ExtServManager.new
|
2016-02-11 23:24:10 -05:00
|
|
|
@@ruby = [EnvUtil.rubybin]
|
|
|
|
@@ruby << "-d" if $DEBUG
|
2004-03-19 02:13:01 -05:00
|
|
|
def self.add_service_command(nm)
|
2004-07-28 05:05:08 -04:00
|
|
|
dir = File.dirname(File.expand_path(__FILE__))
|
2016-02-11 23:24:10 -05:00
|
|
|
DRb::ExtServManager.command[nm] = @@ruby + ["#{dir}/#{nm}"]
|
2004-03-19 02:13:01 -05:00
|
|
|
end
|
2003-10-21 11:04:39 -04:00
|
|
|
|
2017-10-09 07:20:00 -04:00
|
|
|
%w(ut_drb.rb ut_array.rb ut_port.rb ut_large.rb ut_safe1.rb ut_eq.rb).each do |nm|
|
2004-03-19 02:13:01 -05:00
|
|
|
add_service_command(nm)
|
2003-10-21 11:04:39 -04:00
|
|
|
end
|
2007-11-19 13:30:18 -05:00
|
|
|
@server = @@server = DRb::DRbServer.new('druby://localhost:0', @@manager, {})
|
2004-11-08 10:45:31 -05:00
|
|
|
@@manager.uri = @@server.uri
|
2003-10-21 11:04:39 -04:00
|
|
|
def self.manager
|
|
|
|
@@manager
|
|
|
|
end
|
|
|
|
def self.server
|
|
|
|
@server || @@server
|
|
|
|
end
|
2005-01-01 11:20:35 -05:00
|
|
|
def self.ext_service(name)
|
2015-07-13 06:07:01 -04:00
|
|
|
Timeout.timeout(100, RuntimeError) do
|
2005-01-01 11:20:35 -05:00
|
|
|
manager.service(name)
|
|
|
|
end
|
|
|
|
end
|
2014-06-02 07:34:50 -04:00
|
|
|
def self.finish
|
|
|
|
@server.instance_variable_get(:@grp).list.each {|th| th.join }
|
|
|
|
end
|
2003-10-21 11:04:39 -04:00
|
|
|
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
|
|
|
|
|
2013-06-19 03:47:38 -04:00
|
|
|
module DRbBase
|
|
|
|
def setup_service(service_name)
|
|
|
|
@service_name = service_name
|
2012-04-22 07:35:03 -04:00
|
|
|
@ext = DRbService.ext_service(@service_name)
|
2003-10-21 11:04:39 -04:00
|
|
|
@there = @ext.front
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2012-04-22 07:35:03 -04:00
|
|
|
@ext.stop_service if defined?(@ext) && @ext
|
|
|
|
DRbService.manager.unregist(@service_name)
|
2012-09-20 04:05:05 -04:00
|
|
|
while (@there&&@there.to_s rescue nil)
|
2012-07-18 18:39:28 -04:00
|
|
|
# nop
|
|
|
|
end
|
2012-09-29 07:19:02 -04:00
|
|
|
signal = /mswin|mingw/ =~ RUBY_PLATFORM ? :KILL : :TERM
|
2012-04-22 07:35:03 -04:00
|
|
|
Thread.list.each {|th|
|
|
|
|
if th.respond_to?(:pid) && th[:drb_service] == @service_name
|
2013-04-11 00:21:34 -04:00
|
|
|
10.times do
|
|
|
|
begin
|
|
|
|
Process.kill signal, th.pid
|
|
|
|
break
|
|
|
|
rescue Errno::ESRCH
|
|
|
|
break
|
|
|
|
rescue Errno::EPERM # on Windows
|
|
|
|
sleep 0.1
|
|
|
|
retry
|
|
|
|
end
|
2012-04-23 06:55:39 -04:00
|
|
|
end
|
2012-04-22 07:35:03 -04:00
|
|
|
th.join
|
|
|
|
end
|
|
|
|
}
|
2003-10-21 11:04:39 -04:00
|
|
|
end
|
2013-06-19 03:47:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
module DRbCore
|
|
|
|
include DRbBase
|
2003-10-21 11:04:39 -04:00
|
|
|
|
|
|
|
def test_00_DRbObject
|
|
|
|
ro = DRbObject.new(nil, 'druby://localhost:12345')
|
|
|
|
assert_equal('druby://localhost:12345', ro.__drburi)
|
|
|
|
assert_equal(nil, ro.__drbref)
|
2003-10-21 23:53:41 -04:00
|
|
|
|
2003-10-21 11:04:39 -04:00
|
|
|
ro = DRbObject.new_with_uri('druby://localhost:12345')
|
|
|
|
assert_equal('druby://localhost:12345', ro.__drburi)
|
|
|
|
assert_equal(nil, ro.__drbref)
|
2003-10-21 23:53:41 -04:00
|
|
|
|
2003-10-21 11:04:39 -04:00
|
|
|
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)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2017-04-07 09:26:12 -04:00
|
|
|
assert_respond_to(@there, [:to_a, true])
|
|
|
|
assert_respond_to(@there, [:eval, true])
|
|
|
|
assert_not_respond_to(@there, [:eval, false])
|
|
|
|
assert_not_respond_to(@there, :eval)
|
2003-10-21 11:04:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_01_02_loop
|
|
|
|
onecky = Onecky.new('3')
|
2003-10-21 23:53:41 -04:00
|
|
|
50.times do
|
2003-10-21 11:04:39 -04:00
|
|
|
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)
|
2014-05-31 23:07:58 -04:00
|
|
|
assert_equal('DRbTests::Unknown2', obj.name)
|
2003-10-21 11:04:39 -04:00
|
|
|
|
|
|
|
obj = @there.unknown_module
|
|
|
|
assert_kind_of(DRb::DRbUnknown, obj)
|
2014-05-31 23:07:58 -04:00
|
|
|
assert_equal('DRbTests::DRbEx::', obj.name)
|
2003-10-21 11:04:39 -04:00
|
|
|
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(DRb::DRbUnknownError) do
|
2003-10-21 11:04:39 -04:00
|
|
|
@there.unknown_error
|
|
|
|
end
|
|
|
|
|
|
|
|
onecky = FailOnecky.new('3')
|
|
|
|
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(FailOnecky::OneckyError) do
|
2003-10-21 11:04:39 -04:00
|
|
|
@there.sample(onecky, 1, 2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_03
|
|
|
|
assert_equal(8, @there.sum(1, 1, 1, 1, 1, 1, 1, 1))
|
2010-06-14 04:52:30 -04:00
|
|
|
assert_raise(DRb::DRbConnError) do
|
2003-10-21 11:04:39 -04:00
|
|
|
@there.sum(1, 1, 1, 1, 1, 1, 1, 1, 1)
|
|
|
|
end
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(DRb::DRbConnError) do
|
2003-10-30 08:30:48 -05:00
|
|
|
@there.sum('1' * 4096)
|
2003-10-21 11:04:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_04
|
|
|
|
assert_respond_to(@there, 'sum')
|
2017-04-07 09:26:12 -04:00
|
|
|
assert_not_respond_to(@there, "foobar")
|
2003-10-21 11:04:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_05_eq
|
|
|
|
a = @there.to_a[0]
|
|
|
|
b = @there.to_a[0]
|
2017-04-07 09:26:12 -04:00
|
|
|
assert_not_same(a, b)
|
2003-10-21 11:04:39 -04:00
|
|
|
assert_equal(a, b)
|
2017-04-07 09:26:12 -04:00
|
|
|
assert_equal(a, @there)
|
2003-10-21 11:04:39 -04:00
|
|
|
assert_equal(a.hash, b.hash)
|
|
|
|
assert_equal(a.hash, @there.hash)
|
2017-04-07 09:26:12 -04:00
|
|
|
assert_operator(a, :eql?, b)
|
|
|
|
assert_operator(a, :eql?, @there)
|
2003-10-21 11:04:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_06_timeout
|
|
|
|
ten = Onecky.new(10)
|
2014-10-07 16:00:09 -04:00
|
|
|
assert_raise(Timeout::Error) do
|
2003-10-21 11:04:39 -04:00
|
|
|
@there.do_timeout(ten)
|
|
|
|
end
|
2014-10-07 16:00:09 -04:00
|
|
|
assert_raise(Timeout::Error) do
|
2003-10-21 11:04:39 -04:00
|
|
|
@there.do_timeout(ten)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-11 21:47:29 -05:00
|
|
|
def test_07_private_missing
|
|
|
|
e = assert_raise(NoMethodError) {
|
|
|
|
@there.method_missing(:eval, 'nil')
|
2003-10-21 11:04:39 -04:00
|
|
|
}
|
2015-02-11 21:47:29 -05:00
|
|
|
assert_match(/^private method \`eval\'/, e.message)
|
|
|
|
|
|
|
|
e = assert_raise(NoMethodError) {
|
|
|
|
@there.call_private_method
|
2005-07-31 12:19:42 -04:00
|
|
|
}
|
2015-02-11 21:47:29 -05:00
|
|
|
assert_match(/^private method \`call_private_method\'/, e.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_07_protected_missing
|
|
|
|
e = assert_raise(NoMethodError) {
|
|
|
|
@there.call_protected_method
|
2005-07-31 12:19:42 -04:00
|
|
|
}
|
2015-02-11 21:47:29 -05:00
|
|
|
assert_match(/^protected method \`call_protected_method\'/, e.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_07_public_missing
|
|
|
|
e = assert_raise(NoMethodError) {
|
|
|
|
@there.method_missing(:undefined_method_test)
|
2003-10-21 11:04:39 -04:00
|
|
|
}
|
2015-02-11 21:47:29 -05:00
|
|
|
assert_match(/^undefined method \`undefined_method_test\'/, e.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_07_send_missing
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(DRb::DRbConnError) do
|
2003-10-21 11:04:39 -04:00
|
|
|
@there.method_missing(:__send__, :to_s)
|
|
|
|
end
|
2005-07-31 12:19:42 -04:00
|
|
|
assert_equal(true, @there.missing)
|
2003-10-21 11:04:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_08_here
|
|
|
|
ro = DRbObject.new(nil, DRb.uri)
|
|
|
|
assert_kind_of(String, ro.to_s)
|
2003-10-21 23:53:41 -04:00
|
|
|
|
2003-10-21 11:04:39 -04:00
|
|
|
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
|
|
|
|
|
2003-10-30 08:30:48 -05:00
|
|
|
def test_10_yield
|
|
|
|
@there.simple_hash.each do |k, v|
|
|
|
|
assert_kind_of(String, k)
|
|
|
|
assert_kind_of(Symbol, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-10-21 11:04:39 -04:00
|
|
|
def test_10_yield_undumped
|
2003-10-30 08:30:48 -05:00
|
|
|
@there.xarray2_hash.each do |k, v|
|
2003-10-21 11:04:39 -04:00
|
|
|
assert_kind_of(String, k)
|
|
|
|
assert_kind_of(DRbObject, v)
|
|
|
|
end
|
|
|
|
end
|
2004-12-11 07:12:17 -05:00
|
|
|
|
|
|
|
def test_11_remote_no_method_error
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(DRb::DRbRemoteError) do
|
2004-12-11 07:12:17 -05:00
|
|
|
@there.remote_no_method_error
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
@there.remote_no_method_error
|
|
|
|
rescue
|
|
|
|
error = $!
|
|
|
|
assert_match(/^undefined method .*\(NoMethodError\)/, error.message)
|
|
|
|
assert_equal('NoMethodError', error.reason)
|
|
|
|
end
|
|
|
|
end
|
2003-10-21 11:04:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
module DRbAry
|
2013-06-19 03:47:38 -04:00
|
|
|
include DRbBase
|
2003-10-21 11:04:39 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2007-12-19 03:46:49 -05:00
|
|
|
# retry in block is not supported on ruby 1.9
|
|
|
|
#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
|
2003-10-21 11:04:39 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
2014-05-31 23:07:58 -04:00
|
|
|
|
|
|
|
end
|