Skip to content

Commit

Permalink
Remove [deprecated SETEX redis command](https://redis.io/commands/s…
Browse files Browse the repository at this point in the history
…etex/) from Sidekiq probe (#4)

Add support for RedisClient API

Co-authored-by: kalinkin.ve <vadim.kalinkin@sbermarket.ru>
  • Loading branch information
pseudoclaws and kalinkin.ve committed Aug 16, 2023
1 parent f07e85d commit d94216c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Gemfile.lock
Expand Up @@ -15,6 +15,7 @@ GEM
tzinfo (~> 1.1)
ast (2.4.2)
concurrent-ruby (1.1.10)
connection_pool (2.4.1)
diff-lcs (1.5.0)
docile (1.4.0)
dotenv (2.7.6)
Expand All @@ -28,6 +29,8 @@ GEM
rainbow (3.1.1)
rake (13.0.6)
redis (4.2.5)
redis-client (0.15.0)
connection_pool
regexp_parser (2.5.0)
rexml (3.2.5)
rspec (3.11.0)
Expand Down Expand Up @@ -84,6 +87,7 @@ DEPENDENCIES
http_health_check!
rake (~> 13.0)
redis (~> 4.2.5)
redis-client (~> 0.15.0)
rspec (~> 3.2)
rspec_junit_formatter
rubocop (~> 0.81)
Expand Down
1 change: 1 addition & 0 deletions http_health_check.gemspec
Expand Up @@ -33,6 +33,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'activesupport', '~> 5.0'
spec.add_development_dependency 'dotenv', '~> 2.7.6'
spec.add_development_dependency 'redis', '~> 4.2.5'
spec.add_development_dependency 'redis-client', '~> 0.15.0'
spec.add_development_dependency 'rspec', '~> 3.2'
spec.add_development_dependency 'rubocop', '~> 0.81'
spec.add_development_dependency 'thor', '>= 0.20'
Expand Down
2 changes: 1 addition & 1 deletion lib/http_health_check/probes/sidekiq.rb
Expand Up @@ -14,7 +14,7 @@ def initialize(sidekiq: ::Sidekiq)

def probe(_env)
@sidekiq_module.redis do |conn|
conn.setex(meta[:redis_key], TTL_SEC, MAGIC_NUMBER)
conn.call('SET', meta[:redis_key], MAGIC_NUMBER, 'EX', TTL_SEC)
probe_ok
end
end
Expand Down
53 changes: 45 additions & 8 deletions spec/http_health_check/probes/sidekiq_spec.rb
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'redis'
require 'redis-client'
module Sidekiq; end
require_relative '../../../lib/http_health_check/probes/sidekiq'

Expand All @@ -20,28 +21,64 @@ def redis

let(:sidekiq) { fake_sidekiq.new(redis) }

context 'when redis is available', redis: true do
let(:redis) { Redis.new(url: ENV.fetch('REDIS_URL', 'redis://127.0.0.1:6379/1')) }
shared_context :connected_redis do
let(:redis_url) { ENV.fetch('REDIS_URL', 'redis://127.0.0.1:6379/1') }
end

shared_context :disconnected_redis do
let(:redis_url) { 'redis://127.0.0.1:63799/999' }
end

shared_examples :positive_probe do
it 'writes temporary key into redis and returns positive result' do
result = subject.call(nil)
expect(result).to be_ok

expect(redis.get(result.meta[:redis_key]).to_i).to eq(described_class::MAGIC_NUMBER)
expect(redis.call('GET', result.meta[:redis_key]).to_i).to eq(described_class::MAGIC_NUMBER)

ttl = redis.ttl(result.meta[:redis_key]).to_i
ttl = redis.call('TTL', result.meta[:redis_key]).to_i
expect(ttl).to be <= described_class::TTL_SEC
expect(ttl).to be > 0
end
end

context 'when redis is not available', redis: true do
let(:redis) { Redis.new(url: 'redis://127.0.0.1:63799/999') }

shared_examples :negative_probe do
it 'returns an error' do
result = subject.call(nil)
expect(result).not_to be_ok
expect(result.meta[:error_class]).to eq('Redis::CannotConnectError')
expect(result.meta[:error_class].split('::').last).to eq('CannotConnectError')
end
end

context 'with redis' do
let(:redis) { Redis.new(url: redis_url) }

context 'when server is available', redis: true do
include_context :connected_redis

it_behaves_like :positive_probe
end

context 'when server is not available', redis: true do
include_context :disconnected_redis

it_behaves_like :negative_probe
end
end

context 'with redis-client' do
let(:redis) { RedisClient.new(url: redis_url) }

context 'when server is available', redis: true do
include_context :connected_redis

it_behaves_like :positive_probe
end

context 'when redis-client is not available', redis: true do
include_context :disconnected_redis

it_behaves_like :negative_probe
end
end
end

0 comments on commit d94216c

Please sign in to comment.