summaryrefslogtreecommitdiff
path: root/test/-ext-/wait_for_single_fd/test_wait_for_single_fd.rb
blob: 453870796e269c2b0a989be4554dfacc52b739d9 (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
46
47
48
49
50
51
52
53
54
55
56
# frozen_string_literal: false
require 'test/unit'

class TestWaitForSingleFD < Test::Unit::TestCase
  require '-test-/wait_for_single_fd'

  def with_pipe
    r, w = IO.pipe
    begin
      yield r, w
    ensure
      r.close unless r.closed?
      w.close unless w.closed?
    end
  end

  def test_wait_for_valid_fd
    with_pipe do |r,w|
      rc = IO.wait_for_single_fd(w.fileno, RB_WAITFD_OUT, nil)
      assert_equal RB_WAITFD_OUT, rc
    end
  end

  def test_wait_for_invalid_fd
    # Negative FDs should not cause NoMemoryError or segfault when
    # using select().  For now, match the poll() implementation
    # used on Linux, which sleeps the given amount of time given
    # when fd is negative (as documented in the Linux poll(2) manpage)
    assert_equal 0, IO.wait_for_single_fd(-999, RB_WAITFD_IN, 0)
    assert_equal 0, IO.wait_for_single_fd(-1, RB_WAITFD_OUT, 0)

    # FreeBSD 8.2 or prior sticks this
    # http://bugs.ruby-lang.org/issues/5524
    if /freebsd([\d\.]+)/ =~ RUBY_PLATFORM
      ver = $1.to_r
      skip 'FreeBSD <= 8.2' if ver <= 8.2r
    end
    with_pipe do |r,w|
      wfd = w.fileno
      w.close
      assert_raise(Errno::EBADF) do
        IO.wait_for_single_fd(wfd, RB_WAITFD_OUT, nil)
      end
    end
  end

  def test_wait_for_closed_pipe
    with_pipe do |r,w|
      w.close
      rc = IO.wait_for_single_fd(r.fileno, RB_WAITFD_IN, nil)
      assert_equal RB_WAITFD_IN, rc
    end
  end


end