]> cat aescling's git repositories - mastodon.git/commitdiff
Isolate each specs for cache store (#6450)
authorAkihiko Odaki <akihiko.odaki.4i@stu.hosei.ac.jp>
Sat, 17 Feb 2018 21:35:05 +0000 (06:35 +0900)
committerEugen Rochko <eugen@zeonfederated.com>
Sat, 17 Feb 2018 21:35:05 +0000 (22:35 +0100)
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.

config/environments/test.rb
spec/models/setting_spec.rb
spec/presenters/instance_presenter_spec.rb
spec/rails_helper.rb

index e68cb156dd98c9ad7fb2ae42a8323c804a74e7fe..20fe5f81384ddd431fda2c59eae18eaec54a5010 100644 (file)
@@ -23,6 +23,10 @@ Rails.application.configure do
   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
 
index bbba5f98d9b30b666fd2f137e63bb37d37ed4289..1cc5286748acd34043f19bd8a8b5b06f44b7b97b 100644 (file)
@@ -42,11 +42,6 @@ RSpec.describe Setting, type: :model do
         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)
@@ -103,6 +98,14 @@ RSpec.describe Setting, type: :model do
           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
index 7c47aed616c57e72cabec4235c486d09b337f1c7..006403925fe52cd2e80d216d5fd006b712531ed7 100644 (file)
@@ -90,9 +90,7 @@ describe InstancePresenter do
 
   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
@@ -100,9 +98,7 @@ describe InstancePresenter do
 
   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
@@ -110,9 +106,7 @@ describe InstancePresenter do
 
   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
index 4f7399505c48c9fe3c64558505824bf62d7e18b7..dc1f32e085c32aac5765dfd963941f20c5d39b23 100644 (file)
@@ -51,6 +51,8 @@ RSpec.configure do |config|
   end
 
   config.after :each do
+    Rails.cache.clear
+
     keys = Redis.current.keys
     Redis.current.del(keys) if keys.any?
   end