2004-02-18 08:44:06 -05:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
require 'drb/drb'
|
|
|
|
require 'drb/eq'
|
|
|
|
require 'rinda/tuplespace'
|
|
|
|
|
2005-01-05 10:03:04 -05:00
|
|
|
require 'singleton'
|
|
|
|
|
2004-02-18 08:44:06 -05:00
|
|
|
module Rinda
|
|
|
|
|
2005-01-05 10:03:04 -05:00
|
|
|
class MockClock
|
|
|
|
include Singleton
|
|
|
|
|
|
|
|
class MyTS < Rinda::TupleSpace
|
2008-07-26 20:04:38 -04:00
|
|
|
def keeper_thread
|
2005-01-05 10:03:04 -05:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-01-05 10:03:04 -05:00
|
|
|
def initialize
|
|
|
|
@now = 2
|
2008-07-26 20:04:38 -04:00
|
|
|
@reso = 1
|
2005-01-05 10:03:04 -05:00
|
|
|
@ts = MyTS.new
|
|
|
|
@ts.write([2, :now])
|
|
|
|
@inf = 2**31 - 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def now
|
|
|
|
@now.to_f
|
|
|
|
end
|
|
|
|
|
|
|
|
def at(n)
|
|
|
|
n
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-07-26 20:04:38 -04:00
|
|
|
def _forward(n=nil)
|
2005-01-05 10:03:04 -05:00
|
|
|
now ,= @ts.take([nil, :now])
|
|
|
|
@now = now + n
|
|
|
|
n = @reso if n.nil?
|
|
|
|
@ts.write([@now, :now])
|
|
|
|
end
|
|
|
|
|
2008-07-26 20:04:38 -04:00
|
|
|
def forward(n)
|
2005-01-05 10:03:04 -05:00
|
|
|
while n > 0
|
|
|
|
_forward(@reso)
|
|
|
|
n -= @reso
|
2008-07-26 20:04:38 -04:00
|
|
|
Thread.pass
|
2005-01-05 10:03:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def rewind
|
|
|
|
now ,= @ts.take([nil, :now])
|
|
|
|
@ts.write([@inf, :now])
|
|
|
|
@ts.take([nil, :now])
|
|
|
|
@now = 2
|
|
|
|
@ts.write([2, :now])
|
|
|
|
end
|
|
|
|
|
2008-07-26 20:04:38 -04:00
|
|
|
def sleep(n=nil)
|
2005-01-05 10:03:04 -05:00
|
|
|
now ,= @ts.read([nil, :now])
|
|
|
|
@ts.read([(now + n)..@inf, :now])
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module Time
|
|
|
|
def sleep(n)
|
|
|
|
@m.sleep(n)
|
|
|
|
end
|
|
|
|
module_function :sleep
|
|
|
|
|
|
|
|
def at(n)
|
|
|
|
n
|
|
|
|
end
|
|
|
|
module_function :at
|
|
|
|
|
|
|
|
def now
|
|
|
|
@m ? @m.now : 2
|
|
|
|
end
|
|
|
|
module_function :now
|
|
|
|
|
|
|
|
def rewind
|
|
|
|
@m.rewind
|
|
|
|
end
|
|
|
|
module_function :rewind
|
|
|
|
|
|
|
|
def forward(n)
|
|
|
|
@m.forward(n)
|
|
|
|
end
|
|
|
|
module_function :forward
|
|
|
|
|
|
|
|
@m = MockClock.instance
|
|
|
|
end
|
|
|
|
|
|
|
|
class TupleSpace
|
|
|
|
def sleep(n)
|
|
|
|
Time.sleep(n)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-02-18 08:44:06 -05:00
|
|
|
module TupleSpaceTestModule
|
2005-01-05 10:03:04 -05:00
|
|
|
def sleep(n)
|
|
|
|
if Thread.current == Thread.main
|
|
|
|
Time.forward(n)
|
|
|
|
else
|
|
|
|
Time.sleep(n)
|
|
|
|
end
|
|
|
|
end
|
2008-07-26 20:04:38 -04:00
|
|
|
|
|
|
|
def thread_join(th)
|
|
|
|
while th.alive?
|
|
|
|
Kernel.sleep(0.1)
|
|
|
|
sleep(1)
|
|
|
|
end
|
|
|
|
th.value
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2004-02-18 08:44:06 -05:00
|
|
|
def test_00_tuple
|
|
|
|
tuple = Rinda::TupleEntry.new([1,2,3])
|
|
|
|
assert(!tuple.canceled?)
|
|
|
|
assert(!tuple.expired?)
|
|
|
|
assert(tuple.alive?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_00_template
|
|
|
|
tmpl = Rinda::Template.new([1,2,3])
|
|
|
|
assert_equal(3, tmpl.size)
|
|
|
|
assert_equal(3, tmpl[2])
|
|
|
|
assert(tmpl.match([1,2,3]))
|
|
|
|
assert(!tmpl.match([1,nil,3]))
|
2004-04-21 10:05:40 -04:00
|
|
|
|
2004-02-18 08:44:06 -05:00
|
|
|
tmpl = Rinda::Template.new([/^rinda/i, nil, :hello])
|
|
|
|
assert_equal(3, tmpl.size)
|
|
|
|
assert(tmpl.match(['Rinda', 2, :hello]))
|
|
|
|
assert(!tmpl.match(['Rinda', 2, Symbol]))
|
|
|
|
assert(!tmpl.match([1, 2, :hello]))
|
2004-04-21 10:05:40 -04:00
|
|
|
assert(tmpl.match([/^rinda/i, 2, :hello]))
|
2004-02-18 08:44:06 -05:00
|
|
|
|
|
|
|
tmpl = Rinda::Template.new([Symbol])
|
|
|
|
assert_equal(1, tmpl.size)
|
|
|
|
assert(tmpl.match([:hello]))
|
2004-04-13 10:06:50 -04:00
|
|
|
assert(tmpl.match([Symbol]))
|
2004-02-18 08:44:06 -05:00
|
|
|
assert(!tmpl.match(['Symbol']))
|
|
|
|
|
|
|
|
tmpl = Rinda::Template.new({"message"=>String, "name"=>String})
|
|
|
|
assert_equal(2, tmpl.size)
|
|
|
|
assert(tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
|
2004-04-21 10:05:40 -04:00
|
|
|
assert(!tmpl.match({"message"=>"Hello", "name"=>"Foo", "1"=>2}))
|
|
|
|
assert(!tmpl.match({"message"=>"Hi", "name"=>"Foo", "age"=>1}))
|
2004-02-18 08:44:06 -05:00
|
|
|
assert(!tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
|
|
|
|
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(Rinda::InvalidHashTupleKey) do
|
2004-04-06 11:26:25 -04:00
|
|
|
tmpl = Rinda::Template.new({:message=>String, "name"=>String})
|
|
|
|
end
|
|
|
|
tmpl = Rinda::Template.new({"name"=>String})
|
2004-02-18 08:44:06 -05:00
|
|
|
assert_equal(1, tmpl.size)
|
2004-04-21 10:05:40 -04:00
|
|
|
assert(tmpl.match({"name"=>"Foo"}))
|
|
|
|
assert(!tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
|
|
|
|
assert(!tmpl.match({"message"=>:symbol, "name"=>"Foo", "1"=>2}))
|
|
|
|
assert(!tmpl.match({"message"=>"Hi", "name"=>"Foo", "age"=>1}))
|
2004-02-18 08:44:06 -05:00
|
|
|
assert(!tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
|
|
|
|
|
2004-04-21 10:05:40 -04:00
|
|
|
tmpl = Rinda::Template.new({"message"=>String, "name"=>String})
|
2004-02-18 08:44:06 -05:00
|
|
|
assert_equal(2, tmpl.size)
|
|
|
|
assert(tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
|
2004-04-06 11:26:25 -04:00
|
|
|
assert(!tmpl.match({"message"=>"Hello", "name"=>"Foo", "1"=>2}))
|
|
|
|
assert(!tmpl.match({"message"=>"Hi", "name"=>"Foo", "age"=>1}))
|
|
|
|
assert(!tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
|
|
|
|
|
2004-04-21 10:05:40 -04:00
|
|
|
tmpl = Rinda::Template.new({"message"=>String})
|
2004-04-06 11:26:25 -04:00
|
|
|
assert_equal(1, tmpl.size)
|
2004-04-21 10:05:40 -04:00
|
|
|
assert(tmpl.match({"message"=>"Hello"}))
|
|
|
|
assert(!tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
|
2004-04-06 11:26:25 -04:00
|
|
|
assert(!tmpl.match({"message"=>"Hello", "name"=>"Foo", "1"=>2}))
|
|
|
|
assert(!tmpl.match({"message"=>"Hi", "name"=>"Foo", "age"=>1}))
|
2004-04-21 10:05:40 -04:00
|
|
|
assert(!tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
|
2004-04-06 11:26:25 -04:00
|
|
|
|
|
|
|
tmpl = Rinda::Template.new({"message"=>String, "name"=>nil})
|
|
|
|
assert_equal(2, tmpl.size)
|
|
|
|
assert(tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
|
|
|
|
assert(!tmpl.match({"message"=>"Hello", "name"=>"Foo", "1"=>2}))
|
|
|
|
assert(!tmpl.match({"message"=>"Hi", "name"=>"Foo", "age"=>1}))
|
2004-04-21 10:05:40 -04:00
|
|
|
assert(!tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
|
2004-04-06 11:26:25 -04:00
|
|
|
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(Rinda::InvalidHashTupleKey) do
|
2004-04-06 11:26:25 -04:00
|
|
|
@ts.write({:message=>String, "name"=>String})
|
|
|
|
end
|
2004-04-21 10:05:40 -04:00
|
|
|
|
|
|
|
@ts.write([1, 2, 3])
|
|
|
|
assert_equal([1, 2, 3], @ts.take([1, 2, 3]))
|
|
|
|
|
|
|
|
@ts.write({'1'=>1, '2'=>2, '3'=>3})
|
|
|
|
assert_equal({'1'=>1, '2'=>2, '3'=>3}, @ts.take({'1'=>1, '2'=>2, '3'=>3}))
|
|
|
|
|
|
|
|
entry = @ts.write(['1'=>1, '2'=>2, '3'=>3])
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(Rinda::RequestExpiredError) do
|
2004-04-21 10:05:40 -04:00
|
|
|
assert_equal({'1'=>1, '2'=>2, '3'=>3}, @ts.read({'1'=>1}, 0))
|
|
|
|
end
|
|
|
|
entry.cancel
|
2004-02-18 08:44:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_00_DRbObject
|
|
|
|
ro = DRbObject.new(nil, "druby://host:1234")
|
|
|
|
tmpl = Rinda::DRbObjectTemplate.new
|
|
|
|
assert(tmpl === ro)
|
|
|
|
|
|
|
|
tmpl = Rinda::DRbObjectTemplate.new("druby://host:1234")
|
|
|
|
assert(tmpl === ro)
|
|
|
|
|
|
|
|
tmpl = Rinda::DRbObjectTemplate.new("druby://host:12345")
|
|
|
|
assert(!(tmpl === ro))
|
|
|
|
|
|
|
|
tmpl = Rinda::DRbObjectTemplate.new(/^druby:\/\/host:/)
|
|
|
|
assert(tmpl === ro)
|
|
|
|
|
2005-03-29 08:21:59 -05:00
|
|
|
ro = DRbObject.new_with(12345, 1234)
|
2004-02-18 08:44:06 -05:00
|
|
|
assert(!(tmpl === ro))
|
|
|
|
|
2005-03-29 08:21:59 -05:00
|
|
|
ro = DRbObject.new_with("druby://foo:12345", 1234)
|
2004-02-18 08:44:06 -05:00
|
|
|
assert(!(tmpl === ro))
|
|
|
|
|
|
|
|
tmpl = Rinda::DRbObjectTemplate.new(/^druby:\/\/(foo|bar):/)
|
|
|
|
assert(tmpl === ro)
|
|
|
|
|
2005-03-29 08:21:59 -05:00
|
|
|
ro = DRbObject.new_with("druby://bar:12345", 1234)
|
2004-02-18 08:44:06 -05:00
|
|
|
assert(tmpl === ro)
|
|
|
|
|
2005-03-29 08:21:59 -05:00
|
|
|
ro = DRbObject.new_with("druby://baz:12345", 1234)
|
2004-02-18 08:44:06 -05:00
|
|
|
assert(!(tmpl === ro))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_inp_rdp
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(Rinda::RequestExpiredError) do
|
2004-02-18 08:44:06 -05:00
|
|
|
@ts.take([:empty], 0)
|
|
|
|
end
|
|
|
|
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(Rinda::RequestExpiredError) do
|
2004-02-18 08:44:06 -05:00
|
|
|
@ts.read([:empty], 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-07-26 20:04:38 -04:00
|
|
|
def test_ruby_talk_264062
|
|
|
|
th = Thread.new { @ts.take([:empty], 1) }
|
|
|
|
sleep(10)
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(Rinda::RequestExpiredError) do
|
2008-07-26 20:04:38 -04:00
|
|
|
thread_join(th)
|
|
|
|
end
|
|
|
|
|
|
|
|
th = Thread.new { @ts.read([:empty], 1) }
|
|
|
|
sleep(10)
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(Rinda::RequestExpiredError) do
|
2008-07-26 20:04:38 -04:00
|
|
|
thread_join(th)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_symbol_tuple
|
|
|
|
@ts.write([:symbol, :symbol])
|
|
|
|
@ts.write(['string', :string])
|
|
|
|
assert_equal([[:symbol, :symbol]], @ts.read_all([:symbol, nil]))
|
|
|
|
assert_equal([[:symbol, :symbol]], @ts.read_all([Symbol, nil]))
|
|
|
|
assert_equal([], @ts.read_all([:nil, nil]))
|
|
|
|
end
|
|
|
|
|
2004-02-18 08:44:06 -05:00
|
|
|
def test_core_01
|
|
|
|
5.times do |n|
|
|
|
|
@ts.write([:req, 2])
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal([[:req, 2], [:req, 2], [:req, 2], [:req, 2], [:req, 2]],
|
|
|
|
@ts.read_all([nil, nil]))
|
|
|
|
|
|
|
|
taker = Thread.new do
|
|
|
|
s = 0
|
|
|
|
while true
|
|
|
|
begin
|
2008-07-26 20:04:38 -04:00
|
|
|
tuple = @ts.take([:req, Integer], 1)
|
2004-02-18 08:44:06 -05:00
|
|
|
assert_equal(2, tuple[1])
|
|
|
|
s += tuple[1]
|
|
|
|
rescue Rinda::RequestExpiredError
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@ts.write([:ans, s])
|
|
|
|
s
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-07-26 20:04:38 -04:00
|
|
|
assert_equal(10, thread_join(taker))
|
2005-01-05 10:03:04 -05:00
|
|
|
tuple = @ts.take([:ans, nil])
|
2004-02-18 08:44:06 -05:00
|
|
|
assert_equal(10, tuple[1])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_core_02
|
|
|
|
taker = Thread.new do
|
|
|
|
s = 0
|
|
|
|
while true
|
|
|
|
begin
|
2008-07-26 20:04:38 -04:00
|
|
|
tuple = @ts.take([:req, Integer], 1)
|
2004-02-18 08:44:06 -05:00
|
|
|
assert_equal(2, tuple[1])
|
|
|
|
s += tuple[1]
|
|
|
|
rescue Rinda::RequestExpiredError
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@ts.write([:ans, s])
|
|
|
|
s
|
|
|
|
end
|
|
|
|
|
|
|
|
5.times do |n|
|
|
|
|
@ts.write([:req, 2])
|
|
|
|
end
|
|
|
|
|
2008-07-26 20:04:38 -04:00
|
|
|
assert_equal(10, thread_join(taker))
|
2005-01-05 10:03:04 -05:00
|
|
|
tuple = @ts.take([:ans, nil])
|
2004-02-18 08:44:06 -05:00
|
|
|
assert_equal(10, tuple[1])
|
|
|
|
assert_equal([], @ts.read_all([nil, nil]))
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2004-02-18 08:44:06 -05:00
|
|
|
def test_core_03_notify
|
|
|
|
notify1 = @ts.notify(nil, [:req, Integer])
|
2008-11-18 13:05:28 -05:00
|
|
|
notify2 = @ts.notify(nil, [:ans, Integer], 8)
|
|
|
|
notify3 = @ts.notify(nil, {"message"=>String, "name"=>String}, 8)
|
2004-02-18 08:44:06 -05:00
|
|
|
|
|
|
|
@ts.write({"message"=>"first", "name"=>"3"}, 3)
|
|
|
|
@ts.write({"message"=>"second", "name"=>"1"}, 1)
|
|
|
|
@ts.write({"message"=>"third", "name"=>"0"})
|
|
|
|
@ts.take({"message"=>"third", "name"=>"0"})
|
|
|
|
|
|
|
|
listener1 = Thread.new do
|
|
|
|
lv = 0
|
|
|
|
n = 0
|
|
|
|
notify1.each do |ev, tuple|
|
|
|
|
n += 1
|
|
|
|
if ev == 'write'
|
|
|
|
lv = lv + 1
|
|
|
|
elsif ev == 'take'
|
|
|
|
lv = lv - 1
|
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
|
|
|
assert(lv >= 0)
|
|
|
|
assert_equal([:req, 2], tuple)
|
|
|
|
end
|
|
|
|
[lv, n]
|
|
|
|
end
|
|
|
|
|
|
|
|
listener2 = Thread.new do
|
|
|
|
result = nil
|
|
|
|
lv = 0
|
|
|
|
n = 0
|
2008-11-18 13:05:28 -05:00
|
|
|
notify2.each do |ev, tuple|
|
2004-02-18 08:44:06 -05:00
|
|
|
n += 1
|
2008-11-18 13:05:28 -05:00
|
|
|
if ev == 'write'
|
2004-02-18 08:44:06 -05:00
|
|
|
lv = lv + 1
|
2008-11-18 13:05:28 -05:00
|
|
|
elsif ev == 'take'
|
2004-02-18 08:44:06 -05:00
|
|
|
lv = lv - 1
|
2008-11-18 13:05:28 -05:00
|
|
|
elsif ev == 'close'
|
2004-02-18 08:44:06 -05:00
|
|
|
result = [lv, n]
|
|
|
|
break
|
|
|
|
end
|
|
|
|
assert(lv >= 0)
|
2008-11-18 13:05:28 -05:00
|
|
|
assert_equal([:ans, 10], tuple)
|
2004-02-18 08:44:06 -05:00
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
taker = Thread.new do
|
|
|
|
s = 0
|
|
|
|
while true
|
|
|
|
begin
|
2008-07-26 20:04:38 -04:00
|
|
|
tuple = @ts.take([:req, Integer], 1)
|
2004-02-18 08:44:06 -05:00
|
|
|
s += tuple[1]
|
|
|
|
rescue Rinda::RequestExpiredError
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@ts.write([:ans, s])
|
|
|
|
s
|
|
|
|
end
|
|
|
|
|
2008-07-26 20:04:38 -04:00
|
|
|
5.times do |n|
|
|
|
|
@ts.write([:req, 2])
|
2004-02-18 08:44:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@ts.take({"message"=>"first", "name"=>"3"})
|
|
|
|
|
2005-01-05 10:03:04 -05:00
|
|
|
sleep(4)
|
2008-07-26 20:04:38 -04:00
|
|
|
assert_equal(10, thread_join(taker))
|
2008-11-18 13:05:28 -05:00
|
|
|
# notify2 must not expire until this @ts.take.
|
|
|
|
# sleep(4) might be short enough for the timeout of notify2 (8 secs)
|
2005-01-05 10:03:04 -05:00
|
|
|
tuple = @ts.take([:ans, nil])
|
2004-02-18 08:44:06 -05:00
|
|
|
assert_equal(10, tuple[1])
|
|
|
|
assert_equal([], @ts.read_all([nil, nil]))
|
2008-11-18 13:05:28 -05:00
|
|
|
|
2004-02-18 08:44:06 -05:00
|
|
|
notify1.cancel
|
2008-11-18 13:05:28 -05:00
|
|
|
sleep(7) # notify2 expired (sleep(4)+sleep(7) > 8)
|
|
|
|
|
2008-07-26 20:04:38 -04:00
|
|
|
assert_equal([0, 11], thread_join(listener1))
|
|
|
|
assert_equal([0, 3], thread_join(listener2))
|
2004-02-18 08:44:06 -05:00
|
|
|
|
|
|
|
ary = []
|
|
|
|
ary.push(["write", {"message"=>"first", "name"=>"3"}])
|
|
|
|
ary.push(["write", {"message"=>"second", "name"=>"1"}])
|
|
|
|
ary.push(["write", {"message"=>"third", "name"=>"0"}])
|
|
|
|
ary.push(["take", {"message"=>"third", "name"=>"0"}])
|
|
|
|
ary.push(["take", {"message"=>"first", "name"=>"3"}])
|
|
|
|
ary.push(["delete", {"message"=>"second", "name"=>"1"}])
|
|
|
|
ary.push(["close"])
|
|
|
|
|
|
|
|
notify3.each do |ev|
|
|
|
|
assert_equal(ary.shift, ev)
|
|
|
|
end
|
|
|
|
assert_equal([], ary)
|
|
|
|
end
|
2004-04-06 11:26:25 -04:00
|
|
|
|
2004-02-18 08:44:06 -05:00
|
|
|
def test_cancel_01
|
|
|
|
entry = @ts.write([:removeme, 1])
|
|
|
|
assert_equal([[:removeme, 1]], @ts.read_all([nil, nil]))
|
|
|
|
entry.cancel
|
|
|
|
assert_equal([], @ts.read_all([nil, nil]))
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2004-02-18 08:44:06 -05:00
|
|
|
template = nil
|
|
|
|
taker = Thread.new do
|
2008-07-26 20:04:38 -04:00
|
|
|
@ts.take([:take, nil], 10) do |t|
|
|
|
|
template = t
|
2004-02-18 08:44:06 -05:00
|
|
|
Thread.new do
|
|
|
|
template.cancel
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-07-26 20:04:38 -04:00
|
|
|
sleep(2)
|
2004-02-18 08:44:06 -05:00
|
|
|
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(Rinda::RequestCanceledError) do
|
2008-07-26 20:04:38 -04:00
|
|
|
assert_nil(thread_join(taker))
|
2004-02-18 08:44:06 -05:00
|
|
|
end
|
|
|
|
|
2008-07-26 20:04:38 -04:00
|
|
|
assert(template.canceled?)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-07-26 20:04:38 -04:00
|
|
|
@ts.write([:take, 1])
|
|
|
|
|
2004-02-18 08:44:06 -05:00
|
|
|
assert_equal([[:take, 1]], @ts.read_all([nil, nil]))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cancel_02
|
|
|
|
entry = @ts.write([:removeme, 1])
|
|
|
|
assert_equal([[:removeme, 1]], @ts.read_all([nil, nil]))
|
|
|
|
entry.cancel
|
|
|
|
assert_equal([], @ts.read_all([nil, nil]))
|
|
|
|
|
|
|
|
template = nil
|
|
|
|
reader = Thread.new do
|
2008-07-26 20:04:38 -04:00
|
|
|
@ts.read([:take, nil], 10) do |t|
|
|
|
|
template = t
|
2004-02-18 08:44:06 -05:00
|
|
|
Thread.new do
|
|
|
|
template.cancel
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2005-01-05 10:03:04 -05:00
|
|
|
|
2008-07-26 20:04:38 -04:00
|
|
|
sleep(2)
|
2004-02-18 08:44:06 -05:00
|
|
|
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(Rinda::RequestCanceledError) do
|
2008-07-26 20:04:38 -04:00
|
|
|
assert_nil(thread_join(reader))
|
2004-02-18 08:44:06 -05:00
|
|
|
end
|
|
|
|
|
2008-07-26 20:04:38 -04:00
|
|
|
assert(template.canceled?)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-07-26 20:04:38 -04:00
|
|
|
@ts.write([:take, 1])
|
|
|
|
|
2004-02-18 08:44:06 -05:00
|
|
|
assert_equal([[:take, 1]], @ts.read_all([nil, nil]))
|
|
|
|
end
|
|
|
|
|
|
|
|
class SimpleRenewer
|
|
|
|
def initialize(sec, n = 1)
|
|
|
|
@sec = sec
|
|
|
|
@n = n
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2004-02-18 08:44:06 -05:00
|
|
|
def renew
|
|
|
|
return -1 if @n <= 0
|
|
|
|
@n -= 1
|
|
|
|
return @sec
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_00_renewer
|
|
|
|
tuple = Rinda::TupleEntry.new([1,2,3], true)
|
|
|
|
assert(!tuple.canceled?)
|
|
|
|
assert(tuple.expired?)
|
|
|
|
assert(!tuple.alive?)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2004-02-18 08:44:06 -05:00
|
|
|
tuple = Rinda::TupleEntry.new([1,2,3], 1)
|
|
|
|
assert(!tuple.canceled?)
|
|
|
|
assert(!tuple.expired?)
|
|
|
|
assert(tuple.alive?)
|
|
|
|
sleep(2)
|
|
|
|
assert(tuple.expired?)
|
|
|
|
assert(!tuple.alive?)
|
|
|
|
|
2008-07-26 20:04:38 -04:00
|
|
|
@renewer = SimpleRenewer.new(1,2)
|
|
|
|
tuple = Rinda::TupleEntry.new([1,2,3], @renewer)
|
2004-02-18 08:44:06 -05:00
|
|
|
assert(!tuple.canceled?)
|
|
|
|
assert(!tuple.expired?)
|
|
|
|
assert(tuple.alive?)
|
2008-07-26 20:04:38 -04:00
|
|
|
sleep(1)
|
2004-02-18 08:44:06 -05:00
|
|
|
assert(!tuple.canceled?)
|
|
|
|
assert(!tuple.expired?)
|
|
|
|
assert(tuple.alive?)
|
2008-07-26 20:04:38 -04:00
|
|
|
sleep(2)
|
2004-02-18 08:44:06 -05:00
|
|
|
assert(tuple.expired?)
|
|
|
|
assert(!tuple.alive?)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TupleSpaceTest < Test::Unit::TestCase
|
|
|
|
include TupleSpaceTestModule
|
|
|
|
|
|
|
|
def setup
|
2005-01-05 10:03:04 -05:00
|
|
|
ThreadGroup.new.add(Thread.current)
|
2004-02-18 08:44:06 -05:00
|
|
|
@ts = Rinda::TupleSpace.new(1)
|
|
|
|
end
|
2010-05-26 05:45:49 -04:00
|
|
|
def teardown
|
|
|
|
# implementation-dependent
|
|
|
|
@ts.instance_eval{@keeper.kill if @keeper}
|
|
|
|
end
|
2004-02-18 08:44:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class TupleSpaceProxyTest < Test::Unit::TestCase
|
|
|
|
include TupleSpaceTestModule
|
|
|
|
|
|
|
|
def setup
|
2005-01-05 10:03:04 -05:00
|
|
|
ThreadGroup.new.add(Thread.current)
|
2004-02-18 08:44:06 -05:00
|
|
|
@ts_base = Rinda::TupleSpace.new(1)
|
|
|
|
@ts = Rinda::TupleSpaceProxy.new(@ts_base)
|
|
|
|
end
|
2010-05-26 05:45:49 -04:00
|
|
|
def teardown
|
|
|
|
# implementation-dependent
|
|
|
|
@ts_base.instance_eval{@keeper.kill if @keeper}
|
|
|
|
end
|
2004-02-18 08:44:06 -05:00
|
|
|
|
2005-10-16 01:43:19 -04:00
|
|
|
def test_remote_array_and_hash
|
2008-07-26 20:04:38 -04:00
|
|
|
@ts.write(DRbObject.new([1, 2, 3]))
|
2005-10-16 01:43:19 -04:00
|
|
|
assert_equal([1, 2, 3], @ts.take([1, 2, 3], 0))
|
2008-07-26 20:04:38 -04:00
|
|
|
@ts.write(DRbObject.new({'head' => 1, 'tail' => 2}))
|
2005-10-16 01:43:19 -04:00
|
|
|
assert_equal({'head' => 1, 'tail' => 2},
|
|
|
|
@ts.take({'head' => 1, 'tail' => 2}, 0))
|
|
|
|
end
|
|
|
|
|
2004-02-19 08:40:42 -05:00
|
|
|
@server = DRb.primary_server || DRb.start_service
|
2004-02-18 08:44:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|