reinstate the original (and dangerous) autopush in C
[kgio.git] / test / test_accept_class.rb
blob0e1d17251c7a591b7cdf9438890d5667477e7c5e
1 require 'test/unit'
2 require 'io/nonblock'
3 $-w = true
4 require 'kgio'
6 class TestAcceptClass < Test::Unit::TestCase
7   class FooSocket < Kgio::Socket
8   end
10   def setup
11     assert_equal Kgio::Socket, Kgio.accept_class
12   end
14   def teardown
15     Kgio.accept_class = nil
16     assert_equal Kgio::Socket, Kgio.accept_class
17   end
19   def test_tcp_socket
20     Kgio.accept_class = Kgio::TCPSocket
21     assert_equal Kgio::TCPSocket, Kgio.accept_class
22   end
24   def test_invalid
25     assert_raises(TypeError) { Kgio.accept_class = TCPSocket }
26     assert_equal Kgio::Socket, Kgio.accept_class
27   end
29   def test_accepted_class
30     @host = ENV["TEST_HOST"] || '127.0.0.1'
31     @srv = Kgio::TCPServer.new(@host, 0)
32     @port = @srv.addr[1]
34     Kgio.accept_class = Kgio::TCPSocket
35     client = TCPSocket.new(@host, @port)
36     assert_instance_of Kgio::TCPSocket, @srv.kgio_accept
37     client = TCPSocket.new(@host, @port)
38     IO.select([@srv])
39     assert_instance_of Kgio::TCPSocket, @srv.kgio_tryaccept
41     Kgio.accept_class = nil
42     client = TCPSocket.new(@host, @port)
43     assert_instance_of Kgio::Socket, @srv.kgio_accept
44     client = TCPSocket.new(@host, @port)
45     IO.select([@srv])
46     assert_instance_of Kgio::Socket, @srv.kgio_tryaccept
48     Kgio.accept_class = Kgio::UNIXSocket
49     client = TCPSocket.new(@host, @port)
50     assert_instance_of Kgio::UNIXSocket, @srv.kgio_accept
51     client = TCPSocket.new(@host, @port)
52     IO.select([@srv])
53     assert_instance_of Kgio::UNIXSocket, @srv.kgio_tryaccept
55     client = TCPSocket.new(@host, @port)
56     assert_instance_of FooSocket, @srv.kgio_accept(FooSocket)
58     client = TCPSocket.new(@host, @port)
59     IO.select([@srv])
60     assert_instance_of FooSocket, @srv.kgio_tryaccept(FooSocket)
61   end
62 end