connect,tryopen: set close-on-exec flag for new fds on Ruby 2.0+
[kgio.git] / test / test_unix_connect.rb
blobb85f1f6710cafd5d869371d0ad905e8403a6d066
1 require 'test/unit'
2 require 'io/nonblock'
3 $-w = true
4 require 'kgio'
5 require 'tempfile'
7 class SubSocket < Kgio::Socket
8   attr_accessor :foo
9   def kgio_wait_writable
10     @foo = "waited"
11   end
12 end
14 class TestKgioUnixConnect < Test::Unit::TestCase
16   def setup
17     tmp = Tempfile.new('kgio_unix')
18     @path = tmp.path
19     File.unlink(@path)
20     tmp.close rescue nil
21     @srv = Kgio::UNIXServer.new(@path)
22     @addr = Socket.pack_sockaddr_un(@path)
23   end
25   def teardown
26     @srv.close unless @srv.closed?
27     File.unlink(@path)
28     Kgio.accept_cloexec = true
29   end
31   def test_unix_socket_new_invalid
32     assert_raises(ArgumentError) { Kgio::UNIXSocket.new('*' * 1024 * 1024) }
33   end
35   def test_unix_socket_new
36     sock = Kgio::UNIXSocket.new(@path)
38     sock.respond_to?(:close_on_exec?) and
39       assert_equal(RUBY_VERSION.to_f >= 2.0, sock.close_on_exec?)
41     assert_instance_of Kgio::UNIXSocket, sock
42     ready = IO.select(nil, [ sock ])
43     assert_equal sock, ready[1][0]
44     assert_equal nil, sock.kgio_write("HELLO")
45   end
47   def test_new
48     sock = Kgio::Socket.new(@addr)
50     sock.respond_to?(:close_on_exec?) and
51       assert_equal(RUBY_VERSION.to_f >= 2.0, sock.close_on_exec?)
53     assert_instance_of Kgio::Socket, sock
54     ready = IO.select(nil, [ sock ])
55     assert_equal sock, ready[1][0]
56     assert_equal nil, sock.kgio_write("HELLO")
57   end
59   def test_start
60     sock = Kgio::Socket.start(@addr)
61     assert_instance_of Kgio::Socket, sock
62     ready = IO.select(nil, [ sock ])
63     assert_equal sock, ready[1][0]
64     assert_equal nil, sock.kgio_write("HELLO")
65   end
67   def test_socket_start
68     sock = SubSocket.start(@addr)
69     assert_nil sock.foo
70     ready = IO.select(nil, [ sock ])
71     assert_equal sock, ready[1][0]
72     assert_equal nil, sock.kgio_write("HELLO")
73   end
75   def test_wait_writable_set
76     sock = SubSocket.new(@addr)
77     assert_kind_of Kgio::Socket, sock
78     assert_instance_of SubSocket, sock
79     assert_equal nil, sock.kgio_write("HELLO")
80   end
81 end