From 10b28bc3ea64afbb6f686702353419d5b9718d30 Mon Sep 17 00:00:00 2001 From: TiuSh Date: Wed, 4 Oct 2023 20:14:16 -0400 Subject: [PATCH] DEVOPS-481 Add support for redis_client option --- README.md | 2 ++ lib/potlock/client.rb | 14 +++++++++----- lib/potlock/configuration.rb | 13 +++++++------ spec/configuration_spec.rb | 8 ++++++++ spec/potlock_spec.rb | 4 ++++ 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 9a34a6d..2f53f1d 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,8 @@ Potlock.configure do |config| config.redis_host = "localhost" config.redis_port = "6379" config.redis_db = "1" + # or + # config.redis_client = Redis.new(host: "localhost", port: 6379, db: 1) end ``` diff --git a/lib/potlock/client.rb b/lib/potlock/client.rb index 1bc65f0..4cbe542 100644 --- a/lib/potlock/client.rb +++ b/lib/potlock/client.rb @@ -58,11 +58,15 @@ def retry_delay end def redis - @redis ||= Redis.new( - host: Potlock.configuration.redis_host, - db: Potlock.configuration.redis_db, - port: Potlock.configuration.redis_port, - ) + @redis ||= begin + return Potlock.configuration.redis_client unless Potlock.configuration.redis_client.nil? + + Redis.new( + host: Potlock.configuration.redis_host, + db: Potlock.configuration.redis_db, + port: Potlock.configuration.redis_port, + ) + end end end end diff --git a/lib/potlock/configuration.rb b/lib/potlock/configuration.rb index bcb5333..7d62a81 100644 --- a/lib/potlock/configuration.rb +++ b/lib/potlock/configuration.rb @@ -3,7 +3,7 @@ module Potlock class Configuration # Redis connection information - attr_accessor :redis_host, :redis_port, :redis_db + attr_accessor :redis_client, :redis_host, :redis_port, :redis_db # How many times it'll try to lock a resource attr_accessor :retry_count @@ -12,11 +12,12 @@ class Configuration attr_accessor :retry_delay def initialize - @redis_host = "localhost" - @redis_port = "6379" - @redis_db = "1" - @retry_count = 25 - @retry_delay = 200 + @redis_client = nil + @redis_host = "localhost" + @redis_port = "6379" + @redis_db = "1" + @retry_count = 25 + @retry_delay = 200 end end end diff --git a/spec/configuration_spec.rb b/spec/configuration_spec.rb index ccb1803..33156d2 100644 --- a/spec/configuration_spec.rb +++ b/spec/configuration_spec.rb @@ -3,6 +3,14 @@ RSpec.describe Potlock::Configuration do subject { described_class.new } + describe "redis_client" do + context "when no redis_client is specified" do + it "defaults to nil" do + expect(subject.redis_client).to be_nil + end + end + end + describe "redis_host" do context "when no redis_host is specified" do it "defaults to Redis hostname" do diff --git a/spec/potlock_spec.rb b/spec/potlock_spec.rb index aacda22..18f0655 100644 --- a/spec/potlock_spec.rb +++ b/spec/potlock_spec.rb @@ -1,15 +1,19 @@ # frozen_string_literal: true +require "mock_redis" + RSpec.describe Potlock do describe "#configure" do it "allows Potlock configuration" do Potlock.configure do |config| + config.redis_client = MockRedis.new config.redis_host = "redis" config.redis_port = "6381" config.redis_db = "2" config.retry_count = 10 config.retry_delay = 100 end + expect(Potlock.configuration.redis_host).to be_an_instance_of(MockRedis) expect(Potlock.configuration.redis_host).to eq("redis") expect(Potlock.configuration.redis_port).to eq("6381") expect(Potlock.configuration.redis_db).to eq("2")