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

52 lines
959 B
Ruby
Raw Normal View History

# frozen_string_literal: true
require 'test/unit'
require_relative 'scheduler'
class TestFiberEnumerator < Test::Unit::TestCase
MESSAGE = "Hello World"
def test_read_characters
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
2020-07-15 23:19:47 -04:00
skip "I/O is not non-blocking!"
end
message = String.new
thread = Thread.new do
scheduler = Scheduler.new
2020-10-15 21:25:58 -04:00
Fiber.set_scheduler scheduler
e = i.to_enum(:each_char)
Fiber.schedule do
o.write("Hello World")
o.close
end
Fiber.schedule do
begin
while c = e.next
message << c
end
rescue StopIteration
# Ignore.
end
i.close
end
end
thread.join
assert_equal(MESSAGE, message)
assert_predicate(i, :closed?)
assert_predicate(o, :closed?)
end
end