diff options
author | Samuel Williams <[email protected]> | 2024-10-31 17:26:37 +1300 |
---|---|---|
committer | GitHub <[email protected]> | 2024-10-31 17:26:37 +1300 |
commit | 87fb44dff6409a19d12052cf0fc07ba80a4c45ac (patch) | |
tree | 1446cd9390356ae660c50ef7ae877bc57af8d567 /test | |
parent | 550ac2f2edc07d1b63e3755233df0758a652b53f (diff) |
Introduce Fiber Scheduler `blocking_region` hook. (#11963)
Notes
Notes:
Merged-By: ioquatix <[email protected]>
Diffstat (limited to 'test')
-rw-r--r-- | test/fiber/scheduler.rb | 16 | ||||
-rw-r--r-- | test/fiber/test_io.rb | 5 | ||||
-rw-r--r-- | test/fiber/test_io_buffer.rb | 3 | ||||
-rw-r--r-- | test/fiber/test_process.rb | 4 |
4 files changed, 16 insertions, 12 deletions
diff --git a/test/fiber/scheduler.rb b/test/fiber/scheduler.rb index 3926226ca3..91fba0476e 100644 --- a/test/fiber/scheduler.rb +++ b/test/fiber/scheduler.rb @@ -309,6 +309,10 @@ class Scheduler Addrinfo.getaddrinfo(hostname, nil).map(&:ip_address).uniq end.value end + + def blocking_region(work) + Thread.new(&work).join + end end # This scheduler class implements `io_read` and `io_write` hooks which require @@ -321,8 +325,7 @@ class IOBufferScheduler < Scheduler io.nonblock = true while true - maximum_size = buffer.size - offset - result = blocking{buffer.read(io, maximum_size, offset)} + result = blocking{buffer.read(io, 0, offset)} if result > 0 total += result @@ -349,8 +352,7 @@ class IOBufferScheduler < Scheduler io.nonblock = true while true - maximum_size = buffer.size - offset - result = blocking{buffer.write(io, maximum_size, offset)} + result = blocking{buffer.write(io, 0, offset)} if result > 0 total += result @@ -377,8 +379,7 @@ class IOBufferScheduler < Scheduler io.nonblock = true while true - maximum_size = buffer.size - offset - result = blocking{buffer.pread(io, from, maximum_size, offset)} + result = blocking{buffer.pread(io, from, 0, offset)} if result > 0 total += result @@ -406,8 +407,7 @@ class IOBufferScheduler < Scheduler io.nonblock = true while true - maximum_size = buffer.size - offset - result = blocking{buffer.pwrite(io, from, maximum_size, offset)} + result = blocking{buffer.pwrite(io, from, 0, offset)} if result > 0 total += result diff --git a/test/fiber/test_io.rb b/test/fiber/test_io.rb index 4891c607f7..39e32c5987 100644 --- a/test/fiber/test_io.rb +++ b/test/fiber/test_io.rb @@ -153,12 +153,13 @@ class TestFiberIO < Test::Unit::TestCase Fiber.set_scheduler scheduler Fiber.schedule do - message = i.read(20) + # We add 1 here, to force the read to block (testing that specific code path). + message = i.read(MESSAGE.bytesize + 1) i.close end Fiber.schedule do - o.write("Hello World") + o.write(MESSAGE) o.close end end diff --git a/test/fiber/test_io_buffer.rb b/test/fiber/test_io_buffer.rb index a08b1ce1a9..19e6c1f88e 100644 --- a/test/fiber/test_io_buffer.rb +++ b/test/fiber/test_io_buffer.rb @@ -21,7 +21,8 @@ class TestFiberIOBuffer < Test::Unit::TestCase Fiber.set_scheduler scheduler Fiber.schedule do - message = i.read(20) + # We add 1 here, to force the read to block (testing that specific code path). + message = i.read(MESSAGE.bytesize + 1) i.close end diff --git a/test/fiber/test_process.rb b/test/fiber/test_process.rb index a09b070c0a..f17f767704 100644 --- a/test/fiber/test_process.rb +++ b/test/fiber/test_process.rb @@ -59,12 +59,14 @@ class TestFiberProcess < Test::Unit::TestCase def test_fork omit 'fork not supported' unless Process.respond_to?(:fork) + + pid = Process.fork{} + Thread.new do scheduler = Scheduler.new Fiber.set_scheduler scheduler Fiber.schedule do - pid = Process.fork {} Process.wait(pid) assert_predicate $?, :success? |