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