DEV: Clean up some Redis leaks in test env.

DEV: Clean up some Redis leaks in test env.

diff --git a/app/models/application_request.rb b/app/models/application_request.rb
index df3cc15..52a6a8a 100644
--- a/app/models/application_request.rb
+++ b/app/models/application_request.rb
@@ -15,8 +15,16 @@ class ApplicationRequest < ActiveRecord::Base
 
   include CachedCounting
 
+  def self.disable
+    @enabled = false
+  end
+
+  def self.enable
+    @enabled = true
+  end
+
   def self.increment!(type, opts = nil)
-    perform_increment!(redis_key(type), opts)
+    perform_increment!(redis_key(type), opts) if @enabled
   end
 
   def self.write_cache!(date = nil)
diff --git a/app/services/badge_granter.rb b/app/services/badge_granter.rb
index afa1a3c..e402fb8 100644
--- a/app/services/badge_granter.rb
+++ b/app/services/badge_granter.rb
@@ -2,6 +2,14 @@
 
 class BadgeGranter
 
+  def self.disable_queue
+    @queue_enabled = false
+  end
+
+  def self.enable_queue
+    @queue_enabled = true
+  end
+
   def initialize(badge, user, opts = {})
     @badge, @user, @opts = badge, user, opts
     @granted_by = opts[:granted_by] || Discourse.system_user
@@ -116,7 +124,7 @@ class BadgeGranter
   end
 
   def self.queue_badge_grant(type, opt)
-    return unless SiteSetting.enable_badges
+    return if !SiteSetting.enable_badges || !@queue_enabled
     payload = nil
 
     case type
diff --git a/spec/components/middleware/request_tracker_spec.rb b/spec/components/middleware/request_tracker_spec.rb
index f0b605b..67efd8c 100644
--- a/spec/components/middleware/request_tracker_spec.rb
+++ b/spec/components/middleware/request_tracker_spec.rb
@@ -15,6 +15,15 @@ describe Middleware::RequestTracker do
     }.merge(opts)
   end
 
+  before do
+    ApplicationRequest.enable
+  end
+
+  after do
+    ApplicationRequest.disable
+    ApplicationRequest.clear_cache!
+  end
+
   context "full request" do
     before do
       @orig = WebCrawlerRequest.autoflush
diff --git a/spec/models/application_request_spec.rb b/spec/models/application_request_spec.rb
index 794fbbb..0531e1c 100644
--- a/spec/models/application_request_spec.rb
+++ b/spec/models/application_request_spec.rb
@@ -4,11 +4,12 @@ require 'rails_helper'
 
 describe ApplicationRequest do
   before do
+    ApplicationRequest.enable
     ApplicationRequest.last_flush = Time.now.utc
-    Discourse.redis.flushall
   end
 
   after do
+    ApplicationRequest.disable
     ApplicationRequest.clear_cache!
   end
 
@@ -44,7 +45,7 @@ describe ApplicationRequest do
   end
 
   it 'logs nothing for an unflushed increment' do
-    ApplicationRequest.increment!(:anon)
+    ApplicationRequest.increment!(:page_view_anon)
     expect(ApplicationRequest.count).to eq(0)
   end
 
@@ -90,7 +91,7 @@ describe ApplicationRequest do
 
   it 'clears cache correctly' do
     # otherwise we have test pollution
-    inc(:anon)
+    inc(:page_view_anon)
     ApplicationRequest.clear_cache!
     ApplicationRequest.write_cache!
 
diff --git a/spec/models/report_spec.rb b/spec/models/report_spec.rb
index 7b4dc6d..7ccaf60 100644
--- a/spec/models/report_spec.rb
+++ b/spec/models/report_spec.rb
@@ -1173,6 +1173,7 @@ describe Report do
 
     context "with data" do
       it "works" do
+        ApplicationRequest.enable
         3.times { ApplicationRequest.increment!(:page_view_crawler) }
         2.times { ApplicationRequest.increment!(:page_view_logged_in) }
         ApplicationRequest.increment!(:page_view_anon)
@@ -1190,6 +1191,9 @@ describe Report do
 
         expect(page_view_anon_report[:color]).to eql("#40c8ff")
         expect(page_view_anon_report[:data][0][:y]).to eql(1)
+      ensure
+        ApplicationRequest.disable
+        ApplicationRequest.clear_cache!
       end
     end
   end
diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb
index af09e50..5d3e0bc 100644
--- a/spec/requests/users_controller_spec.rb
+++ b/spec/requests/users_controller_spec.rb
@@ -1779,6 +1779,7 @@ describe UsersController do
           end
 
           it "updates the title" do
+            BadgeGranter.enable_queue
             user.update!(locale: :fr)
             user.change_trust_level!(TrustLevel[4])
             BadgeGranter.process_queue!
@@ -1786,6 +1787,9 @@ describe UsersController do
             leader_title = I18n.t("badges.leader.name", locale: :fr)
             put "/u/#{user.username}.json", params: { title: leader_title }
             expect(user.reload.title).to eq(leader_title)
+          ensure
+            BadgeGranter.disable_queue
+            BadgeGranter.clear_queue!
           end
         end
 
diff --git a/spec/services/badge_granter_spec.rb b/spec/services/badge_granter_spec.rb
index d6e08a1..b2fa73c 100644
--- a/spec/services/badge_granter_spec.rb
+++ b/spec/services/badge_granter_spec.rb
@@ -7,6 +7,15 @@ describe BadgeGranter do
   fab!(:badge) { Fabricate(:badge) }
   fab!(:user) { Fabricate(:user) }
 
+  before do
+    BadgeGranter.enable_queue
+  end
+
+  after do
+    BadgeGranter.disable_queue
+    BadgeGranter.clear_queue!
+  end
+
   describe 'revoke_titles' do
     it 'can correctly revoke titles' do
       badge = Fabricate(:badge, allow_title: true)
@@ -269,10 +278,6 @@ describe BadgeGranter do
     fab!(:user) { Fabricate(:user) }
     fab!(:liker) { Fabricate(:user) }
 
-    before do
-      BadgeGranter.clear_queue!
-    end
-
     it "grants autobiographer" do
       user.user_profile.bio_raw = "THIS IS MY bio it a long bio I like my bio"
       user.uploaded_avatar_id = 10
diff --git a/spec/tasks/redis_spec.rb b/spec/tasks/redis_spec.rb
index 4e22801..627ae66 100644
--- a/spec/tasks/redis_spec.rb
+++ b/spec/tasks/redis_spec.rb
@@ -36,6 +36,8 @@ RSpec.describe "Redis rake tasks", type: :multisite do
       orphan_keys.each do |key|
         expect(redis.get(key)).to eq(nil)
       end
+    ensure
+      active_keys.each { |key| redis.del(key) }
     end
   end
 end

GitHub sha: d01c3368