sleepy_penguin.git  about / heads / tags
Linux I/O events for Ruby
blob 1611dd87276df160ac75e4f03b13243b557a103f 1871 bytes (raw)
$ git show pu:test/test_eventfd.rb	# shows this blob on the CLI

 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
57
58
59
60
61
62
63
64
 
require_relative 'helper'
require 'fcntl'

class TestEventFD < Test::Unit::TestCase
  include SleepyPenguin

  def test_constants
    defined?(EventFD::NONBLOCK) and
      assert_kind_of Integer, EventFD::NONBLOCK
    defined?(EventFD::CLOEXEC) and
      assert_kind_of Integer, EventFD::CLOEXEC
    defined?(EventFD::SEMAPHORE) and
      assert_kind_of Integer, EventFD::SEMAPHORE
    assert_equal 0xfffffffffffffffe, EventFD::MAX
  end

  def test_new
    efd = EventFD.new 0
    assert_kind_of(IO, efd)
    check_cloexec(efd)
  end

  def test_new_nonblock
    efd = EventFD.new(0, EventFD::NONBLOCK)
    flags = efd.fcntl(Fcntl::F_GETFL) & Fcntl::O_NONBLOCK
    assert_equal(Fcntl::O_NONBLOCK, flags)
  end if defined?(EventFD::NONBLOCK)

  def test_new_nonblock_cloexec_sym
    efd = EventFD.new(0, [:NONBLOCK,:CLOEXEC])
    flags = efd.fcntl(Fcntl::F_GETFL) & Fcntl::O_NONBLOCK
    assert_equal(Fcntl::O_NONBLOCK, flags)

    flags = efd.fcntl(Fcntl::F_GETFD) & Fcntl::FD_CLOEXEC
    assert_equal(Fcntl::FD_CLOEXEC, flags)
  end if defined?(EventFD::NONBLOCK) && defined?(EventFD::CLOEXEC)

  def test_new_cloexec
    efd = EventFD.new(0, EventFD::CLOEXEC)
    flags = efd.fcntl(Fcntl::F_GETFD) & Fcntl::FD_CLOEXEC
    assert_equal(Fcntl::FD_CLOEXEC, flags)
  end if defined?(EventFD::CLOEXEC)

  def test_incr_value
    efd = EventFD.new(0)
    assert_equal true, efd.incr(1)
    assert_equal 1, efd.value

    assert_nil efd.value(true)
    assert_equal true, efd.incr(9)
    assert_equal 9, efd.value(true)

    assert_equal true, efd.incr(0xfffffffffffffffe)
    assert_equal false, efd.incr(1, true)
  end

  def test_incr_value_semaphore
    efd = EventFD.new(6, :SEMAPHORE)
    6.times { assert_equal 1, efd.value }
    assert_nil efd.value(true)
    assert_equal true, efd.incr(1)
    assert_equal 1, efd.value
  end
end if defined?(SleepyPenguin::EventFD)

git clone https://yhbt.net/sleepy_penguin.git