summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/rinda/test_rinda.rb89
1 files changed, 42 insertions, 47 deletions
diff --git a/test/rinda/test_rinda.rb b/test/rinda/test_rinda.rb
index 553d307..6da6c3b 100644
--- a/test/rinda/test_rinda.rb
+++ b/test/rinda/test_rinda.rb
@@ -12,14 +12,14 @@ class MockClock
include Singleton
class MyTS < Rinda::TupleSpace
- def keeper
+ def keeper_thread
nil
end
end
def initialize
@now = 2
- @reso = 0.1
+ @reso = 1
@ts = MyTS.new
@ts.write([2, :now])
@inf = 2**31 - 1
@@ -40,10 +40,11 @@ class MockClock
@ts.write([@now, :now])
end
- def forward(n=nil)
+ def forward(n)
while n > 0
_forward(@reso)
n -= @reso
+ Thread.pass
end
end
@@ -56,20 +57,10 @@ class MockClock
end
def sleep(n=nil)
- while will_deadlock?
- n -= @reso
- forward
- return 0 if n <= 0
- end
now ,= @ts.read([nil, :now])
@ts.read([(now + n)..@inf, :now])
0
end
-
- def will_deadlock?
- sz = Thread.current.group.list.find_all {|x| x.status != 'sleep'}.size
- sz <= 1
- end
end
module Time
@@ -115,6 +106,14 @@ module TupleSpaceTestModule
Time.sleep(n)
end
end
+
+ def thread_join(th)
+ while th.alive?
+ Kernel.sleep(0.1)
+ sleep(1)
+ end
+ th.value
+ end
def test_00_tuple
tuple = Rinda::TupleEntry.new([1,2,3])
@@ -242,15 +241,15 @@ module TupleSpaceTestModule
def test_ruby_talk_264062
th = Thread.new { @ts.take([:empty], 1) }
- sleep 2
+ sleep(10)
assert_raises(Rinda::RequestExpiredError) do
- th.value
+ thread_join(th)
end
th = Thread.new { @ts.read([:empty], 1) }
- sleep 2
+ sleep(10)
assert_raises(Rinda::RequestExpiredError) do
- th.value
+ thread_join(th)
end
end
@@ -266,7 +265,7 @@ module TupleSpaceTestModule
s = 0
while true
begin
- tuple = @ts.take([:req, Integer], 0.5)
+ tuple = @ts.take([:req, Integer], 1)
assert_equal(2, tuple[1])
s += tuple[1]
rescue Rinda::RequestExpiredError
@@ -277,10 +276,9 @@ module TupleSpaceTestModule
s
end
- sleep(20)
+ assert_equal(10, thread_join(taker))
tuple = @ts.take([:ans, nil])
assert_equal(10, tuple[1])
- assert_equal(10, taker.value)
end
def test_core_02
@@ -288,7 +286,7 @@ module TupleSpaceTestModule
s = 0
while true
begin
- tuple = @ts.take([:req, Integer], 1.0)
+ tuple = @ts.take([:req, Integer], 1)
assert_equal(2, tuple[1])
s += tuple[1]
rescue Rinda::RequestExpiredError
@@ -303,10 +301,9 @@ module TupleSpaceTestModule
@ts.write([:req, 2])
end
- sleep(20)
+ assert_equal(10, thread_join(taker))
tuple = @ts.take([:ans, nil])
assert_equal(10, tuple[1])
- assert_equal(10, taker.value)
assert_equal([], @ts.read_all([nil, nil]))
end
@@ -363,7 +360,7 @@ module TupleSpaceTestModule
s = 0
while true
begin
- tuple = @ts.take([:req, Integer], 1.0)
+ tuple = @ts.take([:req, Integer], 1)
s += tuple[1]
rescue Rinda::RequestExpiredError
break
@@ -373,26 +370,23 @@ module TupleSpaceTestModule
s
end
- writer = Thread.new do
- 5.times do |n|
- @ts.write([:req, 2])
- sleep 0.1
- end
+ 5.times do |n|
+ @ts.write([:req, 2])
end
@ts.take({"message"=>"first", "name"=>"3"})
sleep(4)
+ assert_equal(10, thread_join(taker))
tuple = @ts.take([:ans, nil])
assert_equal(10, tuple[1])
- assert_equal(10, taker.value)
assert_equal([], @ts.read_all([nil, nil]))
notify1.cancel
sleep(3) # notify2 expired
- assert_equal([0, 11], listener1.value)
- assert_equal([0, 3], listener2.value)
+ assert_equal([0, 11], thread_join(listener1))
+ assert_equal([0, 3], thread_join(listener2))
ary = []
ary.push(["write", {"message"=>"first", "name"=>"3"}])
@@ -419,21 +413,21 @@ module TupleSpaceTestModule
taker = Thread.new do
@ts.take([:take, nil], 10) do |template|
Thread.new do
- sleep 0.2
template.cancel
end
end
end
- sleep(1)
- assert(template.canceled?)
-
- @ts.write([:take, 1])
+ sleep(2)
assert_raises(Rinda::RequestCanceledError) do
- assert_nil(taker.value)
+ assert_nil(thread_join(taker))
end
+ assert(template.canceled?)
+
+ @ts.write([:take, 1])
+
assert_equal([[:take, 1]], @ts.read_all([nil, nil]))
end
@@ -447,21 +441,21 @@ module TupleSpaceTestModule
reader = Thread.new do
@ts.read([:take, nil], 10) do |template|
Thread.new do
- sleep 0.2
template.cancel
end
end
end
- sleep(1)
- assert(template.canceled?)
-
- @ts.write([:take, 1])
+ sleep(2)
assert_raises(Rinda::RequestCanceledError) do
- assert_nil(reader.value)
+ assert_nil(thread_join(reader))
end
+ assert(template.canceled?)
+
+ @ts.write([:take, 1])
+
assert_equal([[:take, 1]], @ts.read_all([nil, nil]))
end
@@ -492,15 +486,16 @@ module TupleSpaceTestModule
assert(tuple.expired?)
assert(!tuple.alive?)
- tuple = Rinda::TupleEntry.new([1,2,3], SimpleRenewer.new(1,2))
+ @renewer = SimpleRenewer.new(1,2)
+ tuple = Rinda::TupleEntry.new([1,2,3], @renewer)
assert(!tuple.canceled?)
assert(!tuple.expired?)
assert(tuple.alive?)
- sleep(1.5)
+ sleep(1)
assert(!tuple.canceled?)
assert(!tuple.expired?)
assert(tuple.alive?)
- sleep(1.5)
+ sleep(2)
assert(tuple.expired?)
assert(!tuple.alive?)
end