File: test_launcher.rb

package info (click to toggle)
ruby-sidekiq 4.2.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,064 kB
  • ctags: 754
  • sloc: ruby: 7,384; makefile: 26; sh: 4
file content (95 lines) | stat: -rw-r--r-- 2,686 bytes parent folder | download
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
# frozen_string_literal: true
require_relative 'helper'
require 'sidekiq/launcher'

class TestLauncher < Sidekiq::Test

  describe 'launcher' do
    before do
      Sidekiq.redis {|c| c.flushdb }
    end

    def new_manager(opts)
      Sidekiq::Manager.new(opts)
    end

    describe 'heartbeat' do
      before do
        @mgr = new_manager(options)
        @launcher = Sidekiq::Launcher.new(options)
        @launcher.manager = @mgr

        Sidekiq::Processor::WORKER_STATE['a'] = {'b' => 1}

        @proctitle = $0
      end

      after do
        Sidekiq::Processor::WORKER_STATE.clear
        $0 = @proctitle
      end

      it 'fires new heartbeat events' do
        i = 0
        Sidekiq.on(:heartbeat) do
          i += 1
        end
        assert_equal 0, i
        @launcher.heartbeat('identity', heartbeat_data, Sidekiq.dump_json(heartbeat_data))
        assert_equal 1, i
        @launcher.heartbeat('identity', heartbeat_data, Sidekiq.dump_json(heartbeat_data))
        assert_equal 1, i
      end

      describe 'when manager is active' do
        before do
          Sidekiq::CLI::PROCTITLES << proc { "xyz" }
          @launcher.heartbeat('identity', heartbeat_data, Sidekiq.dump_json(heartbeat_data))
          Sidekiq::CLI::PROCTITLES.pop
        end

        it 'sets useful info to proctitle' do
          assert_equal "sidekiq #{Sidekiq::VERSION} myapp [1 of 3 busy] xyz", $0
        end

        it 'stores process info in redis' do
          info = Sidekiq.redis { |c| c.hmget('identity', 'busy') }
          assert_equal ["1"], info
          expires = Sidekiq.redis { |c| c.pttl('identity') }
          assert_in_delta 60000, expires, 500
        end
      end

      describe 'when manager is stopped' do
        before do
          @launcher.quiet
          @launcher.heartbeat('identity', heartbeat_data, Sidekiq.dump_json(heartbeat_data))
        end

        #after do
          #puts system('redis-cli -n 15 keys  "*" | while read LINE ; do TTL=`redis-cli -n 15 ttl "$LINE"`; if [ "$TTL" -eq -1 ]; then echo "$LINE"; fi; done;')
        #end

        it 'indicates stopping status in proctitle' do
          assert_equal "sidekiq #{Sidekiq::VERSION} myapp [1 of 3 busy] stopping", $0
        end

        it 'stores process info in redis' do
          info = Sidekiq.redis { |c| c.hmget('identity', 'busy') }
          assert_equal ["1"], info
          expires = Sidekiq.redis { |c| c.pttl('identity') }
          assert_in_delta 60000, expires, 50
        end
      end
    end

    def options
      { :concurrency => 3, :queues => ['default'] }
    end

    def heartbeat_data
      { 'concurrency' => 3, 'tag' => 'myapp' }
    end
  end

end