2020-05-14 06:10:55 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
require 'test/unit'
|
|
|
|
require_relative 'scheduler'
|
|
|
|
|
2020-05-23 03:43:58 -04:00
|
|
|
class TestFiberIO < Test::Unit::TestCase
|
2020-05-14 06:10:55 -04:00
|
|
|
MESSAGE = "Hello World"
|
|
|
|
|
|
|
|
def test_read
|
2022-01-11 07:17:53 -05:00
|
|
|
omit "UNIXSocket is not defined!" unless defined?(UNIXSocket)
|
2020-05-14 06:10:55 -04:00
|
|
|
|
|
|
|
i, o = UNIXSocket.pair
|
2020-07-15 23:19:47 -04:00
|
|
|
|
|
|
|
unless i.nonblock? && o.nonblock?
|
|
|
|
i.close
|
|
|
|
o.close
|
2022-01-11 07:17:53 -05:00
|
|
|
omit "I/O is not non-blocking!"
|
2020-07-15 23:19:47 -04:00
|
|
|
end
|
2020-05-14 06:10:55 -04:00
|
|
|
|
|
|
|
message = nil
|
|
|
|
|
|
|
|
thread = Thread.new do
|
|
|
|
scheduler = Scheduler.new
|
2020-10-15 21:25:58 -04:00
|
|
|
Fiber.set_scheduler scheduler
|
2020-05-14 06:10:55 -04:00
|
|
|
|
2020-08-20 08:52:36 -04:00
|
|
|
Fiber.schedule do
|
2020-05-14 06:10:55 -04:00
|
|
|
message = i.read(20)
|
|
|
|
i.close
|
|
|
|
end
|
|
|
|
|
2020-08-20 08:52:36 -04:00
|
|
|
Fiber.schedule do
|
2020-05-14 06:10:55 -04:00
|
|
|
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?)
|
2020-05-14 06:10:55 -04:00
|
|
|
end
|
2020-07-20 00:31:41 -04:00
|
|
|
|
2020-07-18 01:34:54 -04:00
|
|
|
def test_heavy_read
|
2022-01-11 07:17:53 -05:00
|
|
|
omit unless defined?(UNIXSocket)
|
2020-07-18 01:34:54 -04:00
|
|
|
|
|
|
|
16.times.map do
|
2020-07-23 10:44:25 -04:00
|
|
|
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
|
|
|
|
2020-08-20 08:52:36 -04:00
|
|
|
Fiber.schedule do
|
2020-07-23 10:44:25 -04:00
|
|
|
i.read(20)
|
2020-07-18 01:34:54 -04:00
|
|
|
i.close
|
|
|
|
end
|
|
|
|
|
2020-08-20 08:52:36 -04:00
|
|
|
Fiber.schedule do
|
2020-07-18 01:34:54 -04:00
|
|
|
o.write("Hello World")
|
|
|
|
o.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end.each(&:join)
|
|
|
|
end
|
2021-03-30 03:31:19 -04:00
|
|
|
|
|
|
|
def test_epipe_on_read
|
2022-01-11 07:17:53 -05:00
|
|
|
omit "UNIXSocket is not defined!" unless defined?(UNIXSocket)
|
2021-03-30 03:31:19 -04:00
|
|
|
|
|
|
|
i, o = UNIXSocket.pair
|
|
|
|
|
|
|
|
unless i.nonblock? && o.nonblock?
|
|
|
|
i.close
|
|
|
|
o.close
|
2022-01-11 07:17:53 -05:00
|
|
|
omit "I/O is not non-blocking!"
|
2021-03-30 03:31:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
error = nil
|
|
|
|
|
|
|
|
thread = Thread.new do
|
|
|
|
scheduler = Scheduler.new
|
|
|
|
Fiber.set_scheduler scheduler
|
|
|
|
|
|
|
|
Fiber.schedule do
|
|
|
|
begin
|
|
|
|
i.close
|
|
|
|
o.write(MESSAGE)
|
|
|
|
rescue => error
|
|
|
|
# Saved into error.
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
thread.join
|
|
|
|
|
|
|
|
i.close
|
|
|
|
o.close
|
|
|
|
|
|
|
|
assert_kind_of Errno::EPIPE, error
|
|
|
|
end
|
2021-08-25 06:03:35 -04:00
|
|
|
|
|
|
|
def test_tcp_accept
|
|
|
|
server = TCPServer.new('localhost', 0)
|
|
|
|
|
|
|
|
th = Thread.new do
|
|
|
|
Fiber.set_scheduler(Scheduler.new)
|
|
|
|
|
|
|
|
Fiber.schedule do
|
|
|
|
sender = server.accept
|
|
|
|
sender.wait_writable
|
|
|
|
sender.write "hello"
|
|
|
|
sender.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
recver = TCPSocket.new('localhost', server.local_address.ip_port)
|
|
|
|
assert "hello", recver.read
|
|
|
|
|
|
|
|
recver.close
|
|
|
|
server.close
|
|
|
|
th.join
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_tcp_connect
|
|
|
|
server = TCPServer.new('localhost', 0)
|
|
|
|
|
|
|
|
th = Thread.new do
|
|
|
|
Fiber.set_scheduler(Scheduler.new)
|
|
|
|
|
|
|
|
Fiber.schedule do
|
|
|
|
sender = TCPSocket.new('localhost', server.local_address.ip_port)
|
|
|
|
sender.write "hello"
|
|
|
|
sender.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
recver = server.accept
|
|
|
|
assert "hello", recver.read
|
|
|
|
|
|
|
|
recver.close
|
|
|
|
server.close
|
|
|
|
th.join
|
|
|
|
end
|
2021-12-18 00:19:30 -05:00
|
|
|
|
|
|
|
def test_read_write_blocking
|
2022-01-11 07:17:53 -05:00
|
|
|
omit "UNIXSocket is not defined!" unless defined?(UNIXSocket)
|
2021-12-18 00:19:30 -05:00
|
|
|
|
|
|
|
i, o = UNIXSocket.pair
|
|
|
|
i.nonblock = false
|
|
|
|
o.nonblock = false
|
|
|
|
|
|
|
|
message = nil
|
|
|
|
|
|
|
|
thread = Thread.new do
|
|
|
|
# This scheduler provides non-blocking `io_read`/`io_write`:
|
|
|
|
scheduler = IOBufferScheduler.new
|
|
|
|
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
|
|
|
|
assert_predicate(i, :closed?)
|
|
|
|
assert_predicate(o, :closed?)
|
|
|
|
end
|
2022-10-15 02:59:04 -04:00
|
|
|
|
|
|
|
def test_io_select
|
|
|
|
omit "UNIXSocket is not defined!" unless defined?(UNIXSocket)
|
|
|
|
|
|
|
|
UNIXSocket.pair do |r, w|
|
|
|
|
result = nil
|
|
|
|
|
|
|
|
thread = Thread.new do
|
|
|
|
scheduler = Scheduler.new
|
|
|
|
Fiber.set_scheduler scheduler
|
|
|
|
|
|
|
|
Fiber.schedule do
|
|
|
|
w.write("Hello World")
|
|
|
|
result = IO.select([r], [w])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
thread.join
|
|
|
|
|
|
|
|
assert_equal [[r], [w], []], result
|
|
|
|
end
|
|
|
|
end
|
2020-05-14 06:10:55 -04:00
|
|
|
end
|