test_poll: preserve original trap(:USR1) handler
[kgio.git] / test / test_tcp_connect.rb
blob2cf1b86114ac372f1815470577a35674dd5d073f
1 require 'test/unit'
2 require 'io/nonblock'
3 $-w = true
4 require 'kgio'
6 class SubSocket < Kgio::Socket
7   attr_accessor :foo
8   def kgio_wait_writable
9     @foo = "waited"
10   end
11 end
13 class TestKgioTcpConnect < Test::Unit::TestCase
15   def setup
16     @host = ENV["TEST_HOST"] || '127.0.0.1'
17     @srv = Kgio::TCPServer.new(@host, 0)
18     @port = @srv.addr[1]
19     @addr = Socket.pack_sockaddr_in(@port, @host)
20   end
22   def teardown
23     @srv.close unless @srv.closed?
24     Kgio.accept_cloexec = true
25     Kgio.accept_nonblock = false
26   end
28   def test_new
29     sock = Kgio::Socket.new(@addr)
30     assert_kind_of Kgio::Socket, sock
31     ready = IO.select(nil, [ sock ])
32     assert_equal sock, ready[1][0]
33     assert_equal nil, sock.kgio_write("HELLO")
35     sock.respond_to?(:close_on_exec?) and
36       assert_equal(RUBY_VERSION.to_f >= 2.0, sock.close_on_exec?)
37   end
39   def test_start
40     sock = Kgio::Socket.start(@addr)
42     sock.respond_to?(:close_on_exec?) and
43       assert_equal(RUBY_VERSION.to_f >= 2.0, sock.close_on_exec?)
45     assert_kind_of Kgio::Socket, sock
46     ready = IO.select(nil, [ sock ])
47     assert_equal sock, ready[1][0]
48     assert_equal nil, sock.kgio_write("HELLO")
49   end
51   def test_tcp_socket_new_invalid
52     assert_raises(ArgumentError) { Kgio::TCPSocket.new('example.com', 80) }
53     assert_raises(ArgumentError) { Kgio::TCPSocket.new('999.999.999.999', 80) }
54     assert_raises(TypeError) { Kgio::TCPSocket.new("127.0.0.1", "http") }
55     assert_raises(TypeError) { Kgio::TCPSocket.new('example.com', "http") }
56   end
58   def test_tcp_socket_new
59     sock = Kgio::TCPSocket.new(@host, @port)
61     sock.respond_to?(:close_on_exec?) and
62       assert_equal(RUBY_VERSION.to_f >= 2.0, sock.close_on_exec?)
64     assert_instance_of Kgio::TCPSocket, sock
65     ready = IO.select(nil, [ sock ])
66     assert_equal sock, ready[1][0]
67     assert_equal nil, sock.kgio_write("HELLO")
68   end
70   def test_socket_start
71     sock = SubSocket.start(@addr)
72     assert_nil sock.foo
73     ready = IO.select(nil, [ sock ])
74     assert_equal sock, ready[1][0]
75     assert_equal nil, sock.kgio_write("HELLO")
76   end
78   def test_wait_writable_set
79     sock = SubSocket.new(@addr)
80     assert_equal "waited", sock.foo if RUBY_PLATFORM =~ /linux/
81     IO.select(nil, [sock]) if RUBY_PLATFORM !~ /linux/
82     assert_equal nil, sock.kgio_write("HELLO")
83   end
84 end