2003-10-21 11:04:39 -04:00
|
|
|
require 'test/unit'
|
|
|
|
require 'drb/drb'
|
|
|
|
require 'drb/extservm'
|
|
|
|
require 'timeout'
|
2004-04-08 07:25:24 -04:00
|
|
|
begin
|
|
|
|
loadpath = $:.dup
|
|
|
|
$:.replace($: | [File.expand_path("../ruby", File.dirname(__FILE__))])
|
|
|
|
require 'envutil'
|
|
|
|
ensure
|
|
|
|
$:.replace(loadpath)
|
|
|
|
end
|
2003-10-21 11:04:39 -04:00
|
|
|
|
|
|
|
class DRbService
|
|
|
|
@@manager = DRb::ExtServManager.new
|
2004-04-08 06:45:21 -04:00
|
|
|
@@ruby = EnvUtil.rubybin
|
2003-10-29 08:06:12 -05:00
|
|
|
@@ruby += " -d" if $DEBUG
|
2003-10-21 11:04:39 -04:00
|
|
|
@@dir = File.dirname(File.expand_path(__FILE__))
|
2004-03-19 02:13:01 -05:00
|
|
|
def self.manager
|
|
|
|
@@manager
|
|
|
|
end
|
|
|
|
def self.add_service_command(nm)
|
|
|
|
DRb::ExtServManager.command[nm] = "#{@@ruby} #{@@dir}/#{nm}"
|
|
|
|
end
|
2003-10-21 11:04:39 -04:00
|
|
|
|
|
|
|
%w(ut_drb.rb ut_array.rb ut_port.rb ut_large.rb ut_safe1.rb ut_eval.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
|
|
|
|
@server = @@server = DRb::DRbServer.new(nil, @@manager, {})
|
|
|
|
def self.manager
|
|
|
|
@@manager
|
|
|
|
end
|
|
|
|
def self.server
|
|
|
|
@server || @@server
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
|
|
|
module DRbCore
|
|
|
|
def setup
|
|
|
|
@ext = DRbService.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)
|
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)
|
|
|
|
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)
|
|
|
|
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_raises(DRb::DRbUnknownError) do
|
|
|
|
@there.unknown_error
|
|
|
|
end
|
|
|
|
|
|
|
|
onecky = FailOnecky.new('3')
|
|
|
|
|
|
|
|
assert_raises(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_raises(ArgumentError) do
|
|
|
|
@there.sum(1, 1, 1, 1, 1, 1, 1, 1, 1)
|
|
|
|
end
|
|
|
|
assert_raises(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')
|
|
|
|
assert(!(@there.respond_to? "foobar"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_05_eq
|
|
|
|
a = @there.to_a[0]
|
|
|
|
b = @there.to_a[0]
|
|
|
|
assert(a.object_id != b.object_id)
|
|
|
|
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_raises(TimeoutError) do
|
|
|
|
@there.do_timeout(ten)
|
|
|
|
end
|
|
|
|
assert_raises(TimeoutError) do
|
|
|
|
@there.do_timeout(ten)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_07_public_private
|
|
|
|
assert_nothing_raised() {
|
|
|
|
begin
|
|
|
|
@there.method_missing(:eval)
|
|
|
|
rescue NameError
|
2003-10-21 23:53:41 -04:00
|
|
|
assert_match(/^private method \`eval\'/, $!.message)
|
2003-10-21 11:04:39 -04:00
|
|
|
end
|
|
|
|
}
|
|
|
|
assert_nothing_raised() {
|
|
|
|
begin
|
|
|
|
@there.method_missing(:undefined_method_test)
|
|
|
|
rescue NameError
|
2003-10-21 23:53:41 -04:00
|
|
|
assert_match(/^undefined method \`undefined_method_test\'/, $!.message)
|
2003-10-21 11:04:39 -04:00
|
|
|
end
|
|
|
|
}
|
|
|
|
assert_raises(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)
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
module DRbAry
|
|
|
|
def setup
|
|
|
|
@ext = DRbService.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
|
|
|
|
|
|
|
|
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
|