DEV: Clean up some Redis leaks in test env.

This commit is contained in:
Guo Xiang Tan 2020-05-18 17:22:39 +08:00
parent 320b21ab5b
commit d01c336899
No known key found for this signature in database
GPG Key ID: FBD110179AAC1F20
8 changed files with 50 additions and 9 deletions

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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!

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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