summaryrefslogtreecommitdiff
path: root/test/scheduler/test_enumerator.rb
blob: 7c97382c52f15db0a10d28f8ecbad15c55f2c644 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# frozen_string_literal: true
require 'test/unit'
require 'socket'
require_relative 'scheduler'

class TestSchedulerEnumerator < Test::Unit::TestCase
  MESSAGE = "Hello World"

  def test_read_characters
    skip unless defined?(UNIXSocket)

    i, o = UNIXSocket.pair
    skip unless i.nonblock? && o.nonblock?

    message = String.new

    thread = Thread.new do
      scheduler = Scheduler.new
      Thread.current.scheduler = scheduler

      e = i.to_enum(:each_char)

      Fiber do
        o.write("Hello World")
        o.close
      end

      Fiber do
        begin
          while c = e.next
            message << c
          end
        rescue StopIteration
          # Ignore.
        end

        i.close
      end
    end

    thread.join

    assert_equal(MESSAGE, message)
  end
end