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 TestFiberEnumerator < Test::Unit::TestCase
|
2020-05-14 06:10:55 -04:00
|
|
|
MESSAGE = "Hello World"
|
|
|
|
|
|
|
|
def test_read_characters
|
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
|
|
|
|
2020-07-15 03:36:57 -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 03:36:57 -04:00
|
|
|
end
|
2020-05-14 06:10:55 -04:00
|
|
|
|
|
|
|
message = String.new
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
e = i.to_enum(:each_char)
|
|
|
|
|
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
|
|
|
|
|
2020-08-20 08:52:36 -04:00
|
|
|
Fiber.schedule do
|
2020-05-14 06:10:55 -04:00
|
|
|
begin
|
|
|
|
while c = e.next
|
|
|
|
message << c
|
|
|
|
end
|
|
|
|
rescue StopIteration
|
|
|
|
# Ignore.
|
|
|
|
end
|
|
|
|
|
|
|
|
i.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
thread.join
|
|
|
|
|
|
|
|
assert_equal(MESSAGE, message)
|
2020-07-15 03:36:57 -04:00
|
|
|
assert_predicate(i, :closed?)
|
|
|
|
assert_predicate(o, :closed?)
|
2020-05-14 06:10:55 -04:00
|
|
|
end
|
|
|
|
end
|