diff options
Diffstat (limited to 'test/fiber')
-rw-r--r-- | test/fiber/scheduler.rb | 22 | ||||
-rw-r--r-- | test/fiber/test_address_resolve.rb | 2 | ||||
-rw-r--r-- | test/fiber/test_enumerator.rb | 8 | ||||
-rw-r--r-- | test/fiber/test_io.rb | 15 | ||||
-rw-r--r-- | test/fiber/test_mutex.rb | 2 | ||||
-rw-r--r-- | test/fiber/test_process.rb | 27 | ||||
-rw-r--r-- | test/fiber/test_queue.rb | 20 | ||||
-rw-r--r-- | test/fiber/test_scheduler.rb | 2 | ||||
-rw-r--r-- | test/fiber/test_thread.rb | 2 |
9 files changed, 82 insertions, 18 deletions
diff --git a/test/fiber/scheduler.rb b/test/fiber/scheduler.rb index 5090271db1..3926226ca3 100644 --- a/test/fiber/scheduler.rb +++ b/test/fiber/scheduler.rb @@ -27,7 +27,9 @@ class Scheduler Warning[:experimental] = experimental end - def initialize + def initialize(fiber = Fiber.current) + @fiber = fiber + @readable = {} @writable = {} @waiting = {} @@ -45,6 +47,10 @@ class Scheduler attr :writable attr :waiting + def transfer + @fiber.transfer + end + def next_timeout _fiber, timeout = @waiting.min_by{|key, value| value} @@ -88,7 +94,7 @@ class Scheduler end selected.each do |fiber, events| - fiber.resume(events) + fiber.transfer(events) end if @waiting.any? @@ -98,7 +104,7 @@ class Scheduler waiting.each do |fiber, timeout| if fiber.alive? if timeout <= time - fiber.resume + fiber.transfer else @waiting[fiber] = timeout end @@ -114,7 +120,7 @@ class Scheduler end ready.each do |fiber| - fiber.resume + fiber.transfer end end end @@ -217,7 +223,7 @@ class Scheduler @waiting[fiber] = current_time + duration end - Fiber.yield + @fiber.transfer ensure @waiting.delete(fiber) if duration @readable.delete(io) if readable @@ -254,7 +260,7 @@ class Scheduler if timeout @waiting[fiber] = current_time + timeout begin - Fiber.yield + @fiber.transfer ensure # Remove from @waiting in the case #unblock was called before the timeout expired: @waiting.delete(fiber) @@ -262,7 +268,7 @@ class Scheduler else @blocking[fiber] = true begin - Fiber.yield + @fiber.transfer ensure @blocking.delete(fiber) end @@ -290,7 +296,7 @@ class Scheduler def fiber(&block) fiber = Fiber.new(blocking: false, &block) - fiber.resume + fiber.transfer return fiber end diff --git a/test/fiber/test_address_resolve.rb b/test/fiber/test_address_resolve.rb index 12223bcc06..09c8db6049 100644 --- a/test/fiber/test_address_resolve.rb +++ b/test/fiber/test_address_resolve.rb @@ -179,7 +179,7 @@ class TestAddressResolve < Test::Unit::TestCase Fiber.set_scheduler scheduler Fiber.schedule do - assert_raise(SocketError) { + assert_raise(Socket::ResolutionError) { Addrinfo.getaddrinfo("non-existing-domain.abc", nil) } end diff --git a/test/fiber/test_enumerator.rb b/test/fiber/test_enumerator.rb index 40f7d01725..e9410f925c 100644 --- a/test/fiber/test_enumerator.rb +++ b/test/fiber/test_enumerator.rb @@ -42,4 +42,12 @@ class TestFiberEnumerator < Test::Unit::TestCase assert_predicate(i, :closed?) assert_predicate(o, :closed?) end + + def enumerator_fiber_is_nonblocking + enumerator = Enumerator.new do |yielder| + yielder << Fiber.current.blocking? + end + + assert_equal(false, enumerator.next) + end end diff --git a/test/fiber/test_io.rb b/test/fiber/test_io.rb index c1ad56a8cf..0e3e086d5a 100644 --- a/test/fiber/test_io.rb +++ b/test/fiber/test_io.rb @@ -219,4 +219,19 @@ class TestFiberIO < Test::Unit::TestCase assert_equal [[r], [w], []], result end end + + def test_backquote + result = nil + + thread = Thread.new do + scheduler = Scheduler.new + Fiber.set_scheduler scheduler + Fiber.schedule do + result = `#{EnvUtil.rubybin} -e "sleep 0.1;puts %[ok]"` + end + end + thread.join + + assert_equal "ok\n", result + end end diff --git a/test/fiber/test_mutex.rb b/test/fiber/test_mutex.rb index 449c49f38b..2cee2cc235 100644 --- a/test/fiber/test_mutex.rb +++ b/test/fiber/test_mutex.rb @@ -207,7 +207,7 @@ class TestFiberMutex < Test::Unit::TestCase Fiber.schedule do mutex.synchronize do puts 'in synchronize' - Fiber.yield + scheduler.transfer end end diff --git a/test/fiber/test_process.rb b/test/fiber/test_process.rb index a5990be204..a09b070c0a 100644 --- a/test/fiber/test_process.rb +++ b/test/fiber/test_process.rb @@ -3,13 +3,15 @@ require 'test/unit' require_relative 'scheduler' class TestFiberProcess < Test::Unit::TestCase + TRUE_CMD = RUBY_PLATFORM =~ /mswin|mingw/ ? "exit 0" : "true" + def test_process_wait Thread.new do scheduler = Scheduler.new Fiber.set_scheduler scheduler Fiber.schedule do - pid = Process.spawn("true") + pid = Process.spawn(TRUE_CMD) Process.wait(pid) # TODO test that scheduler was invoked. @@ -25,7 +27,7 @@ class TestFiberProcess < Test::Unit::TestCase Fiber.set_scheduler scheduler Fiber.schedule do - system("true") + system(TRUE_CMD) # TODO test that scheduler was invoked (currently it's not). @@ -34,6 +36,27 @@ class TestFiberProcess < Test::Unit::TestCase end.join end + def test_system_faulty_process_wait + Thread.new do + scheduler = Scheduler.new + + def scheduler.process_wait(pid, flags) + Fiber.blocking{Process.wait(pid, flags)} + + # Don't return `Process::Status` instance. + return false + end + + Fiber.set_scheduler scheduler + + Fiber.schedule do + assert_raise TypeError do + system(TRUE_CMD) + end + end + end.join + end + def test_fork omit 'fork not supported' unless Process.respond_to?(:fork) Thread.new do diff --git a/test/fiber/test_queue.rb b/test/fiber/test_queue.rb index 10d9540492..d78b026f11 100644 --- a/test/fiber/test_queue.rb +++ b/test/fiber/test_queue.rb @@ -5,9 +5,10 @@ require_relative 'scheduler' class TestFiberQueue < Test::Unit::TestCase def test_pop_with_timeout queue = Thread::Queue.new + kill = false result = :unspecified - Thread.new do + thread = Thread.new do scheduler = Scheduler.new Fiber.set_scheduler(scheduler) @@ -16,17 +17,23 @@ class TestFiberQueue < Test::Unit::TestCase end scheduler.run - end.join + end + until thread.join(2) + kill = true + thread.kill + end + assert_false(kill, 'Getting stuck due to a possible compiler bug.') assert_nil result end def test_pop_with_timeout_and_value queue = Thread::Queue.new queue.push(:something) + kill = false result = :unspecified - Thread.new do + thread = Thread.new do scheduler = Scheduler.new Fiber.set_scheduler(scheduler) @@ -35,8 +42,13 @@ class TestFiberQueue < Test::Unit::TestCase end scheduler.run - end.join + end + until thread.join(2) + kill = true + thread.kill + end + assert_false(kill, 'Getting stuck due to a possible compiler bug.') assert_equal :something, result end end diff --git a/test/fiber/test_scheduler.rb b/test/fiber/test_scheduler.rb index f8993bb18f..34effad816 100644 --- a/test/fiber/test_scheduler.rb +++ b/test/fiber/test_scheduler.rb @@ -118,7 +118,7 @@ class TestFiberScheduler < Test::Unit::TestCase end def test_autoload - 100.times do + 10.times do Object.autoload(:TestFiberSchedulerAutoload, File.expand_path("autoload.rb", __dir__)) thread = Thread.new do diff --git a/test/fiber/test_thread.rb b/test/fiber/test_thread.rb index 6fae266955..5e3cc6d0e1 100644 --- a/test/fiber/test_thread.rb +++ b/test/fiber/test_thread.rb @@ -39,7 +39,7 @@ class TestFiberThread < Test::Unit::TestCase assert_predicate sleeper, :alive? ensure - sleeper&.kill + sleeper&.kill&.join end def test_thread_join_implicit |