test_syssend: avoid warning on cleanup
[kgio.git] / test / test_unix_connect.rb
blob7b19941c52e2ff5e436d683fdd5a6ac2a05b9944
1 require 'test/unit'
2 require 'io/nonblock'
3 $-w = true
4 require 'kgio'
5 require 'tempfile'
6 require 'tmpdir'
8 class SubSocket < Kgio::Socket
9   attr_accessor :foo
10   def kgio_wait_writable
11     @foo = "waited"
12   end
13 end
15 class TestKgioUnixConnect < Test::Unit::TestCase
17   def setup
18     @tmpdir = Dir.mktmpdir('kgio_unix_1')
19     tmp = Tempfile.new('kgio_unix_1', @tmpdir)
20     @path = tmp.path
21     tmp.close!
22     @srv = Kgio::UNIXServer.new(@path)
23     @addr = Socket.pack_sockaddr_un(@path)
24   end
26   def teardown
27     @srv.close unless @srv.closed?
28     File.unlink(@path)
29     FileUtils.remove_entry_secure(@tmpdir)
30     Kgio.accept_cloexec = true
31   end
33   def test_unix_socket_new_invalid
34     assert_raises(ArgumentError) { Kgio::UNIXSocket.new('*' * 1024 * 1024) }
35   end
37   def test_unix_socket_new
38     sock = Kgio::UNIXSocket.new(@path)
40     sock.respond_to?(:close_on_exec?) and
41       assert_equal(RUBY_VERSION.to_f >= 2.0, sock.close_on_exec?)
43     assert_instance_of Kgio::UNIXSocket, sock
44     ready = IO.select(nil, [ sock ])
45     assert_equal sock, ready[1][0]
46     assert_equal nil, sock.kgio_write("HELLO")
47   end
49   def test_new
50     sock = Kgio::Socket.new(@addr)
52     sock.respond_to?(:close_on_exec?) and
53       assert_equal(RUBY_VERSION.to_f >= 2.0, sock.close_on_exec?)
55     assert_instance_of Kgio::Socket, sock
56     ready = IO.select(nil, [ sock ])
57     assert_equal sock, ready[1][0]
58     assert_equal nil, sock.kgio_write("HELLO")
59   end
61   def test_start
62     sock = Kgio::Socket.start(@addr)
63     assert_instance_of Kgio::Socket, sock
64     ready = IO.select(nil, [ sock ])
65     assert_equal sock, ready[1][0]
66     assert_equal nil, sock.kgio_write("HELLO")
67   end
69   def test_socket_start
70     sock = SubSocket.start(@addr)
71     assert_nil sock.foo
72     ready = IO.select(nil, [ sock ])
73     assert_equal sock, ready[1][0]
74     assert_equal nil, sock.kgio_write("HELLO")
75   end
77   def test_wait_writable_set
78     sock = SubSocket.new(@addr)
79     assert_kind_of Kgio::Socket, sock
80     assert_instance_of SubSocket, sock
81     assert_equal nil, sock.kgio_write("HELLO")
82   end
83 end