raindrops.git  about / heads / tags
real-time stats for preforking Rack servers
blob 82083e0030cec95c2b0992916c83dc8406cd6218 1306 bytes (raw)
$ git show HEAD:test/test_linux_reuseport_tcp_listen_stats.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
 
# -*- encoding: binary -*-
# frozen_string_literal: false
require "./test/rack_unicorn"
require 'test/unit'
require 'socket'
require 'raindrops'
$stderr.sync = $stdout.sync = true

class TestLinuxReuseportTcpListenStats < Test::Unit::TestCase
  include Raindrops::Linux
  include Unicorn::SocketHelper
  TEST_ADDR = ENV['UNICORN_TEST_ADDR'] || '127.0.0.1'
  DEFAULT_BACKLOG = 10

  def setup
    @socks = []
  end

  def teardown
    @socks.each { |io| io.closed? or io.close }
  end

  def new_socket_server(**kwargs)
    s = new_tcp_server TEST_ADDR, kwargs[:port] || 0, kwargs
    s.listen(kwargs[:backlog] || DEFAULT_BACKLOG)
    @socks << s
    [ s, s.addr[1] ]
  end

  def new_client(port)
    s = TCPSocket.new("127.0.0.1", port)
    @socks << s
    s
  end

  def test_reuseport_queue_stats
    listeners = 10
    _, port = new_socket_server(reuseport: true)
    addr = "#{TEST_ADDR}:#{port}"
    (listeners - 1).times do
      new_socket_server(reuseport: true, port: port)
    end

    listeners.times do |i|
      all = Raindrops::Linux.tcp_listener_stats
      assert_equal [0, i], all[addr].to_a
      new_client(port)
      all = Raindrops::Linux.tcp_listener_stats
      assert_equal [0, i+1], all[addr].to_a
    end
  end
end if RUBY_PLATFORM =~ /linux/ && Object.const_defined?(:Unicorn)

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