sleepy_penguin.git  about / heads / tags
Linux I/O events for Ruby
blob a5198bf972a9a1607ced1e8a59393efa50daac65 1839 bytes (raw)
$ git show HEAD:test/test_timerfd.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
65
66
 
require_relative 'helper'
require 'fcntl'

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

  def test_constants
    assert_kind_of Integer, TimerFD::REALTIME
    assert_kind_of Integer, TimerFD::MONOTONIC
  end

  def test_create
    tfd = TimerFD.new
    assert_kind_of(IO, tfd)
    check_cloexec(tfd)
  end

  def test_create_nonblock
    tfd = TimerFD.new(TimerFD::REALTIME, TimerFD::NONBLOCK)
    flags = tfd.fcntl(Fcntl::F_GETFL) & Fcntl::O_NONBLOCK
    assert_equal(Fcntl::O_NONBLOCK, flags)
  end if defined?(TimerFD::NONBLOCK)

  def test_create_nonblock_sym
    tfd = TimerFD.new(:REALTIME, :NONBLOCK)
    flags = tfd.fcntl(Fcntl::F_GETFL) & Fcntl::O_NONBLOCK
    assert_equal(Fcntl::O_NONBLOCK, flags)
  end if defined?(TimerFD::NONBLOCK)

  def test_create_cloexec
    tfd = TimerFD.new(TimerFD::REALTIME, TimerFD::CLOEXEC)
    flags = tfd.fcntl(Fcntl::F_GETFD) & Fcntl::FD_CLOEXEC
    assert_equal(Fcntl::FD_CLOEXEC, flags)
  end if defined?(TimerFD::CLOEXEC)

  def test_settime
    tfd = TimerFD.new(TimerFD::REALTIME)
    assert_equal([0, 0], tfd.settime(TimerFD::ABSTIME, 0, 0.01))
    sleep 0.01
    assert_equal 1, tfd.expirations
  end

  def test_settime_symbol
    tfd = TimerFD.new(:REALTIME)
    assert_equal([0, 0], tfd.settime(:ABSTIME, 0, 0.01))
    sleep 0.01
    assert_equal 1, tfd.expirations
  end

  def test_gettime
    tfd = TimerFD.new :REALTIME
    now = Time.now.to_i
    assert_equal([0, 0], tfd.settime(nil, 0, now + 5))
    interval, value = tfd.gettime
    assert_equal 0, interval
    assert_in_delta now + 5, value, 0.01
  end

  def test_expirations_nonblock
    tfd = TimerFD.new(:MONOTONIC)
    assert_equal([0, 0], tfd.settime(0, 0, 0.05))
    assert_nil tfd.expirations(true)
    sleep 0.05
    assert_equal 1, tfd.expirations
  end
end if defined?(SleepyPenguin::TimerFD)

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