1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/test/fiber/test_io_buffer.rb
Kazuki Yamaguchi 6b67f06312 test/fiber/test_io_buffer.rb: fix file descriptor leaks
I got the warning while running "make test-all":

	Leaked file descriptor: TestFiberIOBuffer#test_write_nonblock: 9 : #<UNIXSocket:fd 9>
	Closed file descriptor: TestFiberIOBuffer#test_read_write_blocking: 9
	Leaked file descriptor: TestFiberIOBuffer#test_timeout_after: 10 : #<UNIXSocket:fd 10>
	Closed file descriptor: TestFiberIOBuffer#test_read_nonblock: 10
2021-12-21 00:05:24 +09:00

125 lines
2.3 KiB
Ruby

# frozen_string_literal: true
require 'test/unit'
require_relative 'scheduler'
require 'timeout'
class TestFiberIOBuffer < Test::Unit::TestCase
MESSAGE = "Hello World"
def test_read_write_blocking
skip "UNIXSocket is not defined!" unless defined?(UNIXSocket)
i, o = UNIXSocket.pair
i.nonblock = false
o.nonblock = false
message = nil
thread = Thread.new do
scheduler = IOBufferScheduler.new
Fiber.set_scheduler scheduler
Fiber.schedule do
message = i.read(20)
i.close
end
Fiber.schedule do
o.write(MESSAGE)
o.close
end
end
thread.join
assert_equal MESSAGE, message
assert_predicate(i, :closed?)
assert_predicate(o, :closed?)
ensure
i&.close
o&.close
end
def test_timeout_after
skip "UNIXSocket is not defined!" unless defined?(UNIXSocket)
i, o = UNIXSocket.pair
i.nonblock = false
o.nonblock = false
message = nil
error = nil
thread = Thread.new do
scheduler = IOBufferScheduler.new
Fiber.set_scheduler scheduler
Fiber.schedule do
Timeout.timeout(0.1) do
message = i.read(20)
end
rescue Timeout::Error => error
# Assertions below.
ensure
i.close
end
end
thread.join
assert_nil message
assert_kind_of Timeout::Error, error
ensure
i&.close
o&.close
end
def test_read_nonblock
skip "UNIXSocket is not defined!" unless defined?(UNIXSocket)
i, o = UNIXSocket.pair
message = nil
thread = Thread.new do
scheduler = IOBufferScheduler.new
Fiber.set_scheduler scheduler
Fiber.schedule do
message = i.read_nonblock(20, exception: false)
i.close
end
end
thread.join
assert_equal :wait_readable, message
ensure
i&.close
o&.close
end
def test_write_nonblock
skip "UNIXSocket is not defined!" unless defined?(UNIXSocket)
i, o = UNIXSocket.pair
thread = Thread.new do
scheduler = IOBufferScheduler.new
Fiber.set_scheduler scheduler
Fiber.schedule do
o.write_nonblock(MESSAGE, exception: false)
o.close
end
end
thread.join
assert_equal MESSAGE, i.read
ensure
i&.close
o&.close
end
end