diff options
author | Samuel Williams <samuel.williams@oriontransfer.co.nz> | 2022-05-22 00:32:41 +1200 |
---|---|---|
committer | Samuel Williams <samuel.williams@oriontransfer.co.nz> | 2022-05-25 15:24:24 +1200 |
commit | 42bcc629fba518215c844488223bc279006a4fa2 (patch) | |
tree | b1953c08c41e526c501c78b76aa6360e6e1ab5f2 /test | |
parent | 901525b1079ac02da0122a76d8e4c3546a7f80f6 (diff) | |
download | ruby-42bcc629fba518215c844488223bc279006a4fa2.tar.gz |
Retain reference to blocking fibers.
Diffstat (limited to 'test')
-rw-r--r-- | test/fiber/scheduler.rb | 14 | ||||
-rw-r--r-- | test/fiber/test_scheduler.rb | 26 |
2 files changed, 22 insertions, 18 deletions
diff --git a/test/fiber/scheduler.rb b/test/fiber/scheduler.rb index 4138015e4b..96b22856d1 100644 --- a/test/fiber/scheduler.rb +++ b/test/fiber/scheduler.rb @@ -30,7 +30,7 @@ class Scheduler @closed = false @lock = Thread::Mutex.new - @blocking = 0 + @blocking = Hash.new.compare_by_identity @ready = [] @urgent = IO.pipe @@ -57,7 +57,7 @@ class Scheduler def run # $stderr.puts [__method__, Fiber.current].inspect - while @readable.any? or @writable.any? or @waiting.any? or @blocking.positive? + while @readable.any? or @writable.any? or @waiting.any? or @blocking.any? # Can only handle file descriptors up to 1024... readable, writable = IO.select(@readable.keys + [@urgent.first], @writable.keys, [], next_timeout) @@ -211,20 +211,22 @@ class Scheduler def block(blocker, timeout = nil) # $stderr.puts [__method__, blocker, timeout].inspect + fiber = Fiber.current + if timeout - @waiting[Fiber.current] = current_time + timeout + @waiting[fiber] = current_time + timeout begin Fiber.yield ensure # Remove from @waiting in the case #unblock was called before the timeout expired: - @waiting.delete(Fiber.current) + @waiting.delete(fiber) end else - @blocking += 1 + @blocking[fiber] = true begin Fiber.yield ensure - @blocking -= 1 + @blocking.delete(fiber) end end end diff --git a/test/fiber/test_scheduler.rb b/test/fiber/test_scheduler.rb index f1030ea3fa..4b1310f0a6 100644 --- a/test/fiber/test_scheduler.rb +++ b/test/fiber/test_scheduler.rb @@ -106,22 +106,24 @@ class TestFiberScheduler < Test::Unit::TestCase end def test_autoload - Object.autoload(:TestFiberSchedulerAutoload, File.expand_path("autoload.rb", __dir__)) + 100.times do + Object.autoload(:TestFiberSchedulerAutoload, File.expand_path("autoload.rb", __dir__)) - thread = Thread.new do - scheduler = Scheduler.new - Fiber.set_scheduler scheduler + thread = Thread.new do + scheduler = Scheduler.new + Fiber.set_scheduler scheduler - 10.times do - Fiber.schedule do - Object.const_get(:TestFiberSchedulerAutoload) + 10.times do + Fiber.schedule do + Object.const_get(:TestFiberSchedulerAutoload) + end end end - end - thread.join - ensure - $LOADED_FEATURES.delete(File.expand_path("autoload.rb", __dir__)) - Object.send(:remove_const, :TestFiberSchedulerAutoload) + thread.join + ensure + $LOADED_FEATURES.delete(File.expand_path("autoload.rb", __dir__)) + Object.send(:remove_const, :TestFiberSchedulerAutoload) + end end end |