summaryrefslogtreecommitdiff
path: root/test/fiber/test_io.rb
blob: e7f06f74297f7ab34be47a37ff3953eddd1ba226 (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
# frozen_string_literal: true
require 'test/unit'
require_relative 'scheduler'

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

  def test_read
    skip unless defined?(UNIXSocket)

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

    message = nil

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

      Fiber do
        message = i.read(20)
        i.close
      end

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

    thread.join

    assert_equal MESSAGE, message
  end
end