reinstate the original (and dangerous) autopush in C
[kgio.git] / test / test_poll.rb
blob6463ef9fef95a0bbbd67ceb5057d591b2e4fc5dd
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_signal_close
57     orig = trap(:USR1) { @rd.close }
58     res = nil
59     thr = Thread.new { sleep 0.100; Process.kill(:USR1, $$) }
60     t0 = Time.now
61     assert_raises(IOError) do
62       result = Kgio.poll({@rd => Kgio::POLLIN})
63       result.each_key { |io| io.read_nonblock(1) }
64     end
65     diff = Time.now - t0
66     thr.join
67     assert diff >= 0.010, "diff=#{diff}"
68     ensure
69       trap(:USR1, orig)
70   end
72   def test_poll_EINTR
73     ok = false
74     orig = trap(:USR1) { ok = true }
75     thr = Thread.new do
76       sleep 0.100
77       Process.kill(:USR1, $$)
78     end
79     t0 = Time.now
80     res = Kgio.poll({@rd => Kgio::POLLIN}, 1000)
81     diff = Time.now - t0
82     thr.join
83     assert_nil res
84     assert diff >= 1.0, "diff=#{diff}"
85     assert ok
86     ensure
87       trap(:USR1, orig)
88   end
90   def test_poll_signal_torture
91     usr1 = 0
92     empty = 0
93     nr = 100
94     set = { @rd => Kgio::POLLIN }
95     orig = trap(:USR1) { usr1 += 1 }
96     pid = fork do
97       trap(:USR1, "DEFAULT")
98       sleep 0.1
99       ppid = Process.ppid
100       nr.times { Process.kill(:USR1, ppid); sleep 0.05 }
101       @wr.syswrite('.')
102       exit!(0)
103     end
105     empty += 1 until Kgio.poll(set.dup, 100)
106     _, status = Process.waitpid2(pid)
107     assert status.success?, status.inspect
108     assert usr1 > 0, "usr1: #{usr1}"
109   ensure
110     trap(:USR1, orig)
111   end unless RUBY_PLATFORM =~ /kfreebsd-gnu/
112 end if Kgio.respond_to?(:poll)