1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* test/monitor/test_monitor.rb: fix the timing problem by Queue.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4944 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
shugo 2003-11-12 05:10:54 +00:00
parent 64446f023c
commit ecec550855
2 changed files with 51 additions and 32 deletions

View file

@ -1,3 +1,7 @@
Wed Nov 12 14:09:43 2003 Shugo Maeda <shugo@ruby-lang.org>
* test/monitor/test_monitor.rb: fix the timing problem by Queue.
Wed Nov 12 12:59:44 2003 Shugo Maeda <shugo@ruby-lang.org> Wed Nov 12 12:59:44 2003 Shugo Maeda <shugo@ruby-lang.org>
* test/monitor/test_monitor.rb: added. * test/monitor/test_monitor.rb: added.

View file

@ -10,8 +10,9 @@ class TestMonitor < Test::Unit::TestCase
def test_enter def test_enter
ary = [] ary = []
queue = Queue.new
th = Thread.start { th = Thread.start {
Thread.pass queue.pop
@monitor.enter @monitor.enter
for i in 6 .. 10 for i in 6 .. 10
ary.push(i) ary.push(i)
@ -20,6 +21,7 @@ class TestMonitor < Test::Unit::TestCase
@monitor.exit @monitor.exit
} }
@monitor.enter @monitor.enter
queue.enq(nil)
for i in 1 .. 5 for i in 1 .. 5
ary.push(i) ary.push(i)
Thread.pass Thread.pass
@ -31,8 +33,9 @@ class TestMonitor < Test::Unit::TestCase
def test_synchronize def test_synchronize
ary = [] ary = []
queue = Queue.new
th = Thread.start { th = Thread.start {
Thread.pass queue.pop
@monitor.synchronize do @monitor.synchronize do
for i in 6 .. 10 for i in 6 .. 10
ary.push(i) ary.push(i)
@ -41,6 +44,7 @@ class TestMonitor < Test::Unit::TestCase
end end
} }
@monitor.synchronize do @monitor.synchronize do
queue.enq(nil)
for i in 1 .. 5 for i in 1 .. 5
ary.push(i) ary.push(i)
Thread.pass Thread.pass
@ -51,18 +55,23 @@ class TestMonitor < Test::Unit::TestCase
end end
def test_try_enter def test_try_enter
queue = Queue.new queue1 = Queue.new
queue2 = Queue.new
th = Thread.start { th = Thread.start {
queue.deq queue1.deq
@monitor.enter @monitor.enter
queue.deq queue2.enq(nil)
queue1.deq
@monitor.exit @monitor.exit
queue2.enq(nil)
} }
assert_equal(true, @monitor.try_enter) assert_equal(true, @monitor.try_enter)
@monitor.exit @monitor.exit
queue.enq(Object.new) queue1.enq(nil)
queue2.deq
assert_equal(false, @monitor.try_enter) assert_equal(false, @monitor.try_enter)
queue.enq(Object.new) queue1.enq(nil)
queue2.deq
assert_equal(true, @monitor.try_enter) assert_equal(true, @monitor.try_enter)
end end
@ -70,14 +79,16 @@ class TestMonitor < Test::Unit::TestCase
cond = @monitor.new_cond cond = @monitor.new_cond
a = "foo" a = "foo"
queue1 = Queue.new
Thread.start do Thread.start do
Thread.pass queue1.deq
@monitor.synchronize do @monitor.synchronize do
a = "bar" a = "bar"
cond.signal cond.signal
end end
end end
@monitor.synchronize do @monitor.synchronize do
queue1.enq(nil)
assert_equal("foo", a) assert_equal("foo", a)
result1 = cond.wait result1 = cond.wait
assert_equal(true, result1) assert_equal(true, result1)
@ -85,14 +96,16 @@ class TestMonitor < Test::Unit::TestCase
end end
b = "foo" b = "foo"
queue2 = Queue.new
Thread.start do Thread.start do
Thread.pass queue2.deq
@monitor.synchronize do @monitor.synchronize do
b = "bar" b = "bar"
cond.signal cond.signal
end end
end end
@monitor.synchronize do @monitor.synchronize do
queue2.enq(nil)
assert_equal("foo", b) assert_equal("foo", b)
result2 = cond.wait(0.1) result2 = cond.wait(0.1)
assert_equal(true, result2) assert_equal(true, result2)
@ -117,28 +130,30 @@ class TestMonitor < Test::Unit::TestCase
assert_equal("bar", c) assert_equal("bar", c)
end end
d = "foo" # d = "foo"
cumber_thread = Thread.start { # cumber_thread = Thread.start {
loop do # loop do
@monitor.synchronize do # @monitor.synchronize do
d = "foo" # d = "foo"
end # end
end # end
} # }
Thread.start do # queue3 = Queue.new
Thread.pass # Thread.start do
@monitor.synchronize do # queue3.pop
d = "bar" # @monitor.synchronize do
cond.signal # d = "bar"
end # cond.signal
end # end
@monitor.synchronize do # end
assert_equal("foo", d) # @monitor.synchronize do
result5 = cond.wait # queue3.enq(nil)
assert_equal(true, result5) # assert_equal("foo", d)
# this thread has priority over cumber_thread # result5 = cond.wait
assert_equal("bar", d) # assert_equal(true, result5)
end # # this thread has priority over cumber_thread
cumber_thread.kill # assert_equal("bar", d)
# end
# cumber_thread.kill
end end
end end