2016-11-22 23:39:00 -05:00
# frozen_string_literal: true
2022-03-03 12:50:03 -08:00
require_relative " helper "
2012-12-02 18:55:21 -05:00
2019-02-28 12:43:50 -08:00
describe Sidekiq do
2022-05-31 13:37:31 -07:00
before do
2022-08-25 10:15:11 -07:00
@config = reset!
2022-05-31 13:37:31 -07:00
end
2022-03-03 12:50:03 -08:00
describe " json processing " do
it " handles json " do
2015-09-22 12:48:43 -07:00
assert_equal ( { " foo " = > " bar " } , Sidekiq . load_json ( " { \" foo \" : \" bar \" } " ) )
2022-03-03 12:50:03 -08:00
assert_equal " { \" foo \" : \" bar \" } " , Sidekiq . dump_json ( { " foo " = > " bar " } )
2012-12-02 18:55:21 -05:00
end
end
2013-02-02 12:47:58 +02:00
describe " redis connection " do
2022-03-03 12:50:03 -08:00
it " returns error without creating a connection if block is not given " do
assert_raises ( ArgumentError ) do
2022-05-31 13:37:31 -07:00
@config . redis
2013-02-02 12:47:58 +02:00
end
2022-03-03 12:50:03 -08:00
end
2013-02-02 12:47:58 +02:00
end
2022-03-03 12:50:03 -08:00
describe " lifecycle events " do
it " handles invalid input " do
2022-08-25 10:15:11 -07:00
@config [ :lifecycle_events ] [ :startup ] . clear
2014-05-13 14:00:20 -07:00
2014-03-10 20:46:19 -07:00
e = assert_raises ArgumentError do
2022-08-25 10:15:11 -07:00
@config . 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
2022-08-25 10:15:11 -07:00
@config . 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 )
2022-08-25 10:15:11 -07:00
@config . on ( :startup ) do
2014-03-10 20:51:10 -07:00
1 + 1
end
2022-08-25 10:15:11 -07:00
assert_equal 2 , @config [ :lifecycle_events ] [ :startup ] . first . call
2014-03-10 20:46:19 -07:00
end
end
2015-01-09 17:08:58 +00:00
2022-03-03 12:50:03 -08:00
describe " default_job_options " do
it " stringifies keys " do
2022-08-25 10:15:11 -07:00
@old_options = Sidekiq . default_job_options
2015-09-22 12:48:43 -07:00
begin
2022-08-25 10:15:11 -07:00
Sidekiq . default_job_options = { queue : " cat " }
assert_equal " cat " , Sidekiq . default_job_options [ " queue " ]
2015-09-22 12:48:43 -07:00
ensure
2022-08-25 10:15:11 -07:00
Sidekiq . default_job_options = @old_options
2015-09-22 12:48:43 -07:00
end
2015-01-09 17:08:58 +00:00
end
end
2015-07-10 12:35:02 -07:00
2022-03-03 12:50:03 -08:00
describe " error handling " do
it " deals with user-specified error handlers which raise errors " do
2022-08-25 10:15:11 -07:00
output = capture_logging ( @config ) do
2022-05-31 13:37:31 -07:00
@config . error_handlers << proc { | x , hash |
2022-03-03 12:50:03 -08:00
raise " boom "
}
2022-05-31 13:37:31 -07:00
@config . handle_exception ( RuntimeError . new ( " hello " ) )
2022-03-03 12:50:03 -08:00
ensure
2022-05-31 13:37:31 -07:00
@config . error_handlers . pop
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
2022-03-03 12:50:03 -08:00
describe " redis connection " do
it " does not continually retry " do
2022-08-25 10:15:11 -07:00
assert_raises Sidekiq :: RedisClientAdapter :: CommandError do
2022-05-31 13:37:31 -07:00
@config . redis do | c |
2022-08-25 10:15:11 -07:00
raise Sidekiq :: RedisClientAdapter :: CommandError , " READONLY You can't write against a replica. "
2015-09-11 10:55:45 -07:00
end
end
end
2022-03-03 12:50:03 -08:00
it " reconnects if connection is flagged as readonly " do
2015-09-11 10:55:45 -07:00
counts = [ ]
2022-05-31 13:37:31 -07:00
@config . redis do | c |
2022-03-03 12:50:03 -08:00
counts << c . info [ " total_connections_received " ] . to_i
2022-08-25 10:15:11 -07:00
raise Sidekiq :: RedisClientAdapter :: CommandError , " READONLY You can't write against a replica. " if counts . size == 1
2015-09-11 10:55:45 -07:00
end
assert_equal 2 , counts . size
assert_equal counts [ 0 ] + 1 , counts [ 1 ]
end
2021-09-08 00:04:01 +02:00
2022-03-03 12:50:03 -08:00
it " reconnects if instance state changed " do
2021-09-08 00:04:01 +02:00
counts = [ ]
2022-05-31 13:37:31 -07:00
@config . redis do | c |
2022-03-03 12:50:03 -08:00
counts << c . info [ " total_connections_received " ] . to_i
2022-08-25 10:15:11 -07:00
raise Sidekiq :: RedisClientAdapter :: CommandError , " UNBLOCKED force unblock from blocking operation, instance state changed (master -> replica?) " if counts . size == 1
2021-09-08 00:04:01 +02:00
end
assert_equal 2 , counts . size
assert_equal counts [ 0 ] + 1 , counts [ 1 ]
end
2015-09-11 10:55:45 -07:00
end
2016-02-25 23:29:41 +01:00
2022-03-03 12:50:03 -08:00
describe " redis info " do
it " calls the INFO command which returns at least redis_version " do
2022-05-31 13:37:31 -07:00
output = @config . redis_info
2016-02-25 23:29:41 +01:00
assert_includes output . keys , " redis_version "
end
end
2013-09-22 14:38:33 -07:00
end