1 # -*- encoding: binary -*-
2 # frozen_string_literal: false
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
26 tmp = Raindrops::TCP_Info.new s
27 assert_equal 0, tmp.unacked
28 before = tmp.object_id
31 assert_equal before, tmp.object_id
34 [ c, a, s ].compact.each(&:close)
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)
46 assert_kind_of Integer, val
49 assert tmp.respond_to?(:state), 'every OS knows about TCP state, right?'
54 def test_tcp_server_delayed
56 delay_ms = (delay * 1000).to_i
57 s = TCPServer.new(TEST_ADDR, 0)
58 c = TCPSocket.new TEST_ADDR, s.addr[1]
62 i = Raindrops::TCP_Info.new(a)
63 assert i.last_data_recv >= delay_ms, "#{i.last_data_recv} < #{delay_ms}"
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
77 if Raindrops.const_defined?(:TCP)
78 assert_equal state, Raindrops::TCP[:ESTABLISHED]
81 sleep(0.01) # wait for kernel to update state
83 assert_not_equal state, i.state
89 end if defined? Raindrops::TCP_Info