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
|
|
|
|
|
2021-04-27 12:42:50 -04:00
|
|
|
def test_enter_in_different_fibers
|
|
|
|
@monitor.enter
|
|
|
|
Fiber.new {
|
|
|
|
assert_equal false, @monitor.try_enter
|
|
|
|
}.resume
|
|
|
|
end
|
|
|
|
|
2003-11-11 23:01:21 -05:00
|
|
|
def test_enter
|
|
|
|
ary = []
|
2021-06-28 10:01:53 -04:00
|
|
|
queue = Thread::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
|
|
|
|
|
2019-11-11 20:02:47 -05:00
|
|
|
def test_exit
|
|
|
|
m = Monitor.new
|
|
|
|
m.enter
|
|
|
|
assert_equal true, m.mon_owned?
|
|
|
|
m.exit
|
|
|
|
assert_equal false, m.mon_owned?
|
|
|
|
|
|
|
|
assert_raise ThreadError do
|
|
|
|
m.exit
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal false, m.mon_owned?
|
|
|
|
|
|
|
|
m.enter
|
|
|
|
Thread.new{
|
|
|
|
assert_raise(ThreadError) do
|
|
|
|
m.exit
|
|
|
|
end
|
|
|
|
}.join
|
|
|
|
assert_equal true, m.mon_owned?
|
|
|
|
m.exit
|
|
|
|
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 = []
|
2021-06-28 10:01:53 -04:00
|
|
|
queue = Thread::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 = []
|
2021-06-28 10:01:53 -04:00
|
|
|
queue = Thread::Queue.new
|
2009-10-20 11:57:39 -04:00
|
|
|
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
|
2021-06-28 10:01:53 -04:00
|
|
|
queue1 = Thread::Queue.new
|
|
|
|
queue2 = Thread::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
|
2021-06-28 10:01:53 -04:00
|
|
|
queue1 = Thread::Queue.new
|
|
|
|
queue2 = Thread::Queue.new
|
2017-09-19 21:40:53 -04:00
|
|
|
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"
|
2021-06-28 10:01:53 -04:00
|
|
|
queue1 = Thread::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
|
|
|
|
2019-12-03 23:36:41 -05:00
|
|
|
class NewCondTest
|
|
|
|
include MonitorMixin
|
|
|
|
attr_reader :cond
|
|
|
|
def initialize
|
|
|
|
@cond = new_cond
|
|
|
|
super # mon_initialize
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_new_cond_before_initialize
|
|
|
|
assert NewCondTest.new.cond.instance_variable_get(:@monitor) != nil
|
|
|
|
end
|
|
|
|
|
2020-07-11 03:01:05 -04:00
|
|
|
class KeywordInitializeParent
|
|
|
|
def initialize(x:)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class KeywordInitializeChild < KeywordInitializeParent
|
|
|
|
include MonitorMixin
|
|
|
|
def initialize
|
|
|
|
super(x: 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_initialize_with_keyword_arg
|
|
|
|
assert KeywordInitializeChild.new
|
|
|
|
end
|
|
|
|
|
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"
|
2021-06-28 10:01:53 -04:00
|
|
|
queue2 = Thread::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"
|
2021-06-28 10:01:53 -04:00
|
|
|
queue3 = Thread::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)
|
2020-02-05 19:14:40 -05:00
|
|
|
assert_equal(false, result3)
|
2014-11-13 08:04:43 -05:00
|
|
|
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
|
|
|
|
# }
|
2021-06-28 10:01:53 -04:00
|
|
|
# queue3 = Thread::Queue.new
|
2003-11-12 00:10:54 -05:00
|
|
|
# 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
|
|
|
|
cond = @monitor.new_cond
|
2019-10-19 15:52:20 -04:00
|
|
|
|
2018-11-27 20:51:44 -05:00
|
|
|
th = Thread.start {
|
|
|
|
@monitor.synchronize do
|
|
|
|
begin
|
|
|
|
cond.wait(0.1)
|
2019-10-19 15:52:20 -04:00
|
|
|
@monitor.mon_owned?
|
2018-11-27 20:51:44 -05:00
|
|
|
rescue Interrupt
|
2019-10-19 15:52:20 -04:00
|
|
|
@monitor.mon_owned?
|
2018-11-27 20:51:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
sleep(0.1)
|
|
|
|
th.raise(Interrupt)
|
2019-10-19 15:52:20 -04:00
|
|
|
|
|
|
|
begin
|
|
|
|
assert_equal true, th.value
|
|
|
|
rescue Interrupt
|
|
|
|
end
|
2018-11-27 20:51:44 -05:00
|
|
|
end
|
2003-11-11 23:01:21 -05:00
|
|
|
end
|