kgio.git  about / heads / tags
kinder, gentler I/O for Ruby
blob fcf9c877119ca80c22445c8a2e946cc279501815 2337 bytes (raw)
$ git show HEAD:test/lib_server_accept.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
 
require 'test/unit'
require 'fcntl'
require 'io/nonblock'
require 'fileutils'
$-w = true
require 'kgio'

module LibServerAccept

  def teardown
    @srv.close unless @srv.closed?
    FileUtils.remove_entry_secure(@tmpdir) if defined?(@tmpdir)
    Kgio.accept_cloexec = true
    Kgio.accept_nonblock = false
  end

  def test_tryaccept_success
    a = client_connect
    IO.select([@srv])
    b = @srv.kgio_tryaccept
    assert_kind_of Kgio::Socket, b
    assert_equal @host, b.kgio_addr
    a.close
  end

  def test_tryaccept_flags
    a = client_connect
    IO.select([@srv])
    b = @srv.kgio_tryaccept nil, 0
    assert_kind_of Kgio::Socket, b
    assert_equal 0, b.fcntl(Fcntl::F_GETFD)
    a.close
  end

  def test_blocking_accept_flags
    a = client_connect
    IO.select([@srv])
    b = @srv.kgio_accept nil, 0
    assert_kind_of Kgio::Socket, b
    assert_equal 0, b.fcntl(Fcntl::F_GETFD)
    a.close
  end

  def test_tryaccept_fail
    assert_equal nil, @srv.kgio_tryaccept
  end

  def test_blocking_accept
    t0 = Time.now
    pid = fork { sleep 1; a = client_connect; sleep; a.close }
    b = @srv.kgio_accept
    elapsed = Time.now - t0
    assert_kind_of Kgio::Socket, b
    assert_equal @host, b.kgio_addr
    Process.kill(:KILL, pid)
    Process.waitpid(pid)
    assert elapsed >= 1, "elapsed: #{elapsed}"
  end

  def test_blocking_accept_with_nonblock_socket
    @srv.nonblock = true
    t0 = Time.now
    pid = fork { sleep 1; a = client_connect; sleep; a.close }
    b = @srv.kgio_accept
    elapsed = Time.now - t0
    assert_kind_of Kgio::Socket, b
    assert_equal @host, b.kgio_addr
    Process.kill(:KILL, pid)
    Process.waitpid(pid)
    assert elapsed >= 1, "elapsed: #{elapsed}"

    t0 = Time.now
    pid = fork { sleep 6; a = client_connect; sleep; a.close }
    b = @srv.kgio_accept
    elapsed = Time.now - t0
    assert_kind_of Kgio::Socket, b
    assert_equal @host, b.kgio_addr
    Process.kill(:KILL, pid)
    Process.waitpid(pid)
    assert elapsed >= 6, "elapsed: #{elapsed}"

    t0 = Time.now
    pid = fork { sleep 1; a = client_connect; sleep; a.close }
    b = @srv.kgio_accept
    elapsed = Time.now - t0
    assert_kind_of Kgio::Socket, b
    assert_equal @host, b.kgio_addr
    Process.kill(:KILL, pid)
    Process.waitpid(pid)
    assert elapsed >= 1, "elapsed: #{elapsed}"
  end
end

git clone git://yhbt.net/kgio.git
git clone https://yhbt.net/kgio.git