From c22f2b5ebccbca8e04aa22821964f67c4a81c675 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 6 Jul 2011 19:11:47 -0700 Subject: export SOCK_NONBLOCK, SOCK_CLOEXEC constants in Kgio It's more reliable than relying on IO::NONBLOCK and Fcntl::FD_CLOEXEC constants. The existing constants are not guaranteed to be equivalent to what accept4() takes even though the current Linux implementation does it this way. --- test/test_accept_flags.rb | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 test/test_accept_flags.rb (limited to 'test') diff --git a/test/test_accept_flags.rb b/test/test_accept_flags.rb new file mode 100644 index 0000000..3aae216 --- /dev/null +++ b/test/test_accept_flags.rb @@ -0,0 +1,49 @@ +require 'test/unit' +require 'fcntl' +require 'io/nonblock' +$-w = true +require 'kgio' + +class TestAcceptFlags < Test::Unit::TestCase + def test_accept_flags + @host = ENV["TEST_HOST"] || '127.0.0.1' + @srv = Kgio::TCPServer.new(@host, 0) + @port = @srv.addr[1] + + client = TCPSocket.new(@host, @port) + accepted = @srv.kgio_accept(nil, Kgio::SOCK_NONBLOCK) + assert_instance_of Kgio::Socket, accepted + assert accepted.nonblock? + flags = accepted.fcntl(Fcntl::F_GETFD) + assert_equal 0, flags & Fcntl::FD_CLOEXEC + assert_nil client.close + assert_nil accepted.close + + client = TCPSocket.new(@host, @port) + accepted = @srv.kgio_accept(nil, Kgio::SOCK_CLOEXEC) + assert_instance_of Kgio::Socket, accepted + assert ! accepted.nonblock? + flags = accepted.fcntl(Fcntl::F_GETFD) + assert_equal Fcntl::FD_CLOEXEC, flags & Fcntl::FD_CLOEXEC + assert_nil client.close + assert_nil accepted.close + + client = TCPSocket.new(@host, @port) + accepted = @srv.kgio_accept(nil, Kgio::SOCK_CLOEXEC|Kgio::SOCK_NONBLOCK) + assert_instance_of Kgio::Socket, accepted + assert accepted.nonblock? + flags = accepted.fcntl(Fcntl::F_GETFD) + assert_equal Fcntl::FD_CLOEXEC, flags & Fcntl::FD_CLOEXEC + assert_nil client.close + assert_nil accepted.close + + client = TCPSocket.new(@host, @port) + accepted = @srv.kgio_accept(nil, Kgio::SOCK_CLOEXEC|Kgio::SOCK_NONBLOCK) + assert_instance_of Kgio::Socket, accepted + assert accepted.nonblock? + flags = accepted.fcntl(Fcntl::F_GETFD) + assert_equal Fcntl::FD_CLOEXEC, flags & Fcntl::FD_CLOEXEC + assert_nil client.close + assert_nil accepted.close + end +end -- cgit v1.2.3-24-ge0c7