pkg.mk: update to the latest version
[kgio.git] / test / test_tcp_connect.rb
blob9756407282d90cd5b55e607bcfd755f0f0658be8
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")
34   end
36   def test_start
37     sock = Kgio::Socket.start(@addr)
38     assert_kind_of Kgio::Socket, sock
39     ready = IO.select(nil, [ sock ])
40     assert_equal sock, ready[1][0]
41     assert_equal nil, sock.kgio_write("HELLO")
42   end
44   def test_tcp_socket_new_invalid
45     assert_raises(ArgumentError) { Kgio::TCPSocket.new('example.com', 80) }
46     assert_raises(ArgumentError) { Kgio::TCPSocket.new('999.999.999.999', 80) }
47     assert_raises(TypeError) { Kgio::TCPSocket.new("127.0.0.1", "http") }
48     assert_raises(TypeError) { Kgio::TCPSocket.new('example.com', "http") }
49   end
51   def test_tcp_socket_new
52     sock = Kgio::TCPSocket.new(@host, @port)
53     assert_instance_of Kgio::TCPSocket, 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_socket_start
60     sock = SubSocket.start(@addr)
61     assert_nil sock.foo
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_wait_writable_set
68     sock = SubSocket.new(@addr)
69     assert_equal "waited", sock.foo if RUBY_PLATFORM =~ /linux/
70     assert_equal nil, sock.kgio_write("HELLO")
71   end
72 end