1 # -*- encoding: binary -*-
7 TestStruct = Struct.new(
8 *(Unicorn::Configurator::DEFAULTS.keys + %w(listener_opts listeners)))
9 class TestConfigurator < Test::Unit::TestCase
12 Unicorn::Configurator.new {}
16 meth = Unicorn::Configurator.new.method(:expand_addr)
18 assert_equal "/var/run/unicorn.sock", meth.call("/var/run/unicorn.sock")
19 assert_equal "#{Dir.pwd}/foo/bar.sock", meth.call("unix:foo/bar.sock")
21 path = meth.call("~/foo/bar.sock")
22 assert_equal "/", path[0..0]
23 assert_match %r{/foo/bar\.sock\z}, path
25 path = meth.call("~root/foo/bar.sock")
26 assert_equal "/", path[0..0]
27 assert_match %r{/foo/bar\.sock\z}, path
29 assert_equal "1.2.3.4:2007", meth.call('1.2.3.4:2007')
30 assert_equal "0.0.0.0:2007", meth.call('0.0.0.0:2007')
31 assert_equal "0.0.0.0:2007", meth.call(':2007')
32 assert_equal "0.0.0.0:2007", meth.call('*:2007')
33 assert_equal "0.0.0.0:2007", meth.call('2007')
34 assert_equal "0.0.0.0:2007", meth.call(2007)
36 %w([::1]:2007 [::]:2007).each do |addr|
37 assert_equal addr, meth.call(addr.dup)
40 # for Rainbows! users only
41 assert_equal "[::]:80", meth.call("[::]")
42 assert_equal "127.6.6.6:80", meth.call("127.6.6.6")
44 # the next two aren't portable, consider them unsupported for now
45 # assert_match %r{\A\d+\.\d+\.\d+\.\d+:2007\z}, meth.call('1:2007')
46 # assert_match %r{\A\d+\.\d+\.\d+\.\d+:2007\z}, meth.call('2:2007')
49 def test_config_invalid
50 tmp = Tempfile.new('unicorn_config')
51 tmp.syswrite(%q(asdfasdf "hello-world"))
52 assert_raises(NoMethodError) do
53 Unicorn::Configurator.new(:config_file => tmp.path)
57 def test_config_non_existent
58 tmp = Tempfile.new('unicorn_config')
61 assert_raises(Errno::ENOENT) do
62 Unicorn::Configurator.new(:config_file => path)
66 def test_config_defaults
67 cfg = Unicorn::Configurator.new(:use_defaults => true)
68 test_struct = TestStruct.new
69 cfg.commit!(test_struct)
70 Unicorn::Configurator::DEFAULTS.each do |key,value|
71 assert_equal value, test_struct.__send__(key)
75 def test_config_defaults_skip
76 cfg = Unicorn::Configurator.new(:use_defaults => true)
78 test_struct = TestStruct.new
79 cfg.commit!(test_struct, :skip => skip)
80 Unicorn::Configurator::DEFAULTS.each do |key,value|
81 next if skip.include?(key)
82 assert_equal value, test_struct.__send__(key)
84 assert_nil test_struct.logger
87 def test_listen_options
88 tmp = Tempfile.new('unicorn_config')
89 expect = { :sndbuf => 1, :rcvbuf => 2, :backlog => 10 }.freeze
90 listener = "127.0.0.1:12345"
91 tmp.syswrite("listen '#{listener}', #{expect.inspect}\n")
92 cfg = Unicorn::Configurator.new(:config_file => tmp.path)
93 test_struct = TestStruct.new
94 cfg.commit!(test_struct)
95 assert(listener_opts = test_struct.listener_opts)
96 assert_equal expect, listener_opts[listener]
99 def test_listen_option_bad
100 tmp = Tempfile.new('unicorn_config')
101 expect = { :sndbuf => "five" }
102 listener = "127.0.0.1:12345"
103 tmp.syswrite("listen '#{listener}', #{expect.inspect}\n")
104 assert_raises(ArgumentError) do
105 Unicorn::Configurator.new(:config_file => tmp.path)
109 def test_listen_option_bad_delay
110 tmp = Tempfile.new('unicorn_config')
111 expect = { :delay => "five" }
112 listener = "127.0.0.1:12345"
113 tmp.syswrite("listen '#{listener}', #{expect.inspect}\n")
114 assert_raises(ArgumentError) do
115 Unicorn::Configurator.new(:config_file => tmp.path)
119 def test_listen_option_float_delay
120 tmp = Tempfile.new('unicorn_config')
121 expect = { :delay => 0.5 }
122 listener = "127.0.0.1:12345"
123 tmp.syswrite("listen '#{listener}', #{expect.inspect}\n")
124 Unicorn::Configurator.new(:config_file => tmp.path)
127 def test_listen_option_int_delay
128 tmp = Tempfile.new('unicorn_config')
129 expect = { :delay => 5 }
130 listener = "127.0.0.1:12345"
131 tmp.syswrite("listen '#{listener}', #{expect.inspect}\n")
132 Unicorn::Configurator.new(:config_file => tmp.path)
135 def test_check_client_connection
136 tmp = Tempfile.new('unicorn_config')
137 test_struct = TestStruct.new
138 tmp.syswrite("check_client_connection true\n")
140 assert_nothing_raised do
141 Unicorn::Configurator.new(:config_file => tmp.path).commit!(test_struct)
144 assert test_struct.check_client_connection
147 def test_check_client_connection_with_tcp_bad
148 tmp = Tempfile.new('unicorn_config')
149 test_struct = TestStruct.new
150 listener = "127.0.0.1:12345"
151 tmp.syswrite("check_client_connection true\n")
152 tmp.syswrite("listen '#{listener}', :tcp_nopush => true\n")
154 assert_raises(ArgumentError) do
155 Unicorn::Configurator.new(:config_file => tmp.path).commit!(test_struct)
159 def test_after_fork_proc
160 test_struct = TestStruct.new
161 [ proc { |a,b| }, Proc.new { |a,b| }, lambda { |a,b| } ].each do |my_proc|
162 Unicorn::Configurator.new(:after_fork => my_proc).commit!(test_struct)
163 assert_equal my_proc, test_struct.after_fork
167 def test_after_fork_wrong_arity
168 [ proc { |a| }, Proc.new { }, lambda { |a,b,c| } ].each do |my_proc|
169 assert_raises(ArgumentError) do
170 Unicorn::Configurator.new(:after_fork => my_proc)