read_write: call rb_str_modify() before rb_str_resize()
[kgio.git] / test / lib_server_accept.rb
blob6ea461b5af0d6f12d9b9bcd501f4b8d0f48f1171
1 require 'test/unit'
2 require 'fcntl'
3 require 'io/nonblock'
4 $-w = true
5 require 'kgio'
7 module LibServerAccept
9   def teardown
10     @srv.close unless @srv.closed?
11     Kgio.accept_cloexec = true
12     Kgio.accept_nonblock = false
13   end
15   def test_tryaccept_success
16     a = client_connect
17     IO.select([@srv])
18     b = @srv.kgio_tryaccept
19     assert_kind_of Kgio::Socket, b
20     assert_equal @host, b.kgio_addr
21   end
23   def test_tryaccept_flags
24     a = client_connect
25     IO.select([@srv])
26     b = @srv.kgio_tryaccept nil, 0
27     assert_kind_of Kgio::Socket, b
28     assert_equal false, b.nonblock?
29     assert_equal 0, b.fcntl(Fcntl::F_GETFD)
30   end
32   def test_blocking_accept_flags
33     a = client_connect
34     IO.select([@srv])
35     b = @srv.kgio_accept nil, 0
36     assert_kind_of Kgio::Socket, b
37     assert_equal false, b.nonblock?
38     assert_equal 0, b.fcntl(Fcntl::F_GETFD)
39   end
41   def test_tryaccept_fail
42     assert_equal nil, @srv.kgio_tryaccept
43   end
45   def test_blocking_accept
46     t0 = Time.now
47     pid = fork { sleep 1; a = client_connect; sleep }
48     b = @srv.kgio_accept
49     elapsed = Time.now - t0
50     assert_kind_of Kgio::Socket, b
51     assert_equal @host, b.kgio_addr
52     Process.kill(:TERM, pid)
53     Process.waitpid(pid)
54     assert elapsed >= 1, "elapsed: #{elapsed}"
55   end
57   def test_blocking_accept_with_nonblock_socket
58     @srv.nonblock = true
59     t0 = Time.now
60     pid = fork { sleep 1; a = client_connect; sleep }
61     b = @srv.kgio_accept
62     elapsed = Time.now - t0
63     assert_kind_of Kgio::Socket, b
64     assert_equal @host, b.kgio_addr
65     Process.kill(:TERM, pid)
66     Process.waitpid(pid)
67     assert elapsed >= 1, "elapsed: #{elapsed}"
69     t0 = Time.now
70     pid = fork { sleep 6; a = client_connect; sleep }
71     b = @srv.kgio_accept
72     elapsed = Time.now - t0
73     assert_kind_of Kgio::Socket, b
74     assert_equal @host, b.kgio_addr
75     Process.kill(:TERM, pid)
76     Process.waitpid(pid)
77     assert elapsed >= 6, "elapsed: #{elapsed}"
79     t0 = Time.now
80     pid = fork { sleep 1; a = client_connect; sleep }
81     b = @srv.kgio_accept
82     elapsed = Time.now - t0
83     assert_kind_of Kgio::Socket, b
84     assert_equal @host, b.kgio_addr
85     Process.kill(:TERM, pid)
86     Process.waitpid(pid)
87     assert elapsed >= 1, "elapsed: #{elapsed}"
88   end
89 end