2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2003-11-11 23:01:21 -05:00
|
|
|
require "monitor"
|
|
|
|
|
|
|
|
require "test/unit"
|
|
|
|
|
|
|
|
class TestMonitor < Test::Unit::TestCase
|
2016-08-30 02:22:30 -04:00
|
|
|
Queue = Thread::Queue
|
|
|
|
|
2003-11-11 23:01:21 -05:00
|
|
|
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
|
|
|
|
}
|
2014-11-13 08:04:43 -05:00
|
|
|
th2 = Thread.start {
|
|
|
|
@monitor.enter
|
|
|
|
queue.enq(nil)
|
|
|
|
for i in 1 .. 5
|
|
|
|
ary.push(i)
|
|
|
|
Thread.pass
|
|
|
|
end
|
|
|
|
@monitor.exit
|
|
|
|
}
|
|
|
|
assert_join_threads([th, th2])
|
2003-11-11 23:01:21 -05:00
|
|
|
assert_equal((1..10).to_a, ary)
|
|
|
|
end
|
|
|
|
|
2015-05-18 00:56:22 -04:00
|
|
|
def test_enter_second_after_killed_thread
|
|
|
|
th = Thread.start {
|
|
|
|
@monitor.enter
|
|
|
|
Thread.current.kill
|
|
|
|
@monitor.exit
|
|
|
|
}
|
|
|
|
th.join
|
|
|
|
@monitor.enter
|
|
|
|
@monitor.exit
|
|
|
|
th2 = Thread.start {
|
|
|
|
@monitor.enter
|
|
|
|
@monitor.exit
|
|
|
|
}
|
|
|
|
assert_join_threads([th, th2])
|
|
|
|
end
|
|
|
|
|
2003-11-11 23:01:21 -05:00
|
|
|
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
|
|
|
|
}
|
2014-11-13 08:04:43 -05:00
|
|
|
th2 = Thread.start {
|
|
|
|
@monitor.synchronize do
|
|
|
|
queue.enq(nil)
|
|
|
|
for i in 1 .. 5
|
|
|
|
ary.push(i)
|
|
|
|
Thread.pass
|
|
|
|
end
|
2003-11-11 23:01:21 -05:00
|
|
|
end
|
2014-11-13 08:04:43 -05:00
|
|
|
}
|
|
|
|
assert_join_threads([th, th2])
|
2003-11-11 23:01:21 -05:00
|
|
|
assert_equal((1..10).to_a, ary)
|
|
|
|
end
|
|
|
|
|
2009-10-20 11:57:39 -04:00
|
|
|
def test_killed_thread_in_synchronize
|
|
|
|
ary = []
|
|
|
|
queue = Queue.new
|
|
|
|
t1 = Thread.start {
|
|
|
|
queue.pop
|
|
|
|
@monitor.synchronize {
|
|
|
|
ary << :t1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t2 = Thread.start {
|
|
|
|
queue.pop
|
|
|
|
@monitor.synchronize {
|
|
|
|
ary << :t2
|
|
|
|
}
|
|
|
|
}
|
2014-11-13 08:04:43 -05:00
|
|
|
t3 = Thread.start {
|
|
|
|
@monitor.synchronize do
|
|
|
|
queue.enq(nil)
|
|
|
|
queue.enq(nil)
|
|
|
|
assert_equal([], ary)
|
|
|
|
t1.kill
|
|
|
|
t2.kill
|
|
|
|
ary << :main
|
|
|
|
end
|
|
|
|
assert_equal([:main], ary)
|
|
|
|
}
|
|
|
|
assert_join_threads([t1, t2, t3])
|
2009-10-20 11:57:39 -04:00
|
|
|
end
|
|
|
|
|
2003-11-11 23:01:21 -05:00
|
|
|
def test_try_enter
|
2003-11-12 00:10:54 -05:00
|
|
|
queue1 = Queue.new
|
|
|
|
queue2 = Queue.new
|
2014-05-31 11:31:46 -04: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
|
|
|
}
|
2014-11-13 08:04:43 -05:00
|
|
|
th2 = Thread.start {
|
|
|
|
assert_equal(true, @monitor.try_enter)
|
|
|
|
@monitor.exit
|
|
|
|
queue1.enq(nil)
|
|
|
|
queue2.deq
|
|
|
|
assert_equal(false, @monitor.try_enter)
|
|
|
|
queue1.enq(nil)
|
|
|
|
queue2.deq
|
|
|
|
assert_equal(true, @monitor.try_enter)
|
|
|
|
}
|
|
|
|
assert_join_threads([th, th2])
|
2003-11-11 23:01:21 -05:00
|
|
|
end
|
|
|
|
|
2015-05-18 00:56:22 -04:00
|
|
|
def test_try_enter_second_after_killed_thread
|
|
|
|
th = Thread.start {
|
|
|
|
assert_equal(true, @monitor.try_enter)
|
|
|
|
Thread.current.kill
|
|
|
|
@monitor.exit
|
|
|
|
}
|
|
|
|
th.join
|
|
|
|
assert_equal(true, @monitor.try_enter)
|
|
|
|
@monitor.exit
|
|
|
|
th2 = Thread.start {
|
|
|
|
assert_equal(true, @monitor.try_enter)
|
|
|
|
@monitor.exit
|
|
|
|
}
|
|
|
|
assert_join_threads([th, th2])
|
|
|
|
end
|
|
|
|
|
2017-09-19 21:40:53 -04:00
|
|
|
def test_mon_locked_and_owned
|
|
|
|
queue1 = Queue.new
|
|
|
|
queue2 = Queue.new
|
|
|
|
th = Thread.start {
|
|
|
|
@monitor.enter
|
|
|
|
queue1.enq(nil)
|
|
|
|
queue2.deq
|
|
|
|
@monitor.exit
|
|
|
|
queue1.enq(nil)
|
|
|
|
}
|
|
|
|
queue1.deq
|
|
|
|
assert(@monitor.mon_locked?)
|
|
|
|
assert(!@monitor.mon_owned?)
|
|
|
|
|
|
|
|
queue2.enq(nil)
|
|
|
|
queue1.deq
|
|
|
|
assert(!@monitor.mon_locked?)
|
|
|
|
|
|
|
|
@monitor.enter
|
|
|
|
assert @monitor.mon_locked?
|
|
|
|
assert @monitor.mon_owned?
|
|
|
|
@monitor.exit
|
|
|
|
|
|
|
|
@monitor.synchronize do
|
|
|
|
assert @monitor.mon_locked?
|
|
|
|
assert @monitor.mon_owned?
|
|
|
|
end
|
2019-06-28 01:51:23 -04:00
|
|
|
ensure
|
|
|
|
th.join
|
2017-09-19 21:40:53 -04:00
|
|
|
end
|
|
|
|
|
2003-11-11 23:01:21 -05:00
|
|
|
def test_cond
|
|
|
|
cond = @monitor.new_cond
|
|
|
|
|
|
|
|
a = "foo"
|
2003-11-12 00:10:54 -05:00
|
|
|
queue1 = Queue.new
|
2014-05-31 11:31:46 -04:00
|
|
|
th = 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
|
2014-11-13 08:04:43 -05:00
|
|
|
th2 = Thread.start do
|
|
|
|
@monitor.synchronize do
|
|
|
|
queue1.enq(nil)
|
|
|
|
assert_equal("foo", a)
|
|
|
|
result1 = cond.wait
|
|
|
|
assert_equal(true, result1)
|
|
|
|
assert_equal("bar", a)
|
|
|
|
end
|
2003-11-11 23:01:21 -05:00
|
|
|
end
|
2014-11-13 08:04:43 -05:00
|
|
|
assert_join_threads([th, th2])
|
2007-02-24 02:01:18 -05:00
|
|
|
end
|
2003-11-11 23:01:21 -05:00
|
|
|
|
2010-02-06 07:31:52 -05:00
|
|
|
def test_timedwait
|
2009-10-20 11:57:39 -04:00
|
|
|
cond = @monitor.new_cond
|
2003-11-11 23:01:21 -05:00
|
|
|
b = "foo"
|
2003-11-12 00:10:54 -05:00
|
|
|
queue2 = Queue.new
|
2014-05-31 11:31:46 -04:00
|
|
|
th = 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
|
2014-11-13 08:04:43 -05:00
|
|
|
th2 = Thread.start do
|
|
|
|
@monitor.synchronize do
|
|
|
|
queue2.enq(nil)
|
|
|
|
assert_equal("foo", b)
|
|
|
|
result2 = cond.wait(0.1)
|
|
|
|
assert_equal(true, result2)
|
|
|
|
assert_equal("bar", b)
|
|
|
|
end
|
2003-11-11 23:01:21 -05:00
|
|
|
end
|
2014-11-13 08:04:43 -05:00
|
|
|
assert_join_threads([th, th2])
|
2003-11-11 23:01:21 -05:00
|
|
|
|
|
|
|
c = "foo"
|
2003-12-10 01:02:27 -05:00
|
|
|
queue3 = Queue.new
|
2014-05-31 11:31:46 -04:00
|
|
|
th = 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
|
2014-11-13 08:04:43 -05:00
|
|
|
th2 = Thread.start do
|
|
|
|
@monitor.synchronize do
|
|
|
|
assert_equal("foo", c)
|
|
|
|
result3 = cond.wait(0.1)
|
|
|
|
assert_equal(true, result3) # wait always returns true in Ruby 1.9
|
|
|
|
assert_equal("foo", c)
|
|
|
|
queue3.enq(nil)
|
|
|
|
result4 = cond.wait
|
|
|
|
assert_equal(true, result4)
|
|
|
|
assert_equal("bar", c)
|
|
|
|
end
|
2003-11-11 23:01:21 -05:00
|
|
|
end
|
2014-11-13 08:04:43 -05:00
|
|
|
assert_join_threads([th, th2])
|
2003-11-11 23:01:21 -05:00
|
|
|
|
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
|
2018-11-27 20:51:44 -05:00
|
|
|
|
|
|
|
def test_wait_interruption
|
|
|
|
queue = Queue.new
|
|
|
|
cond = @monitor.new_cond
|
|
|
|
@monitor.define_singleton_method(:mon_enter_for_cond) do |*args|
|
|
|
|
queue.deq
|
|
|
|
super(*args)
|
|
|
|
end
|
|
|
|
th = Thread.start {
|
|
|
|
@monitor.synchronize do
|
|
|
|
begin
|
|
|
|
cond.wait(0.1)
|
|
|
|
rescue Interrupt
|
|
|
|
@monitor.instance_variable_get(:@mon_owner)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
sleep(0.1)
|
|
|
|
th.raise(Interrupt)
|
|
|
|
queue.enq(nil)
|
|
|
|
assert_equal th, th.value
|
|
|
|
end
|
2003-11-11 23:01:21 -05:00
|
|
|
end
|