forked from sidekiq/sidekiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_web.rb
159 lines (129 loc) · 4.12 KB
/
test_web.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
156
157
158
159
require 'helper'
require 'sidekiq'
require 'sidekiq/web'
require 'rack/test'
class TestWeb < MiniTest::Unit::TestCase
describe 'sidekiq web' do
include Rack::Test::Methods
def app
Sidekiq::Web
end
before do
Sidekiq.redis = REDIS
Sidekiq.redis {|c| c.flushdb }
end
class WebWorker
include Sidekiq::Worker
def perform(a, b)
a + b
end
end
it 'can display home' do
get '/'
assert_equal 200, last_response.status
assert_match /Sidekiq is idle/, last_response.body
refute_match /default/, last_response.body
end
it 'can display queues' do
assert Sidekiq::Client.push('queue' => :foo, 'class' => WebWorker, 'args' => [1, 3])
get '/queues'
assert_equal 200, last_response.status
assert_match /foo/, last_response.body
refute_match /HardWorker/, last_response.body
end
it 'handles missing retry' do
get '/retries/12391982.123'
assert_equal 302, last_response.status
end
it 'handles queue view' do
get '/queues/default'
assert_equal 200, last_response.status
end
it 'can delete a queue' do
Sidekiq.redis do |conn|
conn.rpush('queue:foo', '{}')
conn.sadd('queues', 'foo')
end
get '/queues/foo'
assert_equal 200, last_response.status
post '/queues/foo'
assert_equal 302, last_response.status
Sidekiq.redis do |conn|
refute conn.smembers('queues').include?('foo')
end
end
it 'can display scheduled' do
get '/scheduled'
assert_equal 200, last_response.status
assert_match /found/, last_response.body
refute_match /HardWorker/, last_response.body
add_scheduled
get '/scheduled'
assert_equal 200, last_response.status
refute_match /found/, last_response.body
assert_match /HardWorker/, last_response.body
end
it 'can display retries' do
get '/retries'
assert_equal 200, last_response.status
assert_match /found/, last_response.body
refute_match /HardWorker/, last_response.body
add_retry
get '/retries'
assert_equal 200, last_response.status
refute_match /found/, last_response.body
assert_match /HardWorker/, last_response.body
end
it 'can display a single retry' do
get '/retries/12938712.123333'
assert_equal 302, last_response.status
_, score = add_retry
get "/retries/#{score}"
assert_equal 200, last_response.status
assert_match /HardWorker/, last_response.body
end
it 'can delete a single retry' do
_, score = add_retry
post "/retries/#{score}", 'delete' => 'Delete'
assert_equal 302, last_response.status
assert_equal 'http://example.org/', last_response.header['Location']
get "/retries"
assert_equal 200, last_response.status
refute_match /#{score}/, last_response.body
end
it 'can retry a single retry now' do
msg, score = add_retry
post "/retries/#{score}", 'retry' => 'Retry'
assert_equal 302, last_response.status
assert_equal 'http://example.org/', last_response.header['Location']
get '/queues/default'
assert_equal 200, last_response.status
assert_match /#{msg['args'][2]}/, last_response.body
end
def add_scheduled
msg = { 'class' => 'HardWorker',
'args' => ['bob', 1, Time.now.to_f],
'at' => Time.now.to_f }
score = Time.now.to_f
Sidekiq.redis do |conn|
conn.zadd('schedule', score, score)
conn.rpush("schedule:#{score}", Sidekiq.dump_json(msg))
end
[msg, score]
end
def add_retry
msg = { 'class' => 'HardWorker',
'args' => ['bob', 1, Time.now.to_f],
'queue' => 'default',
'error_message' => 'Some fake message',
'error_class' => 'RuntimeError',
'retry_count' => 0,
'failed_at' => Time.now.utc, }
score = Time.now.to_f
Sidekiq.redis do |conn|
conn.zadd('retry', score, Sidekiq.dump_json(msg))
end
[msg, score]
end
end
end