inet_diag: small reorganization
[raindrops.git] / test / test_linux_tcp_info.rb
blob1751deeff4bba3ca854f2469b0ec511ac90fbe12
1 # -*- encoding: binary -*-
2 require 'test/unit'
3 require 'tempfile'
4 require 'raindrops'
5 require 'socket'
6 require 'pp'
7 $stderr.sync = $stdout.sync = true
8 class TestLinuxTCP_Info < Test::Unit::TestCase
10   TEST_ADDR = ENV['UNICORN_TEST_ADDR'] || '127.0.0.1'
12   # Linux kernel commit 5ee3afba88f5a79d0bff07ddd87af45919259f91
13   TCP_INFO_useful_listenq = `uname -r`.strip >= '2.6.24'
15   def test_tcp_server
16     s = TCPServer.new(TEST_ADDR, 0)
17     rv = Raindrops::TCP_Info.new s
18     c = TCPSocket.new TEST_ADDR, s.addr[1]
19     tmp = Raindrops::TCP_Info.new s
20     TCP_INFO_useful_listenq and assert_equal 1, tmp.unacked
22     assert_equal 0, rv.unacked
23     a = s.accept
24     tmp = Raindrops::TCP_Info.new s
25     assert_equal 0, tmp.unacked
26     ensure
27       c.close if c
28       a.close if a
29       s.close
30   end
32   def test_accessors
33     s = TCPServer.new TEST_ADDR, 0
34     tmp = Raindrops::TCP_Info.new s
35     tcp_info_methods = tmp.methods - Object.new.methods
36     assert tcp_info_methods.size >= 32
37     tcp_info_methods.each do |m|
38       val = tmp.__send__ m
39       assert_kind_of Integer, val
40       assert val >= 0
41     end
42     ensure
43       s.close
44   end
46   def test_tcp_server_delayed
47     delay = 0.010
48     delay_ms = (delay * 1000).to_i
49     s = TCPServer.new(TEST_ADDR, 0)
50     c = TCPSocket.new TEST_ADDR, s.addr[1]
51     c.syswrite "."
52     sleep delay
53     a = s.accept
54     i = Raindrops::TCP_Info.new(a)
55     assert i.last_data_recv >= delay_ms, "#{i.last_data_recv} < #{delay_ms}"
56     ensure
57       c.close if c
58       a.close if a
59       s.close
60   end
61 end