2013-03-19 23:54:55 +01:00
|
|
|
|
# encoding: utf-8
|
2014-12-30 12:54:58 -08:00
|
|
|
|
require_relative 'helper'
|
2012-12-02 18:55:21 -05:00
|
|
|
|
|
2013-09-22 14:38:33 -07:00
|
|
|
|
class TestSidekiq < Sidekiq::Test
|
2012-12-02 18:55:21 -05:00
|
|
|
|
describe 'json processing' do
|
2015-09-22 12:48:43 -07: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 12:47:58 +02:00
|
|
|
|
describe "redis connection" do
|
|
|
|
|
it "returns error without creating a connection if block is not given" do
|
2015-09-22 12:48:43 -07:00
|
|
|
|
assert_raises(ArgumentError) do
|
2013-02-02 12:47:58 +02:00
|
|
|
|
Sidekiq.redis
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2013-03-19 23:54:55 +01: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 23:54:55 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
2014-03-10 20:46:19 -07:00
|
|
|
|
|
|
|
|
|
describe 'lifecycle events' do
|
|
|
|
|
it 'handles invalid input' do
|
2014-05-13 14:00:20 -07:00
|
|
|
|
Sidekiq.options[:lifecycle_events][:startup].clear
|
|
|
|
|
|
2014-03-10 20:46:19 -07:00
|
|
|
|
e = assert_raises ArgumentError do
|
2014-03-11 21:21:21 -07:00
|
|
|
|
Sidekiq.on(:startp)
|
2014-03-10 20:46:19 -07:00
|
|
|
|
end
|
2014-03-19 17:41:11 -07:00
|
|
|
|
assert_match(/Invalid event name/, e.message)
|
2014-03-10 20:46:19 -07:00
|
|
|
|
e = assert_raises ArgumentError do
|
2014-03-11 21:21:21 -07:00
|
|
|
|
Sidekiq.on('startup')
|
2014-03-10 20:46:19 -07:00
|
|
|
|
end
|
2014-03-19 17:41:11 -07:00
|
|
|
|
assert_match(/Symbols only/, e.message)
|
2014-03-11 21:21:21 -07:00
|
|
|
|
Sidekiq.on(:startup) do
|
2014-03-10 20:51:10 -07:00
|
|
|
|
1 + 1
|
|
|
|
|
end
|
|
|
|
|
|
2014-03-11 21:21:21 -07:00
|
|
|
|
assert_equal 2, Sidekiq.options[:lifecycle_events][:startup].first.call
|
2014-03-10 20:46:19 -07:00
|
|
|
|
end
|
|
|
|
|
end
|
2015-01-09 17:08:58 +00:00
|
|
|
|
|
|
|
|
|
describe 'default_worker_options' do
|
2015-09-22 12:48:43 -07:00
|
|
|
|
it 'stringifies keys' do
|
2015-01-09 17:08:58 +00:00
|
|
|
|
@old_options = Sidekiq.default_worker_options
|
2015-09-22 12:48:43 -07: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 17:08:58 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
2015-07-10 12:35:02 -07:00
|
|
|
|
|
|
|
|
|
describe 'error handling' do
|
|
|
|
|
it 'deals with user-specified error handlers which raise errors' do
|
2015-07-12 10:51:34 -07:00
|
|
|
|
output = capture_logging do
|
2015-07-13 21:51:53 -07: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 10:51:34 -07:00
|
|
|
|
end
|
|
|
|
|
assert_includes output, "boom"
|
|
|
|
|
assert_includes output, "ERROR"
|
2015-07-10 12:35:02 -07:00
|
|
|
|
end
|
|
|
|
|
end
|
2015-09-11 10:55:45 -07: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
|
2013-09-22 14:38:33 -07:00
|
|
|
|
end
|