File: puma_worker_killer_test.rb

package info (click to toggle)
ruby-puma-worker-killer 0.3.1-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 228 kB
  • sloc: ruby: 283; makefile: 4
file content (104 lines) | stat: -rw-r--r-- 4,044 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

require 'test_helper'

class PumaWorkerKillerTest < Test::Unit::TestCase
  def test_starts
    port     = 0 # http://stackoverflow.com/questions/200484/how-do-you-find-a-free-tcp-server-port-using-ruby
    command  = "#{RbConfig.ruby} /usr/bin/puma #{fixture_path.join("default.ru")} -t 1:1 -w 2 --preload --debug -p #{port}"
    options  = { wait_for: 'booted', timeout: 5, env: { 'PUMA_FREQUENCY' => 1 } }

    WaitForIt.new(command, options) do |spawn|
      assert_contains(spawn, 'PumaWorkerKiller')
    end
  end

  def test_without_preload
    port     = 0 # http://stackoverflow.com/questions/200484/how-do-you-find-a-free-tcp-server-port-using-ruby
    command  = "#{RbConfig.ruby} /usr/bin/puma #{fixture_path.join("default.ru")} -t 1:1 -w 2 --debug -p #{port} -C #{fixture_path.join("config/puma_worker_killer_start.rb")}"
    options  = { wait_for: 'booted', timeout: 10, env: { 'PUMA_FREQUENCY' => 1 } }

    WaitForIt.new(command, options) do |spawn|
      assert_contains(spawn, 'PumaWorkerKiller')
    end
  end

  def test_kills_large_app
    file     = fixture_path.join('big.ru')
    port     = 0
    command  = "#{RbConfig.ruby} /usr/bin/puma #{file} -t 1:1 -w 2 --preload --debug -p #{port}"
    options  = { wait_for: 'booted', timeout: 5, env: { 'PUMA_FREQUENCY' => 1, 'PUMA_RAM' => 1 } }

    WaitForIt.new(command, options) do |spawn|
      assert_contains(spawn, 'Out of memory')
    end
  end

  def test_pre_term
    file     = fixture_path.join('pre_term.ru')
    port     = 0
    command  = "#{RbConfig.ruby} /usr/bin/puma #{file} -t 1:1 -w 2 --preload --debug -p #{port}"
    options  = { wait_for: 'booted', timeout: 5, env: { 'PUMA_FREQUENCY' => 1, 'PUMA_RAM' => 1 } }

    WaitForIt.new(command, options) do |spawn|
      assert_contains(spawn, 'Out of memory')
      assert_contains(spawn, 'About to terminate worker:') # defined in pre_term.ru
    end
  end

  def test_on_calculation
    file     = fixture_path.join('on_calculation.ru')
    port     = 0
    command  = "#{RbConfig.ruby} /usr/bin/puma #{file} -t 1:1 -w 2 --preload --debug -p #{port}"
    options  = { wait_for: 'booted', timeout: 5, env: { 'PUMA_FREQUENCY' => 1, 'PUMA_RAM' => 1 } }

    WaitForIt.new(command, options) do |spawn|
      assert_contains(spawn, 'Out of memory')
      assert_contains(spawn, 'Current memory footprint:') # defined in on_calculate.ru
    end
  end

  def assert_contains(spawn, string)
    assert spawn.wait(string), "Expected logs to contain '#{string}' but it did not, contents: #{spawn.log.read}"
  end

  def test_rolling_restart
    file     = fixture_path.join('rolling_restart.ru')
    port     = 0
    command  = "#{RbConfig.ruby} /usr/bin/puma #{file} -t 1:1 -w 2 --preload --debug -p #{port}"
    puts command.inspect
    options = { wait_for: 'booted', timeout: 15, env: {} }

    WaitForIt.new(command, options) do |spawn|
      assert_contains(spawn, 'Rolling Restart')
    end
  end

  def test_rolling_restart_worker_kill_check
    file     = fixture_path.join('rolling_restart.ru')
    port     = 0
    command  = "#{RbConfig.ruby} /usr/bin/puma #{file} -t 1:1 -w 1 --preload --debug -p #{port}"
    puts command.inspect
    options = { wait_for: 'booted', timeout: 120, env: {} }

    WaitForIt.new(command, options) do |spawn|
      # at least 2 matches for TERM (so we set a timeout value longer - 120sec)
      spawn.wait!(/TERM.*TERM/m)
      term_ids = spawn.log.read.scan(/TERM to pid (\d*)/)
      assert term_ids.sort == term_ids.uniq.sort
    end
  end

  def test_rolling_pre_term
    file     = fixture_path.join('rolling_pre_term.ru')
    port     = 0
    command  = "#{RbConfig.ruby} /usr/bin/puma #{file} -t 1:1 -w 2 --preload --debug -p #{port}"
    puts command.inspect
    options = { wait_for: 'booted', timeout: 15, env: {} }

    WaitForIt.new(command, options) do |spawn|
      assert_contains(spawn, 'Rolling Restart')
      assert_contains(spawn, 'About to terminate (rolling) worker:') # defined in rolling_pre_term.ru
    end
  end
end