forked from sidekiq/sidekiq
/
test_launcher.rb
155 lines (120 loc) · 3.74 KB
/
test_launcher.rb
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# frozen_string_literal: true
require_relative 'helper'
require 'sidekiq/launcher'
describe Sidekiq::Launcher do
subject { Sidekiq::Launcher.new(options) }
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
@id = @launcher.identity
Sidekiq::Processor::WORKER_STATE.set('a', {'b' => 1})
@proctitle = $0
end
after do
Sidekiq::Processor::WORKER_STATE.clear
$0 = @proctitle
end
describe '#heartbeat' do
describe 'run' do
it 'sets sidekiq version, tag and the number of busy workers to proctitle' do
subject.heartbeat
assert_equal "sidekiq #{Sidekiq::VERSION} myapp [1 of 3 busy]", $0
end
it 'stores process info in redis' do
subject.heartbeat
workers = Sidekiq.redis { |c| c.hmget(subject.identity, 'busy') }
assert_equal ["1"], workers
expires = Sidekiq.redis { |c| c.pttl(subject.identity) }
assert_in_delta 60000, expires, 500
end
describe 'events' do
before do
@cnt = 0
Sidekiq.on(:heartbeat) do
@cnt += 1
end
end
it 'fires start heartbeat event only once' do
assert_equal 0, @cnt
subject.heartbeat
assert_equal 1, @cnt
subject.heartbeat
assert_equal 1, @cnt
end
end
end
describe 'quiet' do
before do
subject.quiet
end
it 'sets stopping proctitle' do
subject.heartbeat
assert_equal "sidekiq #{Sidekiq::VERSION} myapp [1 of 3 busy] stopping", $0
end
it 'stores process info in redis' do
subject.heartbeat
info = Sidekiq.redis { |c| c.hmget(subject.identity, 'busy') }
assert_equal ["1"], info
expires = Sidekiq.redis { |c| c.pttl(subject.identity) }
assert_in_delta 60000, expires, 50
end
end
it 'fires new heartbeat events' do
i = 0
Sidekiq.on(:heartbeat) do
i += 1
end
assert_equal 0, i
@launcher.heartbeat
assert_equal 1, i
@launcher.heartbeat
assert_equal 1, i
end
describe 'when manager is active' do
before do
Sidekiq::Launcher::PROCTITLES << proc { "xyz" }
@launcher.heartbeat
Sidekiq::Launcher::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(@id, 'busy') }
assert_equal ["1"], info
expires = Sidekiq.redis { |c| c.pttl(@id) }
assert_in_delta 60000, expires, 500
end
end
end
describe 'when manager is stopped' do
before do
@launcher.quiet
@launcher.heartbeat
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(@id, 'busy') }
assert_equal ["1"], info
expires = Sidekiq.redis { |c| c.pttl(@id) }
assert_in_delta 60000, expires, 50
end
end
end
def options
{ :concurrency => 3, :queues => ['default'], :tag => 'myapp' }
end
end