diff options
author | Eric Wong <normalperson@yhbt.net> | 2011-02-08 10:41:51 +0000 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2011-02-08 13:28:33 -0800 |
commit | 5c480aee3067006b5da6d45b7de41d8401b70848 (patch) | |
tree | eebb5907063b3ff442fc97abee8d6336db2636b5 /test/test_poll.rb | |
parent | 47653194bf6ad53b9f5fca1b266c30855df5ebbd (diff) | |
download | kgio-5c480aee3067006b5da6d45b7de41d8401b70848.tar.gz |
It's a nice alternative to IO.select for higher-numbered file descriptors, especially sparse ones. Our interface also generates less garbage than IO.select does.
Diffstat (limited to 'test/test_poll.rb')
-rw-r--r-- | test/test_poll.rb | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/test/test_poll.rb b/test/test_poll.rb new file mode 100644 index 0000000..8cade91 --- /dev/null +++ b/test/test_poll.rb @@ -0,0 +1,68 @@ +require 'test/unit' +$-w = true +require 'kgio' + +class TestPoll < Test::Unit::TestCase + def teardown + [ @rd, @wr ].each { |io| io.close unless io.closed? } + end + + def setup + @rd, @wr = IO.pipe + end + + def test_constants + assert_kind_of Integer, Kgio::POLLIN + assert_kind_of Integer, Kgio::POLLOUT + assert_kind_of Integer, Kgio::POLLPRI + assert_kind_of Integer, Kgio::POLLHUP + assert_kind_of Integer, Kgio::POLLERR + assert_kind_of Integer, Kgio::POLLNVAL + end + + def test_poll_symbol + set = { @rd => :wait_readable, @wr => :wait_writable } + res = Kgio.poll(set) + assert_equal({@wr => Kgio::POLLOUT}, res) + assert_equal set.object_id, res.object_id + end + + def test_poll_integer + set = { @wr => Kgio::POLLOUT|Kgio::POLLHUP } + res = Kgio.poll(set) + assert_equal({@wr => Kgio::POLLOUT}, res) + assert_equal set.object_id, res.object_id + end + + def test_poll_timeout + t0 = Time.now + res = Kgio.poll({}, 10) + diff = Time.now - t0 + assert diff >= 0.010, "diff=#{diff}" + assert_nil res + end + + def test_poll_interrupt + foo = nil + oldquit = trap(:QUIT) { foo = :bar } + thr = Thread.new { sleep 0.100; Process.kill(:QUIT, $$) } + t0 = Time.now + assert_raises(Errno::EINTR) { Kgio.poll({}) } + diff = Time.now - t0 + thr.join + assert diff >= 0.010, "diff=#{diff}" + ensure + trap(:QUIT, "DEFAULT") + end + + def test_poll_close + foo = nil + thr = Thread.new { sleep 0.100; @wr.close } + t0 = Time.now + res = Kgio.poll({@rd => Kgio::POLLIN}) + diff = Time.now - t0 + thr.join + assert_equal([ @rd ], res.keys) + assert diff >= 0.010, "diff=#{diff}" + end +end if Kgio.respond_to?(:poll) |