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
|
require './test/test_helper'
describe 'Cron web' do
include Rack::Test::Methods
TOKEN = SecureRandom.base64(32).freeze
def app
Sidekiq::Web
end
before do
env 'rack.session', { csrf: TOKEN }
env 'HTTP_X_CSRF_TOKEN', TOKEN
REDIS.with { |c| c.respond_to?(:redis) ? c.redis.flushdb : c.flushdb }
Sidekiq.redis = REDIS
end
let(:job_name) { "TestNameOfCronJob" }
let(:cron_job_name) { "TesQueueNameOfCronJob" }
let(:args) do
{
name: job_name,
cron: "*/2 * * * *",
klass: "CronTestClass"
}
end
let(:cron_args) do
{
name: cron_job_name,
cron: "*/2 * * * *",
klass: "CronQueueTestClass",
queue: "cron"
}
end
it 'display cron web' do
get '/cron'
assert_equal 200, last_response.status
end
it 'display cron web with message - no cron jobs' do
get '/cron'
assert last_response.body.include?('No cron jobs were found')
end
it 'display cron web with cron jobs table' do
Sidekiq::Cron::Job.create(args)
get '/cron'
assert_equal 200, last_response.status
refute last_response.body.include?('No cron jobs were found')
assert last_response.body.include?('table')
assert last_response.body.include?("TestNameOfCronJob")
end
describe "work with cron job" do
before do
@job = Sidekiq::Cron::Job.new(args.merge(status: "enabled"))
assert @job.save
@cron_job = Sidekiq::Cron::Job.new(cron_args.merge(status: "enabled"))
assert @cron_job.save
end
it 'shows history of a cron job' do
@job.enque!
get "/cron/#{job_name}"
jid =
Sidekiq.redis do |conn|
history = conn.lrange Sidekiq::Cron::Job.jid_history_key(job_name), 0, -1
Sidekiq.load_json(history.last)['jid']
end
assert jid
assert last_response.body.include?(jid)
end
it 'redirects to cron path when name not found' do
get '/cron/some-fake-name'
assert_match %r{\/cron\z}, last_response['Location']
end
it "disable and enable all cron jobs" do
post "/cron/__all__/disable"
assert_equal Sidekiq::Cron::Job.find(job_name).status, "disabled"
post "/cron/__all__/enable"
assert_equal Sidekiq::Cron::Job.find(job_name).status, "enabled"
end
it "disable and enable cron job" do
post "/cron/#{job_name}/disable"
assert_equal Sidekiq::Cron::Job.find(job_name).status, "disabled"
post "/cron/#{job_name}/enable"
assert_equal Sidekiq::Cron::Job.find(job_name).status, "enabled"
end
it "enqueue all jobs" do
Sidekiq.redis do |conn|
assert_equal 0, conn.llen("queue:default"), "Queue should have no jobs"
end
post "/cron/__all__/enque"
Sidekiq.redis do |conn|
assert_equal 1, conn.llen("queue:default"), "Queue should have 1 job in default"
assert_equal 1, conn.llen("queue:cron"), "Queue should have 1 job in cron"
end
end
it "enqueue job" do
Sidekiq.redis do |conn|
assert_equal 0, conn.llen("queue:default"), "Queue should have no jobs"
end
post "/cron/#{job_name}/enque"
Sidekiq.redis do |conn|
assert_equal 1, conn.llen("queue:default"), "Queue should have 1 job"
end
# Should enqueue more times.
post "/cron/#{job_name}/enque"
Sidekiq.redis do |conn|
assert_equal 2, conn.llen("queue:default"), "Queue should have 2 job"
end
# Should enqueue to cron job queue.
post "/cron/#{cron_job_name}/enque"
Sidekiq.redis do |conn|
assert_equal 1, conn.llen("queue:cron"), "Queue should have 1 cron job"
end
end
it "destroy job" do
assert_equal Sidekiq::Cron::Job.all.size, 2, "Should have 2 job"
post "/cron/#{job_name}/delete"
post "/cron/#{cron_job_name}/delete"
assert_equal Sidekiq::Cron::Job.all.size, 0, "Should have zero jobs"
end
it "destroy all jobs" do
assert_equal Sidekiq::Cron::Job.all.size, 2, "Should have 2 job"
post "/cron/__all__/delete"
assert_equal Sidekiq::Cron::Job.all.size, 0, "Should have zero jobs"
end
end
end
|