Skip to content

Commit

Permalink
Isolate each specs for cache store (mastodon#6450)
Browse files Browse the repository at this point in the history
The cache store is explicitly used by some specs, but they were not
isolated and therefore not reliable. This fixes the issue by clearing
the cache after each specs.
  • Loading branch information
akihikodaki authored and Gargron committed Feb 17, 2018
1 parent a71af98 commit 9b8a448
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
4 changes: 4 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
config.consider_all_requests_local = true
config.action_controller.perform_caching = false

# The default store, file_store is shared by processses parallely executed
# and should not be used.
config.cache_store = :memory_store

# Raise exceptions instead of rendering exception templates.
config.action_dispatch.show_exceptions = false

Expand Down
13 changes: 8 additions & 5 deletions spec/models/setting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@
described_class[key]
end

it 'calls Rails.cache.fetch' do
expect(Rails).to receive_message_chain(:cache, :fetch).with(cache_key)
described_class[key]
end

context 'Rails.cache does not exists' do
before do
allow(RailsSettings::Settings).to receive(:object).with(key).and_return(object)
Expand Down Expand Up @@ -103,6 +98,14 @@
Rails.cache.write(cache_key, cache_value)
end

it 'does not query the database' do
expect do |callback|
ActiveSupport::Notifications.subscribed callback, 'sql.active_record' do
described_class[key]
end
end.not_to yield_control
end

it 'returns the cached value' do
expect(described_class[key]).to eq cache_value
end
Expand Down
12 changes: 3 additions & 9 deletions spec/presenters/instance_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,23 @@

describe "user_count" do
it "returns the number of site users" do
cache = double
allow(Rails).to receive(:cache).and_return(cache)
allow(cache).to receive(:fetch).with("user_count").and_return(123)
Rails.cache.write 'user_count', 123

expect(instance_presenter.user_count).to eq(123)
end
end

describe "status_count" do
it "returns the number of local statuses" do
cache = double
allow(Rails).to receive(:cache).and_return(cache)
allow(cache).to receive(:fetch).with("local_status_count").and_return(234)
Rails.cache.write 'local_status_count', 234

expect(instance_presenter.status_count).to eq(234)
end
end

describe "domain_count" do
it "returns the number of known domains" do
cache = double
allow(Rails).to receive(:cache).and_return(cache)
allow(cache).to receive(:fetch).with("distinct_domain_count").and_return(345)
Rails.cache.write 'distinct_domain_count', 345

expect(instance_presenter.domain_count).to eq(345)
end
Expand Down
2 changes: 2 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def sign_in(resource, _deprecated = nil, scope: nil)
end

config.after :each do
Rails.cache.clear

keys = Redis.current.keys
Redis.current.del(keys) if keys.any?
end
Expand Down

0 comments on commit 9b8a448

Please sign in to comment.