unicorn.git  about / heads / tags
Rack HTTP server for Unix and fast clients
blob 1298f0e372d9c218f53333c8744588e1ff692443 5492 bytes (raw)
$ git show no-kgio-wip:test/unit/test_configurator.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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
 
# -*- encoding: binary -*-

require 'test/unit'
require 'tempfile'
require 'unicorn'

TestStruct = Struct.new(
  *(Unicorn::Configurator::DEFAULTS.keys + %w(listener_opts listeners)))
class TestConfigurator < Test::Unit::TestCase

  def test_config_init
    Unicorn::Configurator.new {}
  end

  def test_expand_addr
    meth = Unicorn::Configurator.new.method(:expand_addr)

    assert_equal "/var/run/unicorn.sock", meth.call("/var/run/unicorn.sock")
    assert_equal "#{Dir.pwd}/foo/bar.sock", meth.call("unix:foo/bar.sock")

    path = meth.call("~/foo/bar.sock")
    assert_equal "/", path[0..0]
    assert_match %r{/foo/bar\.sock\z}, path

    path = meth.call("~root/foo/bar.sock")
    assert_equal "/", path[0..0]
    assert_match %r{/foo/bar\.sock\z}, path

    assert_equal "1.2.3.4:2007", meth.call('1.2.3.4:2007')
    assert_equal "0.0.0.0:2007", meth.call('0.0.0.0:2007')
    assert_equal "0.0.0.0:2007", meth.call(':2007')
    assert_equal "0.0.0.0:2007", meth.call('*:2007')
    assert_equal "0.0.0.0:2007", meth.call('2007')
    assert_equal "0.0.0.0:2007", meth.call(2007)

    %w([::1]:2007 [::]:2007).each do |addr|
      assert_equal addr, meth.call(addr.dup)
    end

    # for Rainbows! users only
    assert_equal "[::]:80", meth.call("[::]")
    assert_equal "127.6.6.6:80", meth.call("127.6.6.6")

    # the next two aren't portable, consider them unsupported for now
    # assert_match %r{\A\d+\.\d+\.\d+\.\d+:2007\z}, meth.call('1:2007')
    # assert_match %r{\A\d+\.\d+\.\d+\.\d+:2007\z}, meth.call('2:2007')
  end

  def test_config_invalid
    tmp = Tempfile.new('unicorn_config')
    tmp.syswrite(%q(asdfasdf "hello-world"))
    assert_raises(NoMethodError) do
      Unicorn::Configurator.new(:config_file => tmp.path)
    end
  end

  def test_config_non_existent
    tmp = Tempfile.new('unicorn_config')
    path = tmp.path
    tmp.close!
    assert_raises(Errno::ENOENT) do
      Unicorn::Configurator.new(:config_file => path)
    end
  end

  def test_config_defaults
    cfg = Unicorn::Configurator.new(:use_defaults => true)
    test_struct = TestStruct.new
    cfg.commit!(test_struct)
    Unicorn::Configurator::DEFAULTS.each do |key,value|
      assert_equal value, test_struct.__send__(key)
    end
  end

  def test_config_defaults_skip
    cfg = Unicorn::Configurator.new(:use_defaults => true)
    skip = [ :logger ]
    test_struct = TestStruct.new
    cfg.commit!(test_struct, :skip => skip)
    Unicorn::Configurator::DEFAULTS.each do |key,value|
      next if skip.include?(key)
      assert_equal value, test_struct.__send__(key)
    end
    assert_nil test_struct.logger
  end

  def test_listen_options
    tmp = Tempfile.new('unicorn_config')
    expect = { :sndbuf => 1, :rcvbuf => 2, :backlog => 10 }.freeze
    listener = "127.0.0.1:12345"
    tmp.syswrite("listen '#{listener}', #{expect.inspect}\n")
    cfg = Unicorn::Configurator.new(:config_file => tmp.path)
    test_struct = TestStruct.new
    cfg.commit!(test_struct)
    assert(listener_opts = test_struct.listener_opts)
    assert_equal expect, listener_opts[listener]
  end

  def test_listen_option_bad
    tmp = Tempfile.new('unicorn_config')
    expect = { :sndbuf => "five" }
    listener = "127.0.0.1:12345"
    tmp.syswrite("listen '#{listener}', #{expect.inspect}\n")
    assert_raises(ArgumentError) do
      Unicorn::Configurator.new(:config_file => tmp.path)
    end
  end

  def test_listen_option_bad_delay
    tmp = Tempfile.new('unicorn_config')
    expect = { :delay => "five" }
    listener = "127.0.0.1:12345"
    tmp.syswrite("listen '#{listener}', #{expect.inspect}\n")
    assert_raises(ArgumentError) do
      Unicorn::Configurator.new(:config_file => tmp.path)
    end
  end

  def test_listen_option_float_delay
    tmp = Tempfile.new('unicorn_config')
    expect = { :delay => 0.5 }
    listener = "127.0.0.1:12345"
    tmp.syswrite("listen '#{listener}', #{expect.inspect}\n")
    Unicorn::Configurator.new(:config_file => tmp.path)
  end

  def test_listen_option_int_delay
    tmp = Tempfile.new('unicorn_config')
    expect = { :delay => 5 }
    listener = "127.0.0.1:12345"
    tmp.syswrite("listen '#{listener}', #{expect.inspect}\n")
    Unicorn::Configurator.new(:config_file => tmp.path)
  end

  def test_check_client_connection
    tmp = Tempfile.new('unicorn_config')
    test_struct = TestStruct.new
    tmp.syswrite("check_client_connection true\n")

    assert_nothing_raised do
      Unicorn::Configurator.new(:config_file => tmp.path).commit!(test_struct)
    end

    assert test_struct.check_client_connection
  end

  def test_check_client_connection_with_tcp_bad
    tmp = Tempfile.new('unicorn_config')
    test_struct = TestStruct.new
    listener = "127.0.0.1:12345"
    tmp.syswrite("check_client_connection true\n")
    tmp.syswrite("listen '#{listener}', :tcp_nopush => true\n")

    assert_raises(ArgumentError) do
      Unicorn::Configurator.new(:config_file => tmp.path).commit!(test_struct)
    end
  end

  def test_after_fork_proc
    test_struct = TestStruct.new
    [ proc { |a,b| }, Proc.new { |a,b| }, lambda { |a,b| } ].each do |my_proc|
      Unicorn::Configurator.new(:after_fork => my_proc).commit!(test_struct)
      assert_equal my_proc, test_struct.after_fork
    end
  end

  def test_after_fork_wrong_arity
    [ proc { |a| }, Proc.new { }, lambda { |a,b,c| } ].each do |my_proc|
      assert_raises(ArgumentError) do
        Unicorn::Configurator.new(:after_fork => my_proc)
      end
    end
  end

end

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