Skip to content

Commit

Permalink
Add specs; Fix some gem dependencies (#1)
Browse files Browse the repository at this point in the history
Co-authored-by: Ngan Pham <ngan@users.noreply.github.com>
  • Loading branch information
tubaxenor and ngan authored Mar 18, 2022
1 parent 45e188f commit 5238032
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 9 deletions.
8 changes: 5 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ source "https://rubygems.org"
# Specify your gem's dependencies in bigrails-redis.gemspec
gemspec

gem "rake", "~> 13.0"
gem "rspec", "~> 3.0"
gem "standard", "~> 1.3"
gem "rake"
gem "rspec"
gem "standard"
gem "fakeredis"
gem "connection_pool"
13 changes: 10 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ PATH
specs:
bigrails-redis (0.2.0)
rails (>= 6)
redis (~> 4.0)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -75,10 +76,13 @@ GEM
ast (2.4.2)
builder (3.2.4)
concurrent-ruby (1.1.9)
connection_pool (2.2.5)
crass (1.0.6)
diff-lcs (1.5.0)
digest (3.1.0)
erubi (1.10.0)
fakeredis (0.8.0)
redis (~> 4.1)
globalid (1.0.0)
activesupport (>= 5.0)
i18n (1.10.0)
Expand Down Expand Up @@ -148,6 +152,7 @@ GEM
zeitwerk (~> 2.5)
rainbow (3.1.1)
rake (13.0.6)
redis (4.6.0)
regexp_parser (2.2.1)
rexml (3.2.5)
rspec (3.11.0)
Expand Down Expand Up @@ -198,9 +203,11 @@ PLATFORMS

DEPENDENCIES
bigrails-redis!
rake (~> 13.0)
rspec (~> 3.0)
standard (~> 1.3)
connection_pool
fakeredis
rake
rspec
standard

BUNDLED WITH
2.2.32
1 change: 1 addition & 0 deletions bigrails-redis.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }

spec.add_dependency "rails", ">= 6"
spec.add_dependency "redis", "~> 4.0"
end
6 changes: 3 additions & 3 deletions lib/big_rails/redis/registry.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "redis"

module BigRails
module Redis
class Registry
Expand Down Expand Up @@ -67,9 +69,7 @@ def build_wrapped_connection(connection)
end

def verify_connection(connection)
connection.with do |redis|
redis.ping == "PONG"
end
connection.with(&:ping) == "PONG"
end

def validate_name(name)
Expand Down
101 changes: 101 additions & 0 deletions spec/big_rails/redis/registry_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# frozen_string_literal: true

RSpec.describe BigRails::Redis::Registry do
subject(:instance) { described_class.new }

before { load_config("simple") }

describe "#for" do
context "when name is not valid" do
it "raises error" do
expect {
instance.for("foo")
}.to raise_error(described_class::UnknownConnection, "connection for 'foo' is not registered")
end
end

it "stores connection" do
expect(instance.builder).to receive(:call).and_call_original.once
expect(instance.for("default")).to be_a(::Redis)
# Second call should not build connection again
instance.for("default")
end

context "with pool options" do
it "returns connection pool" do
expect(instance.for("pooled")).to be_a(::ConnectionPool)
end
end

context "when wrapped" do
it "stores wrapped connection" do
expect(instance.for("pooled", wrapped: true).wrapped_pool).to be_a(::ConnectionPool)
end

context "when has no pool" do
it "returns redis instance" do
expect(instance.for("default", wrapped: true)).to be_a(::Redis)
expect(instance.for("default", wrapped: true)).not_to respond_to(:wrapped_pool)
end
end
end
end

describe "#config_for" do
it "returns config for specific connection" do
expect(instance.config_for("default").redis_options).to eq(
url: "redis://localhost"
)

expect(instance.config_for("pooled").redis_options).to eq(
url: "redis://localhost/2"
)

expect(instance.config_for("pooled").pool_options).to eq(
timeout: 5,
size: 5
)
end
end

describe "#each" do
it "iterates through all connections" do
instance.each do |conn|
if conn.is_a? ::Redis
expect(conn).to eq(instance.for("default"))
elsif conn.is_a? ::ConnectionPool
expect(conn).to eq(instance.for("pooled"))
end
end
end
end

describe "#verify!" do
context "with name" do
it "verifies specified connection" do
conn = instance.for("default")
conn2 = instance.for("pooled")
expect(conn).to receive(:ping).and_call_original
conn2.with do |redis|
expect(redis).to receive(:ping).and_call_original
end

instance.verify!("default")
instance.verify!("pooled")
end
end

context "without" do
it "verifies all connections" do
conn = instance.for("default")
conn2 = instance.for("pooled")
expect(conn).to receive(:ping).and_call_original
conn2.with do |redis|
expect(redis).to receive(:ping).and_call_original
end

instance.verify!
end
end
end
end
13 changes: 13 additions & 0 deletions spec/fixtures/simple/redis.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
connection(:default) do
{
url: "redis://localhost"
}
end

connection(:pooled) do
{
url: "redis://localhost/2",
pool_timeout: 5,
pool_size: 5
}
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
# Disable RSpec exposing methods globally on `Module` and `main`
config.disable_monkey_patching!

Dir[File.expand_path("support/**/*.rb", __dir__)].sort.each { |f| require_relative f }

config.expect_with :rspec do |c|
c.syntax = :expect
end
Expand Down
1 change: 1 addition & 0 deletions spec/support/fakeredis.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "fakeredis/rspec"
21 changes: 21 additions & 0 deletions spec/support/fixtures.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require "pathname"

module Spec
module Support
module Fixtures
def load_config(type)
allow(Rails).to receive(:application).and_return(
OpenStruct.new(paths: {
"config" => [Pathname.new("spec/fixtures/#{type}").expand_path]
})
)
end
end
end
end

RSpec.configure do |config|
config.include(Spec::Support::Fixtures)
end

0 comments on commit 5238032

Please sign in to comment.