Skip to content

Commit 5238032

Browse files
tubaxenorngan
andauthored
Add specs; Fix some gem dependencies (#1)
Co-authored-by: Ngan Pham <[email protected]>
1 parent 45e188f commit 5238032

File tree

9 files changed

+157
-9
lines changed

9 files changed

+157
-9
lines changed

Gemfile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ source "https://rubygems.org"
55
# Specify your gem's dependencies in bigrails-redis.gemspec
66
gemspec
77

8-
gem "rake", "~> 13.0"
9-
gem "rspec", "~> 3.0"
10-
gem "standard", "~> 1.3"
8+
gem "rake"
9+
gem "rspec"
10+
gem "standard"
11+
gem "fakeredis"
12+
gem "connection_pool"

Gemfile.lock

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ PATH
33
specs:
44
bigrails-redis (0.2.0)
55
rails (>= 6)
6+
redis (~> 4.0)
67

78
GEM
89
remote: https://rubygems.org/
@@ -75,10 +76,13 @@ GEM
7576
ast (2.4.2)
7677
builder (3.2.4)
7778
concurrent-ruby (1.1.9)
79+
connection_pool (2.2.5)
7880
crass (1.0.6)
7981
diff-lcs (1.5.0)
8082
digest (3.1.0)
8183
erubi (1.10.0)
84+
fakeredis (0.8.0)
85+
redis (~> 4.1)
8286
globalid (1.0.0)
8387
activesupport (>= 5.0)
8488
i18n (1.10.0)
@@ -148,6 +152,7 @@ GEM
148152
zeitwerk (~> 2.5)
149153
rainbow (3.1.1)
150154
rake (13.0.6)
155+
redis (4.6.0)
151156
regexp_parser (2.2.1)
152157
rexml (3.2.5)
153158
rspec (3.11.0)
@@ -198,9 +203,11 @@ PLATFORMS
198203

199204
DEPENDENCIES
200205
bigrails-redis!
201-
rake (~> 13.0)
202-
rspec (~> 3.0)
203-
standard (~> 1.3)
206+
connection_pool
207+
fakeredis
208+
rake
209+
rspec
210+
standard
204211

205212
BUNDLED WITH
206213
2.2.32

bigrails-redis.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
2222
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
2323

2424
spec.add_dependency "rails", ">= 6"
25+
spec.add_dependency "redis", "~> 4.0"
2526
end

lib/big_rails/redis/registry.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require "redis"
4+
35
module BigRails
46
module Redis
57
class Registry
@@ -67,9 +69,7 @@ def build_wrapped_connection(connection)
6769
end
6870

6971
def verify_connection(connection)
70-
connection.with do |redis|
71-
redis.ping == "PONG"
72-
end
72+
connection.with(&:ping) == "PONG"
7373
end
7474

7575
def validate_name(name)

spec/big_rails/redis/registry_spec.rb

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe BigRails::Redis::Registry do
4+
subject(:instance) { described_class.new }
5+
6+
before { load_config("simple") }
7+
8+
describe "#for" do
9+
context "when name is not valid" do
10+
it "raises error" do
11+
expect {
12+
instance.for("foo")
13+
}.to raise_error(described_class::UnknownConnection, "connection for 'foo' is not registered")
14+
end
15+
end
16+
17+
it "stores connection" do
18+
expect(instance.builder).to receive(:call).and_call_original.once
19+
expect(instance.for("default")).to be_a(::Redis)
20+
# Second call should not build connection again
21+
instance.for("default")
22+
end
23+
24+
context "with pool options" do
25+
it "returns connection pool" do
26+
expect(instance.for("pooled")).to be_a(::ConnectionPool)
27+
end
28+
end
29+
30+
context "when wrapped" do
31+
it "stores wrapped connection" do
32+
expect(instance.for("pooled", wrapped: true).wrapped_pool).to be_a(::ConnectionPool)
33+
end
34+
35+
context "when has no pool" do
36+
it "returns redis instance" do
37+
expect(instance.for("default", wrapped: true)).to be_a(::Redis)
38+
expect(instance.for("default", wrapped: true)).not_to respond_to(:wrapped_pool)
39+
end
40+
end
41+
end
42+
end
43+
44+
describe "#config_for" do
45+
it "returns config for specific connection" do
46+
expect(instance.config_for("default").redis_options).to eq(
47+
url: "redis://localhost"
48+
)
49+
50+
expect(instance.config_for("pooled").redis_options).to eq(
51+
url: "redis://localhost/2"
52+
)
53+
54+
expect(instance.config_for("pooled").pool_options).to eq(
55+
timeout: 5,
56+
size: 5
57+
)
58+
end
59+
end
60+
61+
describe "#each" do
62+
it "iterates through all connections" do
63+
instance.each do |conn|
64+
if conn.is_a? ::Redis
65+
expect(conn).to eq(instance.for("default"))
66+
elsif conn.is_a? ::ConnectionPool
67+
expect(conn).to eq(instance.for("pooled"))
68+
end
69+
end
70+
end
71+
end
72+
73+
describe "#verify!" do
74+
context "with name" do
75+
it "verifies specified connection" do
76+
conn = instance.for("default")
77+
conn2 = instance.for("pooled")
78+
expect(conn).to receive(:ping).and_call_original
79+
conn2.with do |redis|
80+
expect(redis).to receive(:ping).and_call_original
81+
end
82+
83+
instance.verify!("default")
84+
instance.verify!("pooled")
85+
end
86+
end
87+
88+
context "without" do
89+
it "verifies all connections" do
90+
conn = instance.for("default")
91+
conn2 = instance.for("pooled")
92+
expect(conn).to receive(:ping).and_call_original
93+
conn2.with do |redis|
94+
expect(redis).to receive(:ping).and_call_original
95+
end
96+
97+
instance.verify!
98+
end
99+
end
100+
end
101+
end

spec/fixtures/simple/redis.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
connection(:default) do
2+
{
3+
url: "redis://localhost"
4+
}
5+
end
6+
7+
connection(:pooled) do
8+
{
9+
url: "redis://localhost/2",
10+
pool_timeout: 5,
11+
pool_size: 5
12+
}
13+
end

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
# Disable RSpec exposing methods globally on `Module` and `main`
1010
config.disable_monkey_patching!
1111

12+
Dir[File.expand_path("support/**/*.rb", __dir__)].sort.each { |f| require_relative f }
13+
1214
config.expect_with :rspec do |c|
1315
c.syntax = :expect
1416
end

spec/support/fakeredis.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require "fakeredis/rspec"

spec/support/fixtures.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
require "pathname"
4+
5+
module Spec
6+
module Support
7+
module Fixtures
8+
def load_config(type)
9+
allow(Rails).to receive(:application).and_return(
10+
OpenStruct.new(paths: {
11+
"config" => [Pathname.new("spec/fixtures/#{type}").expand_path]
12+
})
13+
)
14+
end
15+
end
16+
end
17+
end
18+
19+
RSpec.configure do |config|
20+
config.include(Spec::Support::Fixtures)
21+
end

0 commit comments

Comments
 (0)