BigW Consortium Gitlab

unicorn_spec.rb 2.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
require 'fileutils'

require 'excon'

require 'spec_helper'

describe 'Unicorn' do
  before(:all) do
    config_lines = File.read('config/unicorn.rb.example').split("\n")

    # Remove these because they make setup harder.
    config_lines = config_lines.reject do |line|
      %w[
14 15 16
        working_directory
        worker_processes
        listen
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
        pid
        stderr_path
        stdout_path
      ].any? { |prefix| line.start_with?(prefix) }
    end

    config_lines << "working_directory '#{Rails.root}'"

    # We want to have exactly 1 worker process because that makes it
    # predictable which process will handle our requests.
    config_lines << 'worker_processes 1'

    @socket_path = File.join(Dir.pwd, 'tmp/tests/unicorn.socket')
    config_lines << "listen '#{@socket_path}'"

    ready_file = 'tmp/tests/unicorn-worker-ready'
    FileUtils.rm_f(ready_file)
    after_fork_index = config_lines.index { |l| l.start_with?('after_fork') }
    config_lines.insert(after_fork_index + 1, "File.write('#{ready_file}', Process.pid)")

    config_path = 'tmp/tests/unicorn.rb'
    File.write(config_path, config_lines.join("\n") + "\n")

    cmd = %W[unicorn -E test -c #{config_path} #{Rails.root.join('config.ru')}]
    @unicorn_master_pid = spawn(*cmd)
Jacob Vosmaer committed
42
    wait_unicorn_boot!(@unicorn_master_pid, ready_file)
43 44 45 46 47 48 49 50 51
    WebMock.allow_net_connect!
  end

  %w[SIGQUIT SIGTERM SIGKILL].each do |signal|
    it "has a worker that self-terminates on signal #{signal}" do
      response = Excon.get('unix:///unicorn_test/pid', socket: @socket_path)
      expect(response.status).to eq(200)

      worker_pid = response.body.to_i
Jacob Vosmaer committed
52
      expect(worker_pid).to be > 0
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

      begin
        Excon.post('unix:///unicorn_test/kill', socket: @socket_path, body: "signal=#{signal}")
      rescue Excon::Error::Socket
        # The connection may be closed abruptly
      end

      expect(pid_gone?(worker_pid)).to eq(true)
    end
  end

  after(:all) do
    WebMock.disable_net_connect!(allow_localhost: true)
    Process.kill('TERM', @unicorn_master_pid)
  end

Jacob Vosmaer committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82
  def wait_unicorn_boot!(master_pid, ready_file)
    # Unicorn should boot in under 60 seconds so 120 seconds seems like a good timeout.
    timeout = 120
    timeout.times do
      return if File.exist?(ready_file)
      pid = Process.waitpid(master_pid, Process::WNOHANG)
      raise "unicorn failed to boot: #{$?}" unless pid.nil?

      sleep 1
    end

    raise "unicorn boot timed out after #{timeout} seconds"
  end

83
  def pid_gone?(pid)
Jacob Vosmaer committed
84 85
    # Worker termination should take less than a second. That makes 10
    # seconds a generous timeout.
86 87 88 89 90 91 92 93 94 95 96 97 98
    10.times do
      begin
        Process.kill(0, pid)
      rescue Errno::ESRCH
        return true
      end

      sleep 1
    end

    false
  end
end