Create own directory for every unix socket in unit tests
[kgio.git] / test / lib_server_accept.rb
blobdb0d120b1d661a5fa10eff174b1b692309e85933
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   end
25   def test_tryaccept_flags
26     a = client_connect
27     IO.select([@srv])
28     b = @srv.kgio_tryaccept nil, 0
29     assert_kind_of Kgio::Socket, b
30     assert_equal 0, b.fcntl(Fcntl::F_GETFD)
31   end
33   def test_blocking_accept_flags
34     a = client_connect
35     IO.select([@srv])
36     b = @srv.kgio_accept nil, 0
37     assert_kind_of Kgio::Socket, b
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(:KILL, 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(:KILL, 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(:KILL, 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(:KILL, pid)
86     Process.waitpid(pid)
87     assert elapsed >= 1, "elapsed: #{elapsed}"
88   end
89 end