diff --git a/README.md b/README.md index 9a34a6d..81eea0a 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 = Redis.new(host: "localhost", port: 6379, db: 1) end ``` diff --git a/lib/potlock/client.rb b/lib/potlock/client.rb index 1bc65f0..ba9d039 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 unless Potlock.configuration.redis.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..9e8f3d1 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, :redis_host, :redis_port, :redis_db # How many times it'll try to lock a resource attr_accessor :retry_count @@ -12,9 +12,10 @@ class Configuration attr_accessor :retry_delay def initialize - @redis_host = "localhost" - @redis_port = "6379" - @redis_db = "1" + @redis = nil + @redis_host = "localhost" + @redis_port = "6379" + @redis_db = "1" @retry_count = 25 @retry_delay = 200 end diff --git a/spec/configuration_spec.rb b/spec/configuration_spec.rb index ccb1803..b18838e 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" do + context "when no redis is specified" do + it "defaults to nil" do + expect(subject.redis).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..8a262bd 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 = 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).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")