about summary refs log tree commit homepage
path: root/test/test_accept_class.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_accept_class.rb')
-rw-r--r--test/test_accept_class.rb52
1 files changed, 52 insertions, 0 deletions
diff --git a/test/test_accept_class.rb b/test/test_accept_class.rb
new file mode 100644
index 0000000..3b5d343
--- /dev/null
+++ b/test/test_accept_class.rb
@@ -0,0 +1,52 @@
+require 'test/unit'
+require 'io/nonblock'
+$-w = true
+require 'kgio'
+
+class TestAcceptClass < Test::Unit::TestCase
+  def setup
+    assert_equal Kgio::Socket, Kgio.accept_class
+  end
+
+  def teardown
+    assert_nothing_raised { Kgio.accept_class = nil }
+    assert_equal Kgio::Socket, Kgio.accept_class
+  end
+
+  def test_tcp_socket
+    assert_nothing_raised { Kgio.accept_class = Kgio::TCPSocket }
+    assert_equal Kgio::TCPSocket, Kgio.accept_class
+  end
+
+  def test_invalid
+    assert_raises(TypeError) { Kgio.accept_class = TCPSocket }
+    assert_equal Kgio::Socket, Kgio.accept_class
+  end
+
+  def test_accepted_class
+    @host = ENV["TEST_HOST"] || '127.0.0.1'
+    @srv = Kgio::TCPServer.new(@host, 0)
+    @port = @srv.addr[1]
+
+    assert_nothing_raised { Kgio.accept_class = Kgio::TCPSocket }
+    client = TCPSocket.new(@host, @port)
+    assert_instance_of Kgio::TCPSocket, @srv.kgio_accept
+    client = TCPSocket.new(@host, @port)
+    IO.select([@srv])
+    assert_instance_of Kgio::TCPSocket, @srv.kgio_tryaccept
+
+    assert_nothing_raised { Kgio.accept_class = nil }
+    client = TCPSocket.new(@host, @port)
+    assert_instance_of Kgio::Socket, @srv.kgio_accept
+    client = TCPSocket.new(@host, @port)
+    IO.select([@srv])
+    assert_instance_of Kgio::Socket, @srv.kgio_tryaccept
+
+    assert_nothing_raised { Kgio.accept_class = Kgio::UNIXSocket }
+    client = TCPSocket.new(@host, @port)
+    assert_instance_of Kgio::UNIXSocket, @srv.kgio_accept
+    client = TCPSocket.new(@host, @port)
+    IO.select([@srv])
+    assert_instance_of Kgio::UNIXSocket, @srv.kgio_tryaccept
+  end
+end