GIT-VERSION-GEN: bump version for new API
[kgio.git] / test / test_poll.rb
blob32d0a4db066223e87bf5287ba2947b4762c799a2
1 require 'test/unit'
2 $-w = true
3 require 'kgio'
5 class TestPoll < Test::Unit::TestCase
6   def teardown
7     [ @rd, @wr ].each { |io| io.close unless io.closed? }
8   end
10   def setup
11     @rd, @wr = IO.pipe
12   end
14   def test_constants
15     assert_kind_of Integer, Kgio::POLLIN
16     assert_kind_of Integer, Kgio::POLLOUT
17     assert_kind_of Integer, Kgio::POLLPRI
18     assert_kind_of Integer, Kgio::POLLHUP
19     assert_kind_of Integer, Kgio::POLLERR
20     assert_kind_of Integer, Kgio::POLLNVAL
21   end
23   def test_poll_symbol
24     set = { @rd => :wait_readable, @wr => :wait_writable }
25     res = Kgio.poll(set)
26     assert_equal({@wr => Kgio::POLLOUT}, res)
27     assert_equal set.object_id, res.object_id
28   end
30   def test_poll_integer
31     set = { @wr => Kgio::POLLOUT|Kgio::POLLHUP }
32     res = Kgio.poll(set)
33     assert_equal({@wr => Kgio::POLLOUT}, res)
34     assert_equal set.object_id, res.object_id
35   end
37   def test_poll_timeout
38     t0 = Time.now
39     res = Kgio.poll({}, 10)
40     diff = Time.now - t0
41     assert diff >= 0.010, "diff=#{diff}"
42     assert_nil res
43   end
45   def test_poll_close
46     foo = nil
47     thr = Thread.new { sleep 0.100; @wr.close }
48     t0 = Time.now
49     res = Kgio.poll({@rd => Kgio::POLLIN})
50     diff = Time.now - t0
51     thr.join
52     assert_equal([ @rd ], res.keys)
53     assert diff >= 0.010, "diff=#{diff}"
54   end
56   def test_poll_EINTR
57     ok = false
58     orig = trap(:USR1) { ok = true }
59     thr = Thread.new do
60       sleep 0.100
61       Process.kill(:USR1, $$)
62     end
63     t0 = Time.now
64     res = Kgio.poll({@rd => Kgio::POLLIN}, 1000)
65     diff = Time.now - t0
66     thr.join
67     assert_nil res
68     assert diff >= 1.0, "diff=#{diff}"
69     assert ok
70     ensure
71       trap(:USR1, orig)
72   end
74   def test_poll_EINTR_changed
75     ok = false
76     orig = trap(:USR1) { ok = true }
77     pollset = { @rd => Kgio::POLLIN }
78     thr = Thread.new do
79       sleep 0.100
80       pollset[@wr] = Kgio::POLLOUT
81       Process.kill(:USR1, $$)
82     end
83     t0 = Time.now
84     res = Kgio.poll(pollset, 1000)
85     diff = Time.now - t0
86     thr.join
87     assert_equal({@wr => Kgio::POLLOUT}, res)
88     assert diff < 1.0, "diff=#{diff}"
89     assert ok
90     ensure
91       trap(:USR1, orig)
92   end
94   def test_poll_signal_torture
95     usr1 = 0
96     empty = 0
97     nr = 100
98     set = { @rd => Kgio::POLLIN }
99     trap(:USR1) { usr1 += 1 }
100     pid = fork do
101       trap(:USR1, "DEFAULT")
102       sleep 0.1
103       ppid = Process.ppid
104       nr.times { Process.kill(:USR1, ppid); sleep 0.05 }
105       @wr.syswrite('.')
106       exit!(0)
107     end
109     assert_nothing_raised do
110       empty += 1 until Kgio.poll(set.dup, 100)
111     end
112     _, status = Process.waitpid2(pid)
113     assert status.success?, status.inspect
114     assert usr1 > 0, "usr1: #{usr1}"
115     rescue Object => err
116       p [ :err, err ]
117     ensure
118       trap(:USR1, "DEFAULT")
119   end
120 end if Kgio.respond_to?(:poll)