2003-11-11 23:01:21 -05:00
|
|
|
require "monitor"
|
|
|
|
require "thread"
|
|
|
|
|
|
|
|
require "test/unit"
|
|
|
|
|
|
|
|
class TestMonitor < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@monitor = Monitor.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_enter
|
|
|
|
ary = []
|
2003-11-12 00:10:54 -05:00
|
|
|
queue = Queue.new
|
2003-11-11 23:01:21 -05:00
|
|
|
th = Thread.start {
|
2003-11-12 00:10:54 -05:00
|
|
|
queue.pop
|
2003-11-11 23:01:21 -05:00
|
|
|
@monitor.enter
|
|
|
|
for i in 6 .. 10
|
|
|
|
ary.push(i)
|
|
|
|
Thread.pass
|
|
|
|
end
|
|
|
|
@monitor.exit
|
|
|
|
}
|
|
|
|
@monitor.enter
|
2003-11-12 00:10:54 -05:00
|
|
|
queue.enq(nil)
|
2003-11-11 23:01:21 -05:00
|
|
|
for i in 1 .. 5
|
|
|
|
ary.push(i)
|
|
|
|
Thread.pass
|
|
|
|
end
|
|
|
|
@monitor.exit
|
|
|
|
th.join
|
|
|
|
assert_equal((1..10).to_a, ary)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_synchronize
|
|
|
|
ary = []
|
2003-11-12 00:10:54 -05:00
|
|
|
queue = Queue.new
|
2003-11-11 23:01:21 -05:00
|
|
|
th = Thread.start {
|
2003-11-12 00:10:54 -05:00
|
|
|
queue.pop
|
2003-11-11 23:01:21 -05:00
|
|
|
@monitor.synchronize do
|
|
|
|
for i in 6 .. 10
|
|
|
|
ary.push(i)
|
|
|
|
Thread.pass
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
@monitor.synchronize do
|
2003-11-12 00:10:54 -05:00
|
|
|
queue.enq(nil)
|
2003-11-11 23:01:21 -05:00
|
|
|
for i in 1 .. 5
|
|
|
|
ary.push(i)
|
|
|
|
Thread.pass
|
|
|
|
end
|
|
|
|
end
|
|
|
|
th.join
|
|
|
|
assert_equal((1..10).to_a, ary)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_try_enter
|
2003-11-12 00:10:54 -05:00
|
|
|
queue1 = Queue.new
|
|
|
|
queue2 = Queue.new
|
2003-11-11 23:01:21 -05:00
|
|
|
th = Thread.start {
|
2003-11-12 00:10:54 -05:00
|
|
|
queue1.deq
|
2003-11-11 23:01:21 -05:00
|
|
|
@monitor.enter
|
2003-11-12 00:10:54 -05:00
|
|
|
queue2.enq(nil)
|
|
|
|
queue1.deq
|
2003-11-11 23:01:21 -05:00
|
|
|
@monitor.exit
|
2003-11-12 00:10:54 -05:00
|
|
|
queue2.enq(nil)
|
2003-11-11 23:01:21 -05:00
|
|
|
}
|
|
|
|
assert_equal(true, @monitor.try_enter)
|
|
|
|
@monitor.exit
|
2003-11-12 00:10:54 -05:00
|
|
|
queue1.enq(nil)
|
|
|
|
queue2.deq
|
2003-11-11 23:01:21 -05:00
|
|
|
assert_equal(false, @monitor.try_enter)
|
2003-11-12 00:10:54 -05:00
|
|
|
queue1.enq(nil)
|
|
|
|
queue2.deq
|
2003-11-11 23:01:21 -05:00
|
|
|
assert_equal(true, @monitor.try_enter)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cond
|
|
|
|
cond = @monitor.new_cond
|
|
|
|
|
|
|
|
a = "foo"
|
2003-11-12 00:10:54 -05:00
|
|
|
queue1 = Queue.new
|
2003-11-11 23:01:21 -05:00
|
|
|
Thread.start do
|
2003-11-12 00:10:54 -05:00
|
|
|
queue1.deq
|
2003-11-11 23:01:21 -05:00
|
|
|
@monitor.synchronize do
|
|
|
|
a = "bar"
|
|
|
|
cond.signal
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@monitor.synchronize do
|
2003-11-12 00:10:54 -05:00
|
|
|
queue1.enq(nil)
|
2003-11-11 23:01:21 -05:00
|
|
|
assert_equal("foo", a)
|
|
|
|
result1 = cond.wait
|
|
|
|
assert_equal(true, result1)
|
|
|
|
assert_equal("bar", a)
|
|
|
|
end
|
2007-02-24 02:01:18 -05:00
|
|
|
end
|
2003-11-11 23:01:21 -05:00
|
|
|
|
2007-02-24 02:01:18 -05:00
|
|
|
def _test_timedwait
|
2003-11-11 23:01:21 -05:00
|
|
|
b = "foo"
|
2003-11-12 00:10:54 -05:00
|
|
|
queue2 = Queue.new
|
2003-11-11 23:01:21 -05:00
|
|
|
Thread.start do
|
2003-11-12 00:10:54 -05:00
|
|
|
queue2.deq
|
2003-11-11 23:01:21 -05:00
|
|
|
@monitor.synchronize do
|
|
|
|
b = "bar"
|
|
|
|
cond.signal
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@monitor.synchronize do
|
2003-11-12 00:10:54 -05:00
|
|
|
queue2.enq(nil)
|
2003-11-11 23:01:21 -05:00
|
|
|
assert_equal("foo", b)
|
|
|
|
result2 = cond.wait(0.1)
|
|
|
|
assert_equal(true, result2)
|
|
|
|
assert_equal("bar", b)
|
|
|
|
end
|
|
|
|
|
|
|
|
c = "foo"
|
2003-12-10 01:02:27 -05:00
|
|
|
queue3 = Queue.new
|
2003-11-11 23:01:21 -05:00
|
|
|
Thread.start do
|
2003-12-10 01:02:27 -05:00
|
|
|
queue3.deq
|
2003-11-11 23:01:21 -05:00
|
|
|
@monitor.synchronize do
|
|
|
|
c = "bar"
|
|
|
|
cond.signal
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@monitor.synchronize do
|
|
|
|
assert_equal("foo", c)
|
|
|
|
result3 = cond.wait(0.1)
|
|
|
|
assert_equal(false, result3)
|
|
|
|
assert_equal("foo", c)
|
2003-12-10 01:02:27 -05:00
|
|
|
queue3.enq(nil)
|
2003-11-11 23:01:21 -05:00
|
|
|
result4 = cond.wait
|
|
|
|
assert_equal(true, result4)
|
|
|
|
assert_equal("bar", c)
|
|
|
|
end
|
|
|
|
|
2003-11-12 00:10:54 -05:00
|
|
|
# d = "foo"
|
|
|
|
# cumber_thread = Thread.start {
|
|
|
|
# loop do
|
|
|
|
# @monitor.synchronize do
|
|
|
|
# d = "foo"
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# }
|
|
|
|
# queue3 = Queue.new
|
|
|
|
# Thread.start do
|
|
|
|
# queue3.pop
|
|
|
|
# @monitor.synchronize do
|
|
|
|
# d = "bar"
|
|
|
|
# cond.signal
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# @monitor.synchronize do
|
|
|
|
# queue3.enq(nil)
|
|
|
|
# assert_equal("foo", d)
|
|
|
|
# result5 = cond.wait
|
|
|
|
# assert_equal(true, result5)
|
|
|
|
# # this thread has priority over cumber_thread
|
|
|
|
# assert_equal("bar", d)
|
|
|
|
# end
|
|
|
|
# cumber_thread.kill
|
2003-11-11 23:01:21 -05:00
|
|
|
end
|
|
|
|
end
|