2015-12-16 08:07:31 +03:00
|
|
|
# frozen_string_literal: false
|
2003-11-12 07:01:21 +03:00
|
|
|
require "monitor"
|
|
|
|
|
|
|
|
require "test/unit"
|
|
|
|
|
|
|
|
class TestMonitor < Test::Unit::TestCase
|
2016-08-30 09:22:30 +03:00
|
|
|
Queue = Thread::Queue
|
|
|
|
|
2003-11-12 07:01:21 +03:00
|
|
|
def setup
|
|
|
|
@monitor = Monitor.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_enter
|
|
|
|
ary = []
|
2003-11-12 08:10:54 +03:00
|
|
|
queue = Queue.new
|
2003-11-12 07:01:21 +03:00
|
|
|
th = Thread.start {
|
2003-11-12 08:10:54 +03:00
|
|
|
queue.pop
|
2003-11-12 07:01:21 +03:00
|
|
|
@monitor.enter
|
|
|
|
for i in 6 .. 10
|
|
|
|
ary.push(i)
|
|
|
|
Thread.pass
|
|
|
|
end
|
|
|
|
@monitor.exit
|
|
|
|
}
|
2014-11-13 16:04:43 +03: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-12 07:01:21 +03:00
|
|
|
assert_equal((1..10).to_a, ary)
|
|
|
|
end
|
|
|
|
|
2015-05-18 07:56:22 +03: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-12 07:01:21 +03:00
|
|
|
def test_synchronize
|
|
|
|
ary = []
|
2003-11-12 08:10:54 +03:00
|
|
|
queue = Queue.new
|
2003-11-12 07:01:21 +03:00
|
|
|
th = Thread.start {
|
2003-11-12 08:10:54 +03:00
|
|
|
queue.pop
|
2003-11-12 07:01:21 +03:00
|
|
|
@monitor.synchronize do
|
|
|
|
for i in 6 .. 10
|
|
|
|
ary.push(i)
|
|
|
|
Thread.pass
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
2014-11-13 16:04:43 +03:00
|
|
|
th2 = Thread.start {
|
|
|
|
@monitor.synchronize do
|
|
|
|
queue.enq(nil)
|
|
|
|
for i in 1 .. 5
|
|
|
|
ary.push(i)
|
|
|
|
Thread.pass
|
|
|
|
end
|
2003-11-12 07:01:21 +03:00
|
|
|
end
|
2014-11-13 16:04:43 +03:00
|
|
|
}
|
|
|
|
assert_join_threads([th, th2])
|
2003-11-12 07:01:21 +03:00
|
|
|
assert_equal((1..10).to_a, ary)
|
|
|
|
end
|
|
|
|
|
2009-10-20 19: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 16:04:43 +03: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 19:57:39 +04:00
|
|
|
end
|
|
|
|
|
2003-11-12 07:01:21 +03:00
|
|
|
def test_try_enter
|
2003-11-12 08:10:54 +03:00
|
|
|
queue1 = Queue.new
|
|
|
|
queue2 = Queue.new
|
2014-05-31 19:31:46 +04:00
|
|
|
th = Thread.start {
|
2003-11-12 08:10:54 +03:00
|
|
|
queue1.deq
|
2003-11-12 07:01:21 +03:00
|
|
|
@monitor.enter
|
2003-11-12 08:10:54 +03:00
|
|
|
queue2.enq(nil)
|
|
|
|
queue1.deq
|
2003-11-12 07:01:21 +03:00
|
|
|
@monitor.exit
|
2003-11-12 08:10:54 +03:00
|
|
|
queue2.enq(nil)
|
2003-11-12 07:01:21 +03:00
|
|
|
}
|
2014-11-13 16:04:43 +03: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-12 07:01:21 +03:00
|
|
|
end
|
|
|
|
|
2015-05-18 07:56:22 +03: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-20 04:40:53 +03: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 08:51:23 +03:00
|
|
|
ensure
|
|
|
|
th.join
|
2017-09-20 04:40:53 +03:00
|
|
|
end
|
|
|
|
|
2003-11-12 07:01:21 +03:00
|
|
|
def test_cond
|
|
|
|
cond = @monitor.new_cond
|
|
|
|
|
|
|
|
a = "foo"
|
2003-11-12 08:10:54 +03:00
|
|
|
queue1 = Queue.new
|
2014-05-31 19:31:46 +04:00
|
|
|
th = Thread.start do
|
2003-11-12 08:10:54 +03:00
|
|
|
queue1.deq
|
2003-11-12 07:01:21 +03:00
|
|
|
@monitor.synchronize do
|
|
|
|
a = "bar"
|
|
|
|
cond.signal
|
|
|
|
end
|
|
|
|
end
|
2014-11-13 16:04:43 +03: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-12 07:01:21 +03:00
|
|
|
end
|
2014-11-13 16:04:43 +03:00
|
|
|
assert_join_threads([th, th2])
|
2007-02-24 10:01:18 +03:00
|
|
|
end
|
2003-11-12 07:01:21 +03:00
|
|
|
|
2010-02-06 15:31:52 +03:00
|
|
|
def test_timedwait
|
2009-10-20 19:57:39 +04:00
|
|
|
cond = @monitor.new_cond
|
2003-11-12 07:01:21 +03:00
|
|
|
b = "foo"
|
2003-11-12 08:10:54 +03:00
|
|
|
queue2 = Queue.new
|
2014-05-31 19:31:46 +04:00
|
|
|
th = Thread.start do
|
2003-11-12 08:10:54 +03:00
|
|
|
queue2.deq
|
2003-11-12 07:01:21 +03:00
|
|
|
@monitor.synchronize do
|
|
|
|
b = "bar"
|
|
|
|
cond.signal
|
|
|
|
end
|
|
|
|
end
|
2014-11-13 16:04:43 +03: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-12 07:01:21 +03:00
|
|
|
end
|
2014-11-13 16:04:43 +03:00
|
|
|
assert_join_threads([th, th2])
|
2003-11-12 07:01:21 +03:00
|
|
|
|
|
|
|
c = "foo"
|
2003-12-10 09:02:27 +03:00
|
|
|
queue3 = Queue.new
|
2014-05-31 19:31:46 +04:00
|
|
|
th = Thread.start do
|
2003-12-10 09:02:27 +03:00
|
|
|
queue3.deq
|
2003-11-12 07:01:21 +03:00
|
|
|
@monitor.synchronize do
|
|
|
|
c = "bar"
|
|
|
|
cond.signal
|
|
|
|
end
|
|
|
|
end
|
2014-11-13 16:04:43 +03: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-12 07:01:21 +03:00
|
|
|
end
|
2014-11-13 16:04:43 +03:00
|
|
|
assert_join_threads([th, th2])
|
2003-11-12 07:01:21 +03:00
|
|
|
|
2003-11-12 08:10:54 +03: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-12 07:01:21 +03:00
|
|
|
end
|
2018-11-28 04:51:44 +03:00
|
|
|
|
|
|
|
def test_wait_interruption
|
|
|
|
cond = @monitor.new_cond
|
2019-10-19 22:52:20 +03:00
|
|
|
|
2018-11-28 04:51:44 +03:00
|
|
|
th = Thread.start {
|
|
|
|
@monitor.synchronize do
|
|
|
|
begin
|
|
|
|
cond.wait(0.1)
|
2019-10-19 22:52:20 +03:00
|
|
|
@monitor.mon_owned?
|
2018-11-28 04:51:44 +03:00
|
|
|
rescue Interrupt
|
2019-10-19 22:52:20 +03:00
|
|
|
@monitor.mon_owned?
|
2018-11-28 04:51:44 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
sleep(0.1)
|
|
|
|
th.raise(Interrupt)
|
2019-10-19 22:52:20 +03:00
|
|
|
|
|
|
|
begin
|
|
|
|
assert_equal true, th.value
|
|
|
|
rescue Interrupt
|
|
|
|
end
|
2018-11-28 04:51:44 +03:00
|
|
|
end
|
2003-11-12 07:01:21 +03:00
|
|
|
end
|