Skip to content

Commit

Permalink
DEVOPS-481 Add support for redis_client option
Browse files Browse the repository at this point in the history
  • Loading branch information
TiuSh committed Oct 5, 2023
1 parent 878e933 commit 10b28bc
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
14 changes: 9 additions & 5 deletions lib/potlock/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 7 additions & 6 deletions lib/potlock/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
8 changes: 8 additions & 0 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions spec/potlock_spec.rb
Original file line number Diff line number Diff line change
@@ -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")
Expand Down

0 comments on commit 10b28bc

Please sign in to comment.