test_tcp*read_write: use blocking kgio_accept in setup
[kgio.git] / test / lib_server_accept.rb
blob1e6bf24a5d8829b0d881f4b20187ff12a50deb61
1 require 'test/unit'
2 require 'io/nonblock'
3 $-w = true
4 require 'kgio'
6 module LibServerAccept
8   def teardown
9     @srv.close unless @srv.closed?
10     Kgio.accept_cloexec = true
11     Kgio.accept_nonblock = false
12   end
14   def test_tryaccept_success
15     a = client_connect
16     IO.select([@srv])
17     b = @srv.kgio_tryaccept
18     assert_kind_of Kgio::Socket, b
19     assert_equal @host, b.kgio_addr
20   end
22   def test_tryaccept_fail
23     assert_equal nil, @srv.kgio_tryaccept
24   end
26   def test_blocking_accept
27     t0 = Time.now
28     pid = fork { sleep 1; a = client_connect; sleep }
29     b = @srv.kgio_accept
30     elapsed = Time.now - t0
31     assert_kind_of Kgio::Socket, b
32     assert_equal @host, b.kgio_addr
33     Process.kill(:TERM, pid)
34     Process.waitpid(pid)
35     assert elapsed >= 1, "elapsed: #{elapsed}"
36   end
38   def test_blocking_accept_with_nonblock_socket
39     @srv.nonblock = true
40     t0 = Time.now
41     pid = fork { sleep 1; a = client_connect; sleep }
42     b = @srv.kgio_accept
43     elapsed = Time.now - t0
44     assert_kind_of Kgio::Socket, b
45     assert_equal @host, b.kgio_addr
46     Process.kill(:TERM, pid)
47     Process.waitpid(pid)
48     assert elapsed >= 1, "elapsed: #{elapsed}"
50     t0 = Time.now
51     pid = fork { sleep 6; a = client_connect; sleep }
52     b = @srv.kgio_accept
53     elapsed = Time.now - t0
54     assert_kind_of Kgio::Socket, b
55     assert_equal @host, b.kgio_addr
56     Process.kill(:TERM, pid)
57     Process.waitpid(pid)
58     assert elapsed >= 6, "elapsed: #{elapsed}"
60     t0 = Time.now
61     pid = fork { sleep 1; a = client_connect; sleep }
62     b = @srv.kgio_accept
63     elapsed = Time.now - t0
64     assert_kind_of Kgio::Socket, b
65     assert_equal @host, b.kgio_addr
66     Process.kill(:TERM, pid)
67     Process.waitpid(pid)
68     assert elapsed >= 1, "elapsed: #{elapsed}"
69   end
70 end