HACKING: updates for wrongdoc vs rdoc
[kgio.git] / test / test_poll.rb
blob8cade9133e9f098404c160f92ce53c9047518ae7
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_interrupt
46     foo = nil
47     oldquit = trap(:QUIT) { foo = :bar }
48     thr = Thread.new { sleep 0.100; Process.kill(:QUIT, $$) }
49     t0 = Time.now
50     assert_raises(Errno::EINTR) { Kgio.poll({}) }
51     diff = Time.now - t0
52     thr.join
53     assert diff >= 0.010, "diff=#{diff}"
54     ensure
55       trap(:QUIT, "DEFAULT")
56   end
58   def test_poll_close
59     foo = nil
60     thr = Thread.new { sleep 0.100; @wr.close }
61     t0 = Time.now
62     res = Kgio.poll({@rd => Kgio::POLLIN})
63     diff = Time.now - t0
64     thr.join
65     assert_equal([ @rd ], res.keys)
66     assert diff >= 0.010, "diff=#{diff}"
67   end
68 end if Kgio.respond_to?(:poll)