2013-03-19 18:54:55 -04:00
|
|
|
|
# encoding: utf-8
|
2016-11-22 23:39:00 -05:00
|
|
|
|
# frozen_string_literal: true
|
2014-12-30 15:54:58 -05:00
|
|
|
|
require_relative 'helper'
|
2012-12-02 18:55:21 -05:00
|
|
|
|
|
2013-09-22 17:38:33 -04:00
|
|
|
|
class TestSidekiq < Sidekiq::Test
|
2012-12-02 18:55:21 -05:00
|
|
|
|
describe 'json processing' do
|
2015-09-22 15:48:43 -04:00
|
|
|
|
it 'handles json' do
|
|
|
|
|
assert_equal({"foo" => "bar"}, Sidekiq.load_json("{\"foo\":\"bar\"}"))
|
2012-12-02 18:55:21 -05:00
|
|
|
|
assert_equal "{\"foo\":\"bar\"}", Sidekiq.dump_json({ "foo" => "bar" })
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2013-02-02 05:47:58 -05:00
|
|
|
|
describe "redis connection" do
|
|
|
|
|
it "returns error without creating a connection if block is not given" do
|
2015-09-22 15:48:43 -04:00
|
|
|
|
assert_raises(ArgumentError) do
|
2013-02-02 05:47:58 -05:00
|
|
|
|
Sidekiq.redis
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2013-03-19 18:54:55 -04:00
|
|
|
|
describe "❨╯°□°❩╯︵┻━┻" do
|
|
|
|
|
before { $stdout = StringIO.new }
|
|
|
|
|
after { $stdout = STDOUT }
|
|
|
|
|
|
|
|
|
|
it "allows angry developers to express their emotional constitution and remedies it" do
|
|
|
|
|
Sidekiq.❨╯°□°❩╯︵┻━┻
|
2015-04-21 13:08:30 -04:00
|
|
|
|
assert_equal "Calm down, yo.\n", $stdout.string
|
2013-03-19 18:54:55 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
2014-03-10 23:46:19 -04:00
|
|
|
|
|
|
|
|
|
describe 'lifecycle events' do
|
|
|
|
|
it 'handles invalid input' do
|
2014-05-13 17:00:20 -04:00
|
|
|
|
Sidekiq.options[:lifecycle_events][:startup].clear
|
|
|
|
|
|
2014-03-10 23:46:19 -04:00
|
|
|
|
e = assert_raises ArgumentError do
|
2014-03-12 00:21:21 -04:00
|
|
|
|
Sidekiq.on(:startp)
|
2014-03-10 23:46:19 -04:00
|
|
|
|
end
|
2014-03-19 20:41:11 -04:00
|
|
|
|
assert_match(/Invalid event name/, e.message)
|
2014-03-10 23:46:19 -04:00
|
|
|
|
e = assert_raises ArgumentError do
|
2014-03-12 00:21:21 -04:00
|
|
|
|
Sidekiq.on('startup')
|
2014-03-10 23:46:19 -04:00
|
|
|
|
end
|
2014-03-19 20:41:11 -04:00
|
|
|
|
assert_match(/Symbols only/, e.message)
|
2014-03-12 00:21:21 -04:00
|
|
|
|
Sidekiq.on(:startup) do
|
2014-03-10 23:51:10 -04:00
|
|
|
|
1 + 1
|
|
|
|
|
end
|
|
|
|
|
|
2014-03-12 00:21:21 -04:00
|
|
|
|
assert_equal 2, Sidekiq.options[:lifecycle_events][:startup].first.call
|
2014-03-10 23:46:19 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
2015-01-09 12:08:58 -05:00
|
|
|
|
|
|
|
|
|
describe 'default_worker_options' do
|
2015-09-22 15:48:43 -04:00
|
|
|
|
it 'stringifies keys' do
|
2015-01-09 12:08:58 -05:00
|
|
|
|
@old_options = Sidekiq.default_worker_options
|
2015-09-22 15:48:43 -04:00
|
|
|
|
begin
|
|
|
|
|
Sidekiq.default_worker_options = { queue: 'cat'}
|
|
|
|
|
assert_equal 'cat', Sidekiq.default_worker_options['queue']
|
|
|
|
|
ensure
|
|
|
|
|
Sidekiq.default_worker_options = @old_options
|
|
|
|
|
end
|
2015-01-09 12:08:58 -05:00
|
|
|
|
end
|
|
|
|
|
end
|
2015-07-10 15:35:02 -04:00
|
|
|
|
|
|
|
|
|
describe 'error handling' do
|
|
|
|
|
it 'deals with user-specified error handlers which raise errors' do
|
2015-07-12 13:51:34 -04:00
|
|
|
|
output = capture_logging do
|
2015-07-14 00:51:53 -04:00
|
|
|
|
begin
|
|
|
|
|
Sidekiq.error_handlers << proc {|x, hash|
|
|
|
|
|
raise 'boom'
|
|
|
|
|
}
|
|
|
|
|
cli = Sidekiq::CLI.new
|
|
|
|
|
cli.handle_exception(RuntimeError.new("hello"))
|
|
|
|
|
ensure
|
|
|
|
|
Sidekiq.error_handlers.pop
|
|
|
|
|
end
|
2015-07-12 13:51:34 -04:00
|
|
|
|
end
|
|
|
|
|
assert_includes output, "boom"
|
|
|
|
|
assert_includes output, "ERROR"
|
2015-07-10 15:35:02 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
2015-09-11 13:55:45 -04:00
|
|
|
|
|
|
|
|
|
describe 'redis connection' do
|
|
|
|
|
it 'does not continually retry' do
|
|
|
|
|
assert_raises Redis::CommandError do
|
|
|
|
|
Sidekiq.redis do |c|
|
|
|
|
|
raise Redis::CommandError, "READONLY You can't write against a read only slave."
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'reconnects if connection is flagged as readonly' do
|
|
|
|
|
counts = []
|
|
|
|
|
Sidekiq.redis do |c|
|
|
|
|
|
counts << c.info['total_connections_received'].to_i
|
|
|
|
|
raise Redis::CommandError, "READONLY You can't write against a read only slave." if counts.size == 1
|
|
|
|
|
end
|
|
|
|
|
assert_equal 2, counts.size
|
|
|
|
|
assert_equal counts[0] + 1, counts[1]
|
|
|
|
|
end
|
|
|
|
|
end
|
2016-02-25 17:29:41 -05:00
|
|
|
|
|
|
|
|
|
describe 'redis info' do
|
|
|
|
|
it 'calls the INFO command which returns at least redis_version' do
|
|
|
|
|
output = Sidekiq.redis_info
|
|
|
|
|
assert_includes output.keys, "redis_version"
|
|
|
|
|
end
|
|
|
|
|
end
|
2013-09-22 17:38:33 -04:00
|
|
|
|
end
|