summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--test/fiber/scheduler.rb16
-rw-r--r--test/fiber/test_scheduler.rb79
2 files changed, 54 insertions, 41 deletions
diff --git a/test/fiber/scheduler.rb b/test/fiber/scheduler.rb
index 820c46dfb0..8f1ce4376b 100644
--- a/test/fiber/scheduler.rb
+++ b/test/fiber/scheduler.rb
@@ -489,15 +489,19 @@ class IOBufferScheduler < Scheduler
end
class IOScheduler < Scheduler
- def __io_ops__
- @__io_ops__ ||= []
+ def operations
+ @operations ||= []
end
def io_write(io, buffer, length, offset)
- fd = io.fileno
- str = buffer.get_string
- __io_ops__ << [:io_write, fd, str]
- Fiber.blocking { buffer.write(io, 0, offset) }
+ descriptor = io.fileno
+ string = buffer.get_string
+
+ self.operations << [:io_write, descriptor, string]
+
+ Fiber.blocking do
+ buffer.write(io, 0, offset)
+ end
end
end
diff --git a/test/fiber/test_scheduler.rb b/test/fiber/test_scheduler.rb
index 0cbd49daca..d3696267f7 100644
--- a/test/fiber/test_scheduler.rb
+++ b/test/fiber/test_scheduler.rb
@@ -288,90 +288,99 @@ class TestFiberScheduler < Test::Unit::TestCase
def test_io_write_on_flush
begin
- fn = File.join(Dir.tmpdir, "ruby_test_io_write_on_flush_#{SecureRandom.hex}")
- write_fd = nil
- io_ops = nil
+ path = File.join(Dir.tmpdir, "ruby_test_io_write_on_flush_#{SecureRandom.hex}")
+ descriptor = nil
+ operations = nil
+
thread = Thread.new do
scheduler = IOScheduler.new
Fiber.set_scheduler scheduler
Fiber.schedule do
- File.open(fn, 'w+') do |f|
- write_fd = f.fileno
- f << 'foo'
- f.flush
- f << 'bar'
+ File.open(path, 'w+') do |file|
+ descriptor = file.fileno
+ file << 'foo'
+ file.flush
+ file << 'bar'
end
end
- io_ops = scheduler.__io_ops__
+
+ operations = scheduler.operations
end
+
thread.join
assert_equal [
- [:io_write, write_fd, 'foo'],
- [:io_write, write_fd, 'bar']
- ], io_ops
+ [:io_write, descriptor, 'foo'],
+ [:io_write, descriptor, 'bar']
+ ], operations
- assert_equal 'foobar', IO.read(fn)
+ assert_equal 'foobar', IO.read(path)
ensure
thread.kill rescue nil
- FileUtils.rm_f(fn)
+ FileUtils.rm_f(path)
end
end
def test_io_read_error
- fn = File.join(Dir.tmpdir, "ruby_test_io_read_error_#{SecureRandom.hex}")
- exception = nil
+ path = File.join(Dir.tmpdir, "ruby_test_io_read_error_#{SecureRandom.hex}")
+ error = nil
+
thread = Thread.new do
scheduler = IOErrorScheduler.new
Fiber.set_scheduler scheduler
Fiber.schedule do
- File.open(fn, 'w+') { it.read }
- rescue => e
- exception = e
+ File.open(path, 'w+') { it.read }
+ rescue => error
+ # Ignore.
end
end
+
thread.join
- assert_kind_of Errno::EBADF, exception
+ assert_kind_of Errno::EBADF, error
ensure
thread.kill rescue nil
- FileUtils.rm_f(fn)
+ FileUtils.rm_f(path)
end
def test_io_write_error
- fn = File.join(Dir.tmpdir, "ruby_test_io_write_error_#{SecureRandom.hex}")
- exception = nil
+ path = File.join(Dir.tmpdir, "ruby_test_io_write_error_#{SecureRandom.hex}")
+ error = nil
+
thread = Thread.new do
scheduler = IOErrorScheduler.new
Fiber.set_scheduler scheduler
Fiber.schedule do
- File.open(fn, 'w+') { it.sync = true; it << 'foo' }
- rescue => e
- exception = e
+ File.open(path, 'w+') { it.sync = true; it << 'foo' }
+ rescue => error
+ # Ignore.
end
end
+
thread.join
- assert_kind_of Errno::EINVAL, exception
+ assert_kind_of Errno::EINVAL, error
ensure
thread.kill rescue nil
- FileUtils.rm_f(fn)
+ FileUtils.rm_f(path)
end
def test_io_write_flush_error
- fn = File.join(Dir.tmpdir, "ruby_test_io_write_flush_error_#{SecureRandom.hex}")
- exception = nil
+ path = File.join(Dir.tmpdir, "ruby_test_io_write_flush_error_#{SecureRandom.hex}")
+ error = nil
+
thread = Thread.new do
scheduler = IOErrorScheduler.new
Fiber.set_scheduler scheduler
Fiber.schedule do
- File.open(fn, 'w+') { it << 'foo' }
- rescue => e
- exception = e
+ File.open(path, 'w+') { it << 'foo' }
+ rescue => error
+ # Ignore.
end
end
+
thread.join
- assert_kind_of Errno::EINVAL, exception
+ assert_kind_of Errno::EINVAL, error
ensure
thread.kill rescue nil
- FileUtils.rm_f(fn)
+ FileUtils.rm_f(path)
end
end