drop remaining 1.8 and fragile autopush code paths
[kgio.git] / test / lib_server_accept.rb
blobfcf9c877119ca80c22445c8a2e946cc279501815
1 require 'test/unit'
2 require 'fcntl'
3 require 'io/nonblock'
4 require 'fileutils'
5 $-w = true
6 require 'kgio'
8 module LibServerAccept
10   def teardown
11     @srv.close unless @srv.closed?
12     FileUtils.remove_entry_secure(@tmpdir) if defined?(@tmpdir)
13     Kgio.accept_cloexec = true
14     Kgio.accept_nonblock = false
15   end
17   def test_tryaccept_success
18     a = client_connect
19     IO.select([@srv])
20     b = @srv.kgio_tryaccept
21     assert_kind_of Kgio::Socket, b
22     assert_equal @host, b.kgio_addr
23     a.close
24   end
26   def test_tryaccept_flags
27     a = client_connect
28     IO.select([@srv])
29     b = @srv.kgio_tryaccept nil, 0
30     assert_kind_of Kgio::Socket, b
31     assert_equal 0, b.fcntl(Fcntl::F_GETFD)
32     a.close
33   end
35   def test_blocking_accept_flags
36     a = client_connect
37     IO.select([@srv])
38     b = @srv.kgio_accept nil, 0
39     assert_kind_of Kgio::Socket, b
40     assert_equal 0, b.fcntl(Fcntl::F_GETFD)
41     a.close
42   end
44   def test_tryaccept_fail
45     assert_equal nil, @srv.kgio_tryaccept
46   end
48   def test_blocking_accept
49     t0 = Time.now
50     pid = fork { sleep 1; a = client_connect; sleep; a.close }
51     b = @srv.kgio_accept
52     elapsed = Time.now - t0
53     assert_kind_of Kgio::Socket, b
54     assert_equal @host, b.kgio_addr
55     Process.kill(:KILL, pid)
56     Process.waitpid(pid)
57     assert elapsed >= 1, "elapsed: #{elapsed}"
58   end
60   def test_blocking_accept_with_nonblock_socket
61     @srv.nonblock = true
62     t0 = Time.now
63     pid = fork { sleep 1; a = client_connect; sleep; a.close }
64     b = @srv.kgio_accept
65     elapsed = Time.now - t0
66     assert_kind_of Kgio::Socket, b
67     assert_equal @host, b.kgio_addr
68     Process.kill(:KILL, pid)
69     Process.waitpid(pid)
70     assert elapsed >= 1, "elapsed: #{elapsed}"
72     t0 = Time.now
73     pid = fork { sleep 6; a = client_connect; sleep; a.close }
74     b = @srv.kgio_accept
75     elapsed = Time.now - t0
76     assert_kind_of Kgio::Socket, b
77     assert_equal @host, b.kgio_addr
78     Process.kill(:KILL, pid)
79     Process.waitpid(pid)
80     assert elapsed >= 6, "elapsed: #{elapsed}"
82     t0 = Time.now
83     pid = fork { sleep 1; a = client_connect; sleep; a.close }
84     b = @srv.kgio_accept
85     elapsed = Time.now - t0
86     assert_kind_of Kgio::Socket, b
87     assert_equal @host, b.kgio_addr
88     Process.kill(:KILL, pid)
89     Process.waitpid(pid)
90     assert elapsed >= 1, "elapsed: #{elapsed}"
91   end
92 end