Revert "epoll: avoid EPOLL_CTL_MOD race condition"
[sleepy_penguin.git] / test / test_timerfd.rb
blob7e102c48403b031af659ef0c75548c5db193cb62
1 require 'test/unit'
2 require 'fcntl'
3 $-w = true
5 require 'sleepy_penguin'
7 class TestTimerFD < Test::Unit::TestCase
8   include SleepyPenguin
10   def test_constants
11     assert_kind_of Integer, TimerFD::REALTIME
12     assert_kind_of Integer, TimerFD::MONOTONIC
13   end
15   def test_create
16     tfd = TimerFD.new
17     assert_kind_of(IO, tfd)
18   end
20   def test_create_nonblock
21     tfd = TimerFD.new(TimerFD::REALTIME, TimerFD::NONBLOCK)
22     flags = tfd.fcntl(Fcntl::F_GETFL) & Fcntl::O_NONBLOCK
23     assert_equal(Fcntl::O_NONBLOCK, flags)
24   end if defined?(TimerFD::NONBLOCK)
26   def test_create_nonblock_sym
27     tfd = TimerFD.new(:REALTIME, :NONBLOCK)
28     flags = tfd.fcntl(Fcntl::F_GETFL) & Fcntl::O_NONBLOCK
29     assert_equal(Fcntl::O_NONBLOCK, flags)
30   end if defined?(TimerFD::NONBLOCK)
32   def test_create_cloexec
33     tfd = TimerFD.new(TimerFD::REALTIME, TimerFD::CLOEXEC)
34     flags = tfd.fcntl(Fcntl::F_GETFD) & Fcntl::FD_CLOEXEC
35     assert_equal(Fcntl::FD_CLOEXEC, flags)
36   end if defined?(TimerFD::CLOEXEC)
38   def test_settime
39     tfd = TimerFD.new(TimerFD::REALTIME)
40     assert_equal([0, 0], tfd.settime(TimerFD::ABSTIME, 0, 0.01))
41     sleep 0.01
42     assert_equal 1, tfd.expirations
43   end
45   def test_settime_symbol
46     tfd = TimerFD.new(:REALTIME)
47     assert_equal([0, 0], tfd.settime(:ABSTIME, 0, 0.01))
48     sleep 0.01
49     assert_equal 1, tfd.expirations
50   end
52   def test_gettime
53     tfd = TimerFD.new :REALTIME
54     now = Time.now.to_i
55     assert_equal([0, 0], tfd.settime(nil, 0, now + 5))
56     interval, value = tfd.gettime
57     assert_equal 0, interval
58     assert_in_delta now + 5, value, 0.01
59   end
61   def test_expirations_nonblock
62     tfd = TimerFD.new(:MONOTONIC)
63     assert_equal([0, 0], tfd.settime(0, 0, 0.01))
64     assert_nil tfd.expirations(true)
65     sleep 0.01
66     assert_equal 1, tfd.expirations
67   end
68 end if defined?(SleepyPenguin::TimerFD)