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.rb

66 lines
1.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require 'test/unit'
require_relative 'scheduler'
class TestFiberIO < Test::Unit::TestCase
MESSAGE = "Hello World"
def test_read
2020-07-15 23:19:47 -04:00
skip "UNIXSocket is not defined!" unless defined?(UNIXSocket)
i, o = UNIXSocket.pair
2020-07-15 23:19:47 -04:00
unless i.nonblock? && o.nonblock?
i.close
o.close
skip "I/O is not non-blocking!"
end
message = nil
thread = Thread.new do
scheduler = Scheduler.new
2020-10-15 21:25:58 -04:00
Fiber.set_scheduler scheduler
Fiber.schedule do
message = i.read(20)
i.close
end
Fiber.schedule do
o.write("Hello World")
o.close
end
end
thread.join
assert_equal MESSAGE, message
2020-07-15 23:19:47 -04:00
assert_predicate(i, :closed?)
assert_predicate(o, :closed?)
end
2020-07-20 00:31:41 -04:00
2020-07-18 01:34:54 -04:00
def test_heavy_read
skip unless defined?(UNIXSocket)
16.times.map do
Thread.new do
2020-07-18 01:34:54 -04:00
i, o = UNIXSocket.pair
2020-07-20 00:31:41 -04:00
2020-07-18 01:34:54 -04:00
scheduler = Scheduler.new
2020-10-15 21:25:58 -04:00
Fiber.set_scheduler scheduler
2020-07-18 01:34:54 -04:00
Fiber.schedule do
i.read(20)
2020-07-18 01:34:54 -04:00
i.close
end
Fiber.schedule do
2020-07-18 01:34:54 -04:00
o.write("Hello World")
o.close
end
end
end.each(&:join)
end
end