1 # -*- encoding: binary -*-
3 # Copyright (c) 2009 Eric Wong
4 FLOCK_PATH = File.expand_path(__FILE__)
5 require './test/test_helper'
8 $unicorn_bin = ENV['UNICORN_TEST_BIN'] || "unicorn"
10 do_test = system($unicorn_bin, '-v')
14 warn "#{$unicorn_bin} not found in PATH=#{ENV['PATH']}, " \
18 unless try_require('rack')
19 warn "Unable to load Rack, skipping this test"
23 class ExecTest < Test::Unit::TestCase
27 use Rack::ContentLength
28 run proc { |env| [ 200, { 'Content-Type' => 'text/plain' }, [ "HI\\n" ] ] }
31 SHOW_RACK_ENV = <<-EOS
32 use Rack::ContentLength
34 [ 200, { 'Content-Type' => 'text/plain' }, [ ENV['RACK_ENV'] ] ]
41 [ 200, { 'Content-Type' => 'text/plain' }, [ "HI\\n" ] ]
46 COMMON_TMP = Tempfile.new('unicorn_tmp') unless defined?(COMMON_TMP)
51 logger Logger.new('#{COMMON_TMP.path}')
52 before_fork do |server, worker|
53 server.logger.info "before_fork: worker=\#{worker.nr}"
57 WORKING_DIRECTORY_CHECK_RU = <<-EOS
58 use Rack::ContentLength
62 b = ::File.stat(Dir.pwd)
63 if (a.ino == b.ino && a.dev == b.dev)
64 [ 200, { 'Content-Type' => 'text/plain' }, [ pwd ] ]
66 [ 404, { 'Content-Type' => 'text/plain' }, [] ]
73 @tmpfile = Tempfile.new('unicorn_exec_test')
74 @tmpdir = @tmpfile.path
78 @addr = ENV['UNICORN_TEST_ADDR'] || '127.0.0.1'
79 @port = unused_port(@addr)
85 return if @start_pid != $$
87 FileUtils.rmtree(@tmpdir)
88 @sockets.each { |path| File.unlink(path) rescue nil }
90 Process.kill('-QUIT', 0)
92 Process.waitpid(-1, Process::WNOHANG) or break
99 def test_working_directory_rel_path_config_file
100 other = Tempfile.new('unicorn.wd')
101 File.unlink(other.path)
102 Dir.mkdir(other.path)
103 File.open("config.ru", "wb") do |fp|
104 fp.syswrite WORKING_DIRECTORY_CHECK_RU
106 FileUtils.cp("config.ru", other.path + "/config.ru")
109 tmp = File.open('unicorn.config', 'wb')
111 working_directory '#@tmpdir'
112 listen '#@addr:#@port'
114 pid = xfork { redirect_test_io { exec($unicorn_bin, "-c#{tmp.path}") } }
115 wait_workers_ready("test_stderr.#{pid}.log", 1)
116 results = hit(["http://#@addr:#@port/"])
117 assert_equal @tmpdir, results.first
118 File.truncate("test_stderr.#{pid}.log", 0)
123 working_directory '#{other.path}'
124 listen '#@addr:#@port'
127 Process.kill(:HUP, pid)
129 re = /config_file=(.+) would not be accessible in working_directory=(.+)/
132 lines = File.readlines("test_stderr.#{pid}.log")
135 File.truncate("test_stderr.#{pid}.log", 0)
136 FileUtils.cp('unicorn.config', other.path + "/unicorn.config")
137 Process.kill(:HUP, pid)
138 wait_workers_ready("test_stderr.#{pid}.log", 1)
139 results = hit(["http://#@addr:#@port/"])
140 assert_equal other.path, results.first
142 Process.kill(:QUIT, pid)
144 FileUtils.rmtree(other.path)
147 def test_working_directory
148 other = Tempfile.new('unicorn.wd')
149 File.unlink(other.path)
150 Dir.mkdir(other.path)
151 File.open("config.ru", "wb") do |fp|
152 fp.syswrite WORKING_DIRECTORY_CHECK_RU
154 FileUtils.cp("config.ru", other.path + "/config.ru")
155 tmp = Tempfile.new('unicorn.config')
157 working_directory '#@tmpdir'
158 listen '#@addr:#@port'
160 pid = xfork { redirect_test_io { exec($unicorn_bin, "-c#{tmp.path}") } }
161 wait_workers_ready("test_stderr.#{pid}.log", 1)
162 results = hit(["http://#@addr:#@port/"])
163 assert_equal @tmpdir, results.first
164 File.truncate("test_stderr.#{pid}.log", 0)
169 working_directory '#{other.path}'
170 listen '#@addr:#@port'
173 Process.kill(:HUP, pid)
174 wait_workers_ready("test_stderr.#{pid}.log", 1)
175 results = hit(["http://#@addr:#@port/"])
176 assert_equal other.path, results.first
178 Process.kill(:QUIT, pid)
180 FileUtils.rmtree(other.path)
183 def test_working_directory_controls_relative_paths
184 other = Tempfile.new('unicorn.wd')
185 File.unlink(other.path)
186 Dir.mkdir(other.path)
187 File.open("config.ru", "wb") do |fp|
188 fp.syswrite WORKING_DIRECTORY_CHECK_RU
190 FileUtils.cp("config.ru", other.path + "/config.ru")
191 system('mkfifo', "#{other.path}/fifo")
192 tmp = Tempfile.new('unicorn.config')
195 stderr_path "stderr_log_here"
196 stdout_path "stdout_log_here"
197 working_directory '#{other.path}'
198 listen '#@addr:#@port'
199 after_fork do |server, worker|
200 File.open("fifo", "wb").close
203 pid = xfork { redirect_test_io { exec($unicorn_bin, "-c#{tmp.path}") } }
204 File.open("#{other.path}/fifo", "rb").close
206 assert ! File.exist?("stderr_log_here")
207 assert ! File.exist?("stdout_log_here")
208 assert ! File.exist?("pid_file_here")
210 assert ! File.exist?("#@tmpdir/stderr_log_here")
211 assert ! File.exist?("#@tmpdir/stdout_log_here")
212 assert ! File.exist?("#@tmpdir/pid_file_here")
214 assert File.exist?("#{other.path}/pid_file_here")
215 assert_equal "#{pid}\n", File.read("#{other.path}/pid_file_here")
216 assert File.exist?("#{other.path}/stderr_log_here")
217 assert File.exist?("#{other.path}/stdout_log_here")
218 wait_master_ready("#{other.path}/stderr_log_here")
220 Process.kill(:QUIT, pid)
222 FileUtils.rmtree(other.path)
226 def test_exit_signals
227 %w(INT TERM QUIT).each do |sig|
228 File.open("config.ru", "wb") { |fp| fp.syswrite(HI) }
229 pid = xfork { redirect_test_io { exec($unicorn_bin, "-l#@addr:#@port") } }
230 wait_master_ready("test_stderr.#{pid}.log")
231 wait_workers_ready("test_stderr.#{pid}.log", 1)
233 Process.kill(sig, pid)
234 pid, status = Process.waitpid2(pid)
236 reaped = File.readlines("test_stderr.#{pid}.log").grep(/reaped/)
237 assert_equal 1, reaped.size
238 assert status.exited?
243 File.open("config.ru", "wb") { |fp| fp.syswrite(HI) }
245 redirect_test_io { exec($unicorn_bin, "-l", "#{@addr}:#{@port}") }
247 results = retry_hit(["http://#{@addr}:#{@port}/"])
248 assert_equal String, results[0].class
252 def test_rack_env_unset
253 File.open("config.ru", "wb") { |fp| fp.syswrite(SHOW_RACK_ENV) }
254 pid = fork { redirect_test_io { exec($unicorn_bin, "-l#@addr:#@port") } }
255 results = retry_hit(["http://#{@addr}:#{@port}/"])
256 assert_equal "development", results.first
260 def test_rack_env_cli_set
261 File.open("config.ru", "wb") { |fp| fp.syswrite(SHOW_RACK_ENV) }
263 redirect_test_io { exec($unicorn_bin, "-l#@addr:#@port", "-Easdf") }
265 results = retry_hit(["http://#{@addr}:#{@port}/"])
266 assert_equal "asdf", results.first
270 def test_rack_env_ENV_set
271 File.open("config.ru", "wb") { |fp| fp.syswrite(SHOW_RACK_ENV) }
273 ENV["RACK_ENV"] = "foobar"
274 redirect_test_io { exec($unicorn_bin, "-l#@addr:#@port") }
276 results = retry_hit(["http://#{@addr}:#{@port}/"])
277 assert_equal "foobar", results.first
281 def test_rack_env_cli_override_ENV
282 File.open("config.ru", "wb") { |fp| fp.syswrite(SHOW_RACK_ENV) }
284 ENV["RACK_ENV"] = "foobar"
285 redirect_test_io { exec($unicorn_bin, "-l#@addr:#@port", "-Easdf") }
287 results = retry_hit(["http://#{@addr}:#{@port}/"])
288 assert_equal "asdf", results.first
293 File.open("config.ru", "wb") { |fp| fp.syswrite(HI) }
294 pid = fork { redirect_test_io { exec($unicorn_bin, "-l#@addr:#@port") } }
295 log = "test_stderr.#{pid}.log"
296 wait_master_ready(log)
298 Process.kill(:TTIN, pid)
299 wait_workers_ready(log, i)
301 File.truncate(log, 0)
303 [ 2, 1, 0].each { |i|
304 Process.kill(:TTOU, pid)
305 DEFAULT_TRIES.times {
307 reaped = File.readlines(log).grep(/reaped.*\s*worker=#{i}$/)
308 break if reaped.size == 1
310 assert_equal 1, reaped.size
316 assert(system($unicorn_bin, "-h"), "help text returns true")
318 assert_equal 0, File.stat("test_stderr.#$$.log").size
319 assert_not_equal 0, File.stat("test_stdout.#$$.log").size
320 lines = File.readlines("test_stdout.#$$.log")
322 # Be considerate of the on-call technician working from their
323 # mobile phone or netbook on a slow connection :)
324 assert lines.size <= 24, "help height fits in an ANSI terminal window"
327 assert line.size <= 80, "help width fits in an ANSI terminal window"
331 def test_broken_reexec_config
332 File.open("config.ru", "wb") { |fp| fp.syswrite(HI) }
333 pid_file = "#{@tmpdir}/test.pid"
334 old_file = "#{pid_file}.oldbin"
335 ucfg = Tempfile.new('unicorn_test_config')
336 ucfg.syswrite("listen %(#@addr:#@port)\n")
337 ucfg.syswrite("pid %(#{pid_file})\n")
338 ucfg.syswrite("logger Logger.new(%(#{@tmpdir}/log))\n")
341 exec($unicorn_bin, "-D", "-l#{@addr}:#{@port}", "-c#{ucfg.path}")
344 results = retry_hit(["http://#{@addr}:#{@port}/"])
345 assert_equal String, results[0].class
347 wait_for_file(pid_file)
349 Process.kill(:USR2, File.read(pid_file).to_i)
350 wait_for_file(old_file)
351 wait_for_file(pid_file)
352 old_pid = File.read(old_file).to_i
353 Process.kill(:QUIT, old_pid)
354 wait_for_death(old_pid)
356 ucfg.syswrite("timeout %(#{pid_file})\n") # introduce a bug
357 current_pid = File.read(pid_file).to_i
358 Process.kill(:USR2, current_pid)
360 # wait for pid_file to restore itself
361 tries = DEFAULT_TRIES
363 while current_pid != File.read(pid_file).to_i
364 sleep(DEFAULT_RES) and (tries -= 1) > 0
367 (sleep(DEFAULT_RES) and (tries -= 1) > 0) and retry
369 assert_equal current_pid, File.read(pid_file).to_i
371 tries = DEFAULT_TRIES
372 while File.exist?(old_file)
373 (sleep(DEFAULT_RES) and (tries -= 1) > 0) or break
375 assert ! File.exist?(old_file), "oldbin=#{old_file} gone"
376 port2 = unused_port(@addr)
381 ucfg.syswrite("listen %(#@addr:#@port)\n")
382 ucfg.syswrite("listen %(#@addr:#{port2})\n")
383 ucfg.syswrite("pid %(#{pid_file})\n")
384 Process.kill(:USR2, current_pid)
386 wait_for_file(old_file)
387 wait_for_file(pid_file)
388 new_pid = File.read(pid_file).to_i
389 assert_not_equal current_pid, new_pid
390 assert_equal current_pid, File.read(old_file).to_i
391 results = retry_hit(["http://#{@addr}:#{@port}/",
392 "http://#{@addr}:#{port2}/"])
393 assert_equal String, results[0].class
394 assert_equal String, results[1].class
396 Process.kill(:QUIT, current_pid)
397 Process.kill(:QUIT, new_pid)
400 def test_broken_reexec_ru
401 File.open("config.ru", "wb") { |fp| fp.syswrite(HI) }
402 pid_file = "#{@tmpdir}/test.pid"
403 old_file = "#{pid_file}.oldbin"
404 ucfg = Tempfile.new('unicorn_test_config')
405 ucfg.syswrite("pid %(#{pid_file})\n")
406 ucfg.syswrite("logger Logger.new(%(#{@tmpdir}/log))\n")
409 exec($unicorn_bin, "-D", "-l#{@addr}:#{@port}", "-c#{ucfg.path}")
412 results = retry_hit(["http://#{@addr}:#{@port}/"])
413 assert_equal String, results[0].class
415 wait_for_file(pid_file)
417 Process.kill(:USR2, File.read(pid_file).to_i)
418 wait_for_file(old_file)
419 wait_for_file(pid_file)
420 old_pid = File.read(old_file).to_i
421 Process.kill(:QUIT, old_pid)
422 wait_for_death(old_pid)
424 File.unlink("config.ru") # break reloading
425 current_pid = File.read(pid_file).to_i
426 Process.kill(:USR2, current_pid)
428 # wait for pid_file to restore itself
429 tries = DEFAULT_TRIES
431 while current_pid != File.read(pid_file).to_i
432 sleep(DEFAULT_RES) and (tries -= 1) > 0
435 (sleep(DEFAULT_RES) and (tries -= 1) > 0) and retry
438 tries = DEFAULT_TRIES
439 while File.exist?(old_file)
440 (sleep(DEFAULT_RES) and (tries -= 1) > 0) or break
442 assert ! File.exist?(old_file), "oldbin=#{old_file} gone"
443 assert_equal current_pid, File.read(pid_file).to_i
446 File.open("config.ru", "wb") { |fp| fp.syswrite(HI) }
447 Process.kill(:USR2, current_pid)
448 wait_for_file(old_file)
449 wait_for_file(pid_file)
450 new_pid = File.read(pid_file).to_i
451 assert_not_equal current_pid, new_pid
452 assert_equal current_pid, File.read(old_file).to_i
453 results = retry_hit(["http://#{@addr}:#{@port}/"])
454 assert_equal String, results[0].class
456 Process.kill(:QUIT, current_pid)
457 Process.kill(:QUIT, new_pid)
460 def test_unicorn_config_listener_swap
461 port_cli = unused_port
462 File.open("config.ru", "wb") { |fp| fp.syswrite(HI) }
463 ucfg = Tempfile.new('unicorn_test_config')
464 ucfg.syswrite("listen '#@addr:#@port'\n")
467 exec($unicorn_bin, "-c#{ucfg.path}", "-l#@addr:#{port_cli}")
470 results = retry_hit(["http://#@addr:#{port_cli}/"])
471 assert_equal String, results[0].class
472 results = retry_hit(["http://#@addr:#@port/"])
473 assert_equal String, results[0].class
475 port2 = unused_port(@addr)
478 ucfg.syswrite("listen '#@addr:#{port2}'\n")
479 Process.kill(:HUP, pid)
481 results = retry_hit(["http://#@addr:#{port2}/"])
482 assert_equal String, results[0].class
483 results = retry_hit(["http://#@addr:#{port_cli}/"])
484 assert_equal String, results[0].class
485 reuse = TCPServer.new(@addr, @port)
490 def test_unicorn_config_listen_with_options
491 File.open("config.ru", "wb") { |fp| fp.syswrite(HI) }
492 ucfg = Tempfile.new('unicorn_test_config')
493 ucfg.syswrite("listen '#{@addr}:#{@port}', :backlog => 512,\n")
494 ucfg.syswrite(" :rcvbuf => 4096,\n")
495 ucfg.syswrite(" :sndbuf => 4096\n")
497 redirect_test_io { exec($unicorn_bin, "-c#{ucfg.path}") }
499 results = retry_hit(["http://#{@addr}:#{@port}/"])
500 assert_equal String, results[0].class
504 def test_unicorn_config_per_worker_listen
506 pid_spit = 'use Rack::ContentLength;' \
507 'run proc { |e| [ 200, {"Content-Type"=>"text/plain"}, ["#$$\\n"] ] }'
508 File.open("config.ru", "wb") { |fp| fp.syswrite(pid_spit) }
509 tmp = Tempfile.new('test.socket')
510 File.unlink(tmp.path)
511 ucfg = Tempfile.new('unicorn_test_config')
512 ucfg.syswrite("listen '#@addr:#@port'\n")
513 ucfg.syswrite("after_fork { |s,w|\n")
514 ucfg.syswrite(" s.listen('#{tmp.path}', :backlog => 5, :sndbuf => 8192)\n")
515 ucfg.syswrite(" s.listen('#@addr:#{port2}', :rcvbuf => 8192)\n")
516 ucfg.syswrite("\n}\n")
518 redirect_test_io { exec($unicorn_bin, "-c#{ucfg.path}") }
520 results = retry_hit(["http://#{@addr}:#{@port}/"])
521 assert_equal String, results[0].class
522 worker_pid = results[0].to_i
523 assert_not_equal pid, worker_pid
524 s = UNIXSocket.new(tmp.path)
525 s.syswrite("GET / HTTP/1.0\r\n\r\n")
527 loop { results << s.sysread(4096) } rescue nil
529 assert_equal worker_pid, results.split(/\r\n/).last.to_i
530 results = hit(["http://#@addr:#{port2}/"])
531 assert_equal String, results[0].class
532 assert_equal worker_pid, results[0].to_i
536 def test_unicorn_config_listen_augments_cli
537 port2 = unused_port(@addr)
538 File.open("config.ru", "wb") { |fp| fp.syswrite(HI) }
539 ucfg = Tempfile.new('unicorn_test_config')
540 ucfg.syswrite("listen '#{@addr}:#{@port}'\n")
543 exec($unicorn_bin, "-c#{ucfg.path}", "-l#{@addr}:#{port2}")
546 uris = [@port, port2].map { |i| "http://#{@addr}:#{i}/" }
547 results = retry_hit(uris)
548 assert_equal results.size, uris.size
549 assert_equal String, results[0].class
550 assert_equal String, results[1].class
554 def test_weird_config_settings
555 File.open("config.ru", "wb") { |fp| fp.syswrite(HI) }
556 ucfg = Tempfile.new('unicorn_test_config')
557 ucfg.syswrite(HEAVY_CFG)
560 exec($unicorn_bin, "-c#{ucfg.path}", "-l#{@addr}:#{@port}")
564 results = retry_hit(["http://#{@addr}:#{@port}/"])
565 assert_equal String, results[0].class
566 wait_master_ready(COMMON_TMP.path)
567 wait_workers_ready(COMMON_TMP.path, 4)
568 bf = File.readlines(COMMON_TMP.path).grep(/\bbefore_fork: worker=/)
569 assert_equal 4, bf.size
570 rotate = Tempfile.new('unicorn_rotate')
572 File.rename(COMMON_TMP.path, rotate.path)
573 Process.kill(:USR1, pid)
575 wait_for_file(COMMON_TMP.path)
576 assert File.exist?(COMMON_TMP.path), "#{COMMON_TMP.path} exists"
577 # USR1 should've been passed to all workers
578 tries = DEFAULT_TRIES
579 log = File.readlines(rotate.path)
580 while (tries -= 1) > 0 &&
581 log.grep(/reopening logs\.\.\./).size < 5
583 log = File.readlines(rotate.path)
585 assert_equal 5, log.grep(/reopening logs\.\.\./).size
586 assert_equal 0, log.grep(/done reopening logs/).size
588 tries = DEFAULT_TRIES
589 log = File.readlines(COMMON_TMP.path)
590 while (tries -= 1) > 0 && log.grep(/done reopening logs/).size < 5
592 log = File.readlines(COMMON_TMP.path)
594 assert_equal 5, log.grep(/done reopening logs/).size
595 assert_equal 0, log.grep(/reopening logs\.\.\./).size
597 Process.kill(:QUIT, pid)
598 pid, status = Process.waitpid2(pid)
600 assert status.success?, "exited successfully"
603 def test_read_embedded_cli_switches
604 File.open("config.ru", "wb") do |fp|
605 fp.syswrite("#\\ -p #{@port} -o #{@addr}\n")
608 pid = fork { redirect_test_io { exec($unicorn_bin) } }
609 results = retry_hit(["http://#{@addr}:#{@port}/"])
610 assert_equal String, results[0].class
614 def test_config_ru_alt_path
615 config_path = "#{@tmpdir}/foo.ru"
616 File.open(config_path, "wb") { |fp| fp.syswrite(HI) }
620 exec($unicorn_bin, "-l#{@addr}:#{@port}", config_path)
623 results = retry_hit(["http://#{@addr}:#{@port}/"])
624 assert_equal String, results[0].class
629 libdir = "#{@tmpdir}/lib"
630 FileUtils.mkpath([ libdir ])
631 config_path = "#{libdir}/hello.rb"
632 File.open(config_path, "wb") { |fp| fp.syswrite(HELLO) }
636 exec($unicorn_bin, "-l#{@addr}:#{@port}", config_path)
639 results = retry_hit(["http://#{@addr}:#{@port}/"])
640 assert_equal String, results[0].class
645 File.open("config.ru", "wb") { |fp| fp.syswrite(HI) }
646 pid_file = "#{@tmpdir}/test.pid"
649 exec($unicorn_bin, "-l#{@addr}:#{@port}", "-P#{pid_file}")
652 reexec_basic_test(pid, pid_file)
655 def test_reexec_alt_config
656 config_file = "#{@tmpdir}/foo.ru"
657 File.open(config_file, "wb") { |fp| fp.syswrite(HI) }
658 pid_file = "#{@tmpdir}/test.pid"
661 exec($unicorn_bin, "-l#{@addr}:#{@port}", "-P#{pid_file}", config_file)
664 reexec_basic_test(pid, pid_file)
667 def test_socket_unlinked_restore
669 sock = Tempfile.new('unicorn_test_sock')
670 sock_path = sock.path
671 @sockets << sock_path
673 ucfg = Tempfile.new('unicorn_test_config')
674 ucfg.syswrite("listen \"#{sock_path}\"\n")
676 File.open("config.ru", "wb") { |fp| fp.syswrite(HI) }
677 pid = xfork { redirect_test_io { exec($unicorn_bin, "-c#{ucfg.path}") } }
678 wait_for_file(sock_path)
679 assert File.socket?(sock_path)
681 sock = UNIXSocket.new(sock_path)
682 sock.syswrite("GET / HTTP/1.0\r\n\r\n")
683 results = sock.sysread(4096)
685 assert_equal String, results.class
686 File.unlink(sock_path)
687 Process.kill(:HUP, pid)
688 wait_for_file(sock_path)
689 assert File.socket?(sock_path)
691 sock = UNIXSocket.new(sock_path)
692 sock.syswrite("GET / HTTP/1.0\r\n\r\n")
693 results = sock.sysread(4096)
695 assert_equal String, results.class
698 def test_unicorn_config_file
699 pid_file = "#{@tmpdir}/test.pid"
700 sock = Tempfile.new('unicorn_test_sock')
701 sock_path = sock.path
703 @sockets << sock_path
705 log = Tempfile.new('unicorn_test_log')
706 ucfg = Tempfile.new('unicorn_test_config')
707 ucfg.syswrite("listen \"#{sock_path}\"\n")
708 ucfg.syswrite("pid \"#{pid_file}\"\n")
709 ucfg.syswrite("logger Logger.new('#{log.path}')\n")
712 File.open("config.ru", "wb") { |fp| fp.syswrite(HI) }
715 exec($unicorn_bin, "-l#{@addr}:#{@port}",
716 "-P#{pid_file}", "-c#{ucfg.path}")
719 results = retry_hit(["http://#{@addr}:#{@port}/"])
720 assert_equal String, results[0].class
721 wait_master_ready(log.path)
722 assert File.exist?(pid_file), "pid_file created"
723 assert_equal pid, File.read(pid_file).to_i
724 assert File.socket?(sock_path), "socket created"
726 sock = UNIXSocket.new(sock_path)
727 sock.syswrite("GET / HTTP/1.0\r\n\r\n")
728 results = sock.sysread(4096)
730 assert_equal String, results.class
732 # try reloading the config
733 sock = Tempfile.new('new_test_sock')
734 new_sock_path = sock.path
735 @sockets << new_sock_path
737 new_log = Tempfile.new('unicorn_test_log')
739 assert_equal 0, new_log.size
741 ucfg = File.open(ucfg.path, "wb")
742 ucfg.syswrite("listen \"#{sock_path}\"\n")
743 ucfg.syswrite("listen \"#{new_sock_path}\"\n")
744 ucfg.syswrite("pid \"#{pid_file}\"\n")
745 ucfg.syswrite("logger Logger.new('#{new_log.path}')\n")
747 Process.kill(:HUP, pid)
749 wait_for_file(new_sock_path)
750 assert File.socket?(new_sock_path), "socket exists"
751 @sockets.each do |path|
752 sock = UNIXSocket.new(path)
753 sock.syswrite("GET / HTTP/1.0\r\n\r\n")
754 results = sock.sysread(4096)
755 assert_equal String, results.class
758 assert_not_equal 0, new_log.size
759 reexec_usr2_quit_test(pid, pid_file)
762 def test_daemonize_reexec
763 pid_file = "#{@tmpdir}/test.pid"
764 log = Tempfile.new('unicorn_test_log')
765 ucfg = Tempfile.new('unicorn_test_config')
766 ucfg.syswrite("pid \"#{pid_file}\"\n")
767 ucfg.syswrite("logger Logger.new('#{log.path}')\n")
770 File.open("config.ru", "wb") { |fp| fp.syswrite(HI) }
773 exec($unicorn_bin, "-D", "-l#{@addr}:#{@port}", "-c#{ucfg.path}")
776 results = retry_hit(["http://#{@addr}:#{@port}/"])
777 assert_equal String, results[0].class
778 wait_for_file(pid_file)
779 new_pid = File.read(pid_file).to_i
780 assert_not_equal pid, new_pid
781 pid, status = Process.waitpid2(pid)
782 assert status.success?, "original process exited successfully"
783 Process.kill(0, new_pid)
784 reexec_usr2_quit_test(new_pid, pid_file)
787 def test_daemonize_redirect_fail
788 pid_file = "#{@tmpdir}/test.pid"
789 ucfg = Tempfile.new('unicorn_test_config')
790 ucfg.syswrite("pid #{pid_file}\"\n")
791 err = Tempfile.new('stderr')
792 out = Tempfile.new('stdout ')
794 File.open("config.ru", "wb") { |fp| fp.syswrite(HI) }
796 $stderr.reopen(err.path, "a")
797 $stdout.reopen(out.path, "a")
798 exec($unicorn_bin, "-D", "-l#{@addr}:#{@port}", "-c#{ucfg.path}")
800 pid, status = Process.waitpid2(pid)
801 assert ! status.success?, "original process exited successfully"
802 sleep 1 # can't waitpid on a daemonized process :<
803 assert err.stat.size > 0
806 def test_reexec_fd_leak
807 unless RUBY_PLATFORM =~ /linux/ # Solaris may work, too, but I forget...
808 warn "FD leak test only works on Linux at the moment"
811 pid_file = "#{@tmpdir}/test.pid"
812 log = Tempfile.new('unicorn_test_log')
814 ucfg = Tempfile.new('unicorn_test_config')
815 ucfg.syswrite("pid \"#{pid_file}\"\n")
816 ucfg.syswrite("logger Logger.new('#{log.path}')\n")
817 ucfg.syswrite("stderr_path '#{log.path}'\n")
818 ucfg.syswrite("stdout_path '#{log.path}'\n")
821 File.open("config.ru", "wb") { |fp| fp.syswrite(HI) }
824 exec($unicorn_bin, "-D", "-l#{@addr}:#{@port}", "-c#{ucfg.path}")
828 wait_master_ready(log.path)
829 wait_workers_ready(log.path, 1)
830 File.truncate(log.path, 0)
831 wait_for_file(pid_file)
832 orig_pid = pid = File.read(pid_file).to_i
833 orig_fds = `ls -l /proc/#{pid}/fd`.split(/\n/)
835 expect_size = orig_fds.size
837 Process.kill(:USR2, pid)
838 wait_for_file("#{pid_file}.oldbin")
839 Process.kill(:QUIT, pid)
843 wait_master_ready(log.path)
844 wait_workers_ready(log.path, 1)
845 File.truncate(log.path, 0)
846 wait_for_file(pid_file)
847 pid = File.read(pid_file).to_i
848 assert_not_equal orig_pid, pid
849 curr_fds = `ls -l /proc/#{pid}/fd`.split(/\n/)
852 # we could've inherited descriptors the first time around
853 assert expect_size >= curr_fds.size, curr_fds.inspect
854 expect_size = curr_fds.size
856 Process.kill(:USR2, pid)
857 wait_for_file("#{pid_file}.oldbin")
858 Process.kill(:QUIT, pid)
862 wait_master_ready(log.path)
863 wait_workers_ready(log.path, 1)
864 File.truncate(log.path, 0)
865 wait_for_file(pid_file)
866 pid = File.read(pid_file).to_i
867 curr_fds = `ls -l /proc/#{pid}/fd`.split(/\n/)
869 assert_equal expect_size, curr_fds.size, curr_fds.inspect
871 Process.kill(:QUIT, pid)
875 def hup_test_common(preload, check_client=false)
876 File.open("config.ru", "wb") { |fp| fp.syswrite(HI.gsub("HI", '#$$')) }
877 pid_file = Tempfile.new('pid')
878 ucfg = Tempfile.new('unicorn_test_config')
879 ucfg.syswrite("listen '#@addr:#@port'\n")
880 ucfg.syswrite("pid '#{pid_file.path}'\n")
881 ucfg.syswrite("preload_app true\n") if preload
882 ucfg.syswrite("check_client_connection true\n") if check_client
883 ucfg.syswrite("stderr_path 'test_stderr.#$$.log'\n")
884 ucfg.syswrite("stdout_path 'test_stdout.#$$.log'\n")
886 redirect_test_io { exec($unicorn_bin, "-D", "-c", ucfg.path) }
888 _, status = Process.waitpid2(pid)
889 assert status.success?
890 wait_master_ready("test_stderr.#$$.log")
891 wait_workers_ready("test_stderr.#$$.log", 1)
892 uri = URI.parse("http://#@addr:#@port/")
893 pids = Tempfile.new('worker_pids')
898 at_exit { pids.syswrite(bodies.inspect) }
899 trap(:TERM) { exit(0) }
902 rv = Net::HTTP.get(uri)
909 elsif bodies.size > 1
916 assert_equal '1', r.read(1)
917 daemon_pid = File.read(pid_file.path).to_i
918 assert daemon_pid > 0
919 Process.kill(:HUP, daemon_pid)
920 assert_equal '2', r.read(1)
921 Process.kill(:TERM, hitter)
922 _, hitter_status = Process.waitpid2(hitter)
923 assert(hitter_status.success?,
924 "invalid: #{hitter_status.inspect} #{File.read(pids.path)}" \
925 "#{File.read("test_stderr.#$$.log")}")
927 pids = eval(pids.read)
928 assert_kind_of(Hash, pids)
929 assert_equal 2, pids.size
931 assert_kind_of(Integer, x)
935 Process.kill(:QUIT, daemon_pid)
936 wait_for_death(daemon_pid)
939 def test_preload_app_hup
940 hup_test_common(true)
944 hup_test_common(false)
947 def test_check_client_hup
948 hup_test_common(false, true)
951 def test_default_listen_hup_holds_listener
952 default_listen_lock do
953 res, pid_path = default_listen_setup
954 daemon_pid = File.read(pid_path).to_i
955 Process.kill(:HUP, daemon_pid)
956 wait_workers_ready("test_stderr.#$$.log", 1)
957 res2 = hit(["http://#{Unicorn::Const::DEFAULT_LISTEN}/"])
958 assert_match %r{\d+}, res2.first
959 assert res2.first != res.first
960 Process.kill(:QUIT, daemon_pid)
961 wait_for_death(daemon_pid)
965 def test_default_listen_upgrade_holds_listener
966 default_listen_lock do
967 res, pid_path = default_listen_setup
968 daemon_pid = File.read(pid_path).to_i
970 Process.kill(:USR2, daemon_pid)
971 wait_for_file("#{pid_path}.oldbin")
972 wait_for_file(pid_path)
973 Process.kill(:QUIT, daemon_pid)
974 wait_for_death(daemon_pid)
976 daemon_pid = File.read(pid_path).to_i
977 wait_workers_ready("test_stderr.#$$.log", 1)
978 File.truncate("test_stderr.#$$.log", 0)
980 res2 = hit(["http://#{Unicorn::Const::DEFAULT_LISTEN}/"])
981 assert_match %r{\d+}, res2.first
982 assert res2.first != res.first
984 Process.kill(:HUP, daemon_pid)
985 wait_workers_ready("test_stderr.#$$.log", 1)
986 File.truncate("test_stderr.#$$.log", 0)
987 res3 = hit(["http://#{Unicorn::Const::DEFAULT_LISTEN}/"])
988 assert res2.first != res3.first
990 Process.kill(:QUIT, daemon_pid)
991 wait_for_death(daemon_pid)
995 def default_listen_setup
996 File.open("config.ru", "wb") { |fp| fp.syswrite(HI.gsub("HI", '#$$')) }
997 pid_path = (tmp = Tempfile.new('pid')).path
999 ucfg = Tempfile.new('unicorn_test_config')
1000 ucfg.syswrite("pid '#{pid_path}'\n")
1001 ucfg.syswrite("stderr_path 'test_stderr.#$$.log'\n")
1002 ucfg.syswrite("stdout_path 'test_stdout.#$$.log'\n")
1004 redirect_test_io { exec($unicorn_bin, "-D", "-c", ucfg.path) }
1006 _, status = Process.waitpid2(pid)
1007 assert status.success?
1008 wait_master_ready("test_stderr.#$$.log")
1009 wait_workers_ready("test_stderr.#$$.log", 1)
1010 File.truncate("test_stderr.#$$.log", 0)
1011 res = hit(["http://#{Unicorn::Const::DEFAULT_LISTEN}/"])
1012 assert_match %r{\d+}, res.first
1016 # we need to flock() something to prevent these tests from running
1017 def default_listen_lock(&block)
1018 fp = File.open(FLOCK_PATH, "rb")
1020 fp.flock(File::LOCK_EX)
1022 TCPServer.new(Unicorn::Const::DEFAULT_HOST,
1023 Unicorn::Const::DEFAULT_PORT).close
1024 rescue Errno::EADDRINUSE, Errno::EACCES
1025 warn "can't bind to #{Unicorn::Const::DEFAULT_LISTEN}"
1029 # unused_port should never take this, but we may run an environment
1030 # where tests are being run against older unicorns...
1031 lock_path = "#{Dir::tmpdir}/unicorn_test." \
1032 "#{Unicorn::Const::DEFAULT_LISTEN}.lock"
1034 File.open(lock_path, File::WRONLY|File::CREAT|File::EXCL, 0600)
1036 rescue Errno::EEXIST
1040 File.unlink(lock_path) if lock_path
1043 fp.flock(File::LOCK_UN)