2020-05-14 06:10:55 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
require 'test/unit'
|
|
|
|
require_relative 'scheduler'
|
|
|
|
|
2020-05-23 03:43:58 -04:00
|
|
|
class TestFiberScheduler < Test::Unit::TestCase
|
2020-05-14 06:10:55 -04:00
|
|
|
def test_fiber_without_scheduler
|
|
|
|
# Cannot create fiber without scheduler.
|
|
|
|
assert_raise RuntimeError do
|
2020-08-20 08:52:36 -04:00
|
|
|
Fiber.schedule do
|
2020-05-14 06:10:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-09-23 02:46:10 -04:00
|
|
|
|
2020-10-15 21:25:58 -04:00
|
|
|
def test_fiber_new
|
|
|
|
f = Fiber.new{}
|
|
|
|
refute f.blocking?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_fiber_new_with_options
|
|
|
|
f = Fiber.new(blocking: true){}
|
|
|
|
assert f.blocking?
|
|
|
|
|
|
|
|
f = Fiber.new(blocking: false){}
|
|
|
|
refute f.blocking?
|
|
|
|
|
|
|
|
f = Fiber.new(pool: nil){}
|
|
|
|
refute f.blocking?
|
|
|
|
end
|
|
|
|
|
2022-10-06 06:00:49 -04:00
|
|
|
def test_fiber_blocking
|
|
|
|
f = Fiber.new(blocking: false) do
|
|
|
|
fiber = Fiber.current
|
|
|
|
refute fiber.blocking?
|
|
|
|
Fiber.blocking do |_fiber|
|
|
|
|
assert_equal fiber, _fiber
|
|
|
|
assert fiber.blocking?
|
|
|
|
end
|
|
|
|
end
|
2022-10-13 02:04:06 -04:00
|
|
|
f.resume
|
2022-10-06 06:00:49 -04:00
|
|
|
end
|
|
|
|
|
2020-09-19 19:34:02 -04:00
|
|
|
def test_closed_at_thread_exit
|
|
|
|
scheduler = Scheduler.new
|
|
|
|
|
|
|
|
thread = Thread.new do
|
2020-10-15 21:25:58 -04:00
|
|
|
Fiber.set_scheduler scheduler
|
2020-09-19 19:34:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
thread.join
|
|
|
|
|
|
|
|
assert scheduler.closed?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_closed_when_set_to_nil
|
|
|
|
scheduler = Scheduler.new
|
|
|
|
|
|
|
|
thread = Thread.new do
|
2020-10-15 21:25:58 -04:00
|
|
|
Fiber.set_scheduler scheduler
|
|
|
|
Fiber.set_scheduler nil
|
2020-09-19 19:34:02 -04:00
|
|
|
|
|
|
|
assert scheduler.closed?
|
|
|
|
end
|
|
|
|
|
|
|
|
thread.join
|
|
|
|
end
|
2020-09-29 22:24:36 -04:00
|
|
|
|
|
|
|
def test_close_at_exit
|
|
|
|
assert_in_out_err %W[-I#{__dir__} -], <<-RUBY, ['Running Fiber'], [], success: true
|
|
|
|
require 'scheduler'
|
2021-11-09 21:42:57 -05:00
|
|
|
Warning[:experimental] = false
|
2020-09-29 22:24:36 -04:00
|
|
|
|
|
|
|
scheduler = Scheduler.new
|
2020-10-15 21:25:58 -04:00
|
|
|
Fiber.set_scheduler scheduler
|
2020-09-29 22:24:36 -04:00
|
|
|
|
|
|
|
Fiber.schedule do
|
|
|
|
sleep(0)
|
|
|
|
puts "Running Fiber"
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
2020-09-30 20:42:58 -04:00
|
|
|
|
2021-06-14 01:56:53 -04:00
|
|
|
def test_minimal_interface
|
|
|
|
scheduler = Object.new
|
|
|
|
|
|
|
|
def scheduler.block
|
|
|
|
end
|
|
|
|
|
|
|
|
def scheduler.unblock
|
|
|
|
end
|
|
|
|
|
|
|
|
def scheduler.io_wait
|
|
|
|
end
|
|
|
|
|
|
|
|
def scheduler.kernel_sleep
|
|
|
|
end
|
|
|
|
|
2020-09-30 20:42:58 -04:00
|
|
|
thread = Thread.new do
|
2021-06-14 01:56:53 -04:00
|
|
|
Fiber.set_scheduler scheduler
|
2020-09-30 20:42:58 -04:00
|
|
|
end
|
2020-10-01 00:47:57 -04:00
|
|
|
|
2020-09-30 20:42:58 -04:00
|
|
|
thread.join
|
|
|
|
end
|
2021-02-11 22:54:52 -05:00
|
|
|
|
|
|
|
def test_current_scheduler
|
|
|
|
thread = Thread.new do
|
|
|
|
scheduler = Scheduler.new
|
|
|
|
Fiber.set_scheduler scheduler
|
|
|
|
|
|
|
|
assert Fiber.scheduler
|
|
|
|
refute Fiber.current_scheduler
|
|
|
|
|
|
|
|
Fiber.schedule do
|
|
|
|
assert Fiber.current_scheduler
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
thread.join
|
|
|
|
end
|
2022-05-07 18:22:58 -04:00
|
|
|
|
|
|
|
def test_autoload
|
2022-05-21 08:32:41 -04:00
|
|
|
100.times do
|
|
|
|
Object.autoload(:TestFiberSchedulerAutoload, File.expand_path("autoload.rb", __dir__))
|
2022-05-07 18:22:58 -04:00
|
|
|
|
2022-05-21 08:32:41 -04:00
|
|
|
thread = Thread.new do
|
|
|
|
scheduler = Scheduler.new
|
|
|
|
Fiber.set_scheduler scheduler
|
2022-05-07 18:22:58 -04:00
|
|
|
|
2022-05-21 08:32:41 -04:00
|
|
|
10.times do
|
|
|
|
Fiber.schedule do
|
|
|
|
Object.const_get(:TestFiberSchedulerAutoload)
|
|
|
|
end
|
2022-05-07 18:22:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-21 08:32:41 -04:00
|
|
|
thread.join
|
|
|
|
ensure
|
|
|
|
$LOADED_FEATURES.delete(File.expand_path("autoload.rb", __dir__))
|
|
|
|
Object.send(:remove_const, :TestFiberSchedulerAutoload)
|
|
|
|
end
|
2022-05-07 18:22:58 -04:00
|
|
|
end
|
2022-10-19 20:38:52 -04:00
|
|
|
|
|
|
|
def test_deadlock
|
|
|
|
mutex = Thread::Mutex.new
|
|
|
|
condition = Thread::ConditionVariable.new
|
|
|
|
q = 0.0001
|
|
|
|
|
|
|
|
signaller = Thread.new do
|
|
|
|
loop do
|
|
|
|
mutex.synchronize do
|
|
|
|
condition.signal
|
|
|
|
end
|
|
|
|
sleep q
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
|
|
|
|
thread = Thread.new do
|
|
|
|
scheduler = SleepingBlockingScheduler.new
|
|
|
|
Fiber.set_scheduler scheduler
|
|
|
|
|
|
|
|
Fiber.schedule do
|
|
|
|
10.times do
|
|
|
|
mutex.synchronize do
|
|
|
|
condition.wait(mutex)
|
|
|
|
sleep q
|
|
|
|
i += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Wait for 10 seconds at most... if it doesn't finish, it's deadlocked.
|
|
|
|
thread.join(10)
|
|
|
|
|
|
|
|
# If it's deadlocked, it will never finish, so this will be 0.
|
|
|
|
assert_equal 10, i
|
|
|
|
ensure
|
|
|
|
# Make sure the threads are dead...
|
|
|
|
thread.kill
|
|
|
|
signaller.kill
|
|
|
|
end
|
2020-05-14 06:10:55 -04:00
|
|
|
end
|