README: update with extra disclaimer
[raindrops.git] / test / test_tcp_info.rb
blob2dc5c503a2e55615edac9c1bb2119f4a123c0a04
1 # -*- encoding: binary -*-
2 # frozen_string_literal: false
3 require 'test/unit'
4 require 'tempfile'
5 require 'raindrops'
6 require 'socket'
7 require 'pp'
8 $stderr.sync = $stdout.sync = true
9 class TestTCP_Info < Test::Unit::TestCase
11   TEST_ADDR = ENV['UNICORN_TEST_ADDR'] || '127.0.0.1'
13   # Linux kernel commit 5ee3afba88f5a79d0bff07ddd87af45919259f91
14   TCP_INFO_useful_listenq = `uname -r`.strip >= '2.6.24'
16   def test_tcp_server_unacked
17     return if RUBY_PLATFORM !~ /linux/ # unacked not implemented on others...
18     s = TCPServer.new(TEST_ADDR, 0)
19     rv = Raindrops::TCP_Info.new s
20     c = TCPSocket.new TEST_ADDR, s.addr[1]
21     tmp = Raindrops::TCP_Info.new s
22     TCP_INFO_useful_listenq and assert_equal 1, tmp.unacked
24     assert_equal 0, rv.unacked
25     a = s.accept
26     tmp = Raindrops::TCP_Info.new s
27     assert_equal 0, tmp.unacked
28     before = tmp.object_id
30     tmp.get!(s)
31     assert_equal before, tmp.object_id
33   ensure
34     [ c, a, s ].compact.each(&:close)
35   end
37   def test_accessors
38     s = TCPServer.new TEST_ADDR, 0
39     tmp = Raindrops::TCP_Info.new s
40     tcp_info_methods = tmp.methods - Object.new.methods
41     assert tcp_info_methods.size >= 32
42     tcp_info_methods.each do |m|
43       next if m.to_sym == :get!
44       next if ! tmp.respond_to?(m)
45       val = tmp.__send__ m
46       assert_kind_of Integer, val
47       assert val >= 0
48     end
49     assert tmp.respond_to?(:state), 'every OS knows about TCP state, right?'
50   ensure
51     s.close
52   end
54   def test_tcp_server_delayed
55     delay = 0.010
56     delay_ms = (delay * 1000).to_i
57     s = TCPServer.new(TEST_ADDR, 0)
58     c = TCPSocket.new TEST_ADDR, s.addr[1]
59     c.syswrite "."
60     sleep(delay * 1.2)
61     a = s.accept
62     i = Raindrops::TCP_Info.new(a)
63     assert i.last_data_recv >= delay_ms, "#{i.last_data_recv} < #{delay_ms}"
64   ensure
65     c.close if c
66     a.close if a
67     s.close
68   end
70   def test_tcp_server_state_closed
71     s = TCPServer.new(TEST_ADDR, 0)
72     c = TCPSocket.new(TEST_ADDR, s.addr[1])
73     i = Raindrops::TCP_Info.allocate
74     a = s.accept
75     i.get!(a)
76     state = i.state
77     if Raindrops.const_defined?(:TCP)
78       assert_equal state, Raindrops::TCP[:ESTABLISHED]
79     end
80     c = c.close
81     sleep(0.01) # wait for kernel to update state
82     i.get!(a)
83     assert_not_equal state, i.state
84   ensure
85     s.close if s
86     c.close if c
87     a.close if a
88   end
89 end if defined? Raindrops::TCP_Info