mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: Clean up some Redis leaks in test env.
This commit is contained in:
parent
320b21ab5b
commit
d01c336899
@ -15,8 +15,16 @@ class ApplicationRequest < ActiveRecord::Base
|
|||||||
|
|
||||||
include CachedCounting
|
include CachedCounting
|
||||||
|
|
||||||
|
def self.disable
|
||||||
|
@enabled = false
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.enable
|
||||||
|
@enabled = true
|
||||||
|
end
|
||||||
|
|
||||||
def self.increment!(type, opts = nil)
|
def self.increment!(type, opts = nil)
|
||||||
perform_increment!(redis_key(type), opts)
|
perform_increment!(redis_key(type), opts) if @enabled
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.write_cache!(date = nil)
|
def self.write_cache!(date = nil)
|
||||||
|
@ -2,6 +2,14 @@
|
|||||||
|
|
||||||
class BadgeGranter
|
class BadgeGranter
|
||||||
|
|
||||||
|
def self.disable_queue
|
||||||
|
@queue_enabled = false
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.enable_queue
|
||||||
|
@queue_enabled = true
|
||||||
|
end
|
||||||
|
|
||||||
def initialize(badge, user, opts = {})
|
def initialize(badge, user, opts = {})
|
||||||
@badge, @user, @opts = badge, user, opts
|
@badge, @user, @opts = badge, user, opts
|
||||||
@granted_by = opts[:granted_by] || Discourse.system_user
|
@granted_by = opts[:granted_by] || Discourse.system_user
|
||||||
@ -116,7 +124,7 @@ class BadgeGranter
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.queue_badge_grant(type, opt)
|
def self.queue_badge_grant(type, opt)
|
||||||
return unless SiteSetting.enable_badges
|
return if !SiteSetting.enable_badges || !@queue_enabled
|
||||||
payload = nil
|
payload = nil
|
||||||
|
|
||||||
case type
|
case type
|
||||||
|
@ -15,6 +15,15 @@ describe Middleware::RequestTracker do
|
|||||||
}.merge(opts)
|
}.merge(opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
before do
|
||||||
|
ApplicationRequest.enable
|
||||||
|
end
|
||||||
|
|
||||||
|
after do
|
||||||
|
ApplicationRequest.disable
|
||||||
|
ApplicationRequest.clear_cache!
|
||||||
|
end
|
||||||
|
|
||||||
context "full request" do
|
context "full request" do
|
||||||
before do
|
before do
|
||||||
@orig = WebCrawlerRequest.autoflush
|
@orig = WebCrawlerRequest.autoflush
|
||||||
|
@ -4,11 +4,12 @@ require 'rails_helper'
|
|||||||
|
|
||||||
describe ApplicationRequest do
|
describe ApplicationRequest do
|
||||||
before do
|
before do
|
||||||
|
ApplicationRequest.enable
|
||||||
ApplicationRequest.last_flush = Time.now.utc
|
ApplicationRequest.last_flush = Time.now.utc
|
||||||
Discourse.redis.flushall
|
|
||||||
end
|
end
|
||||||
|
|
||||||
after do
|
after do
|
||||||
|
ApplicationRequest.disable
|
||||||
ApplicationRequest.clear_cache!
|
ApplicationRequest.clear_cache!
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -44,7 +45,7 @@ describe ApplicationRequest do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it 'logs nothing for an unflushed increment' do
|
it 'logs nothing for an unflushed increment' do
|
||||||
ApplicationRequest.increment!(:anon)
|
ApplicationRequest.increment!(:page_view_anon)
|
||||||
expect(ApplicationRequest.count).to eq(0)
|
expect(ApplicationRequest.count).to eq(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -90,7 +91,7 @@ describe ApplicationRequest do
|
|||||||
|
|
||||||
it 'clears cache correctly' do
|
it 'clears cache correctly' do
|
||||||
# otherwise we have test pollution
|
# otherwise we have test pollution
|
||||||
inc(:anon)
|
inc(:page_view_anon)
|
||||||
ApplicationRequest.clear_cache!
|
ApplicationRequest.clear_cache!
|
||||||
ApplicationRequest.write_cache!
|
ApplicationRequest.write_cache!
|
||||||
|
|
||||||
|
@ -1173,6 +1173,7 @@ describe Report do
|
|||||||
|
|
||||||
context "with data" do
|
context "with data" do
|
||||||
it "works" do
|
it "works" do
|
||||||
|
ApplicationRequest.enable
|
||||||
3.times { ApplicationRequest.increment!(:page_view_crawler) }
|
3.times { ApplicationRequest.increment!(:page_view_crawler) }
|
||||||
2.times { ApplicationRequest.increment!(:page_view_logged_in) }
|
2.times { ApplicationRequest.increment!(:page_view_logged_in) }
|
||||||
ApplicationRequest.increment!(:page_view_anon)
|
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[:color]).to eql("#40c8ff")
|
||||||
expect(page_view_anon_report[:data][0][:y]).to eql(1)
|
expect(page_view_anon_report[:data][0][:y]).to eql(1)
|
||||||
|
ensure
|
||||||
|
ApplicationRequest.disable
|
||||||
|
ApplicationRequest.clear_cache!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1779,6 +1779,7 @@ describe UsersController do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "updates the title" do
|
it "updates the title" do
|
||||||
|
BadgeGranter.enable_queue
|
||||||
user.update!(locale: :fr)
|
user.update!(locale: :fr)
|
||||||
user.change_trust_level!(TrustLevel[4])
|
user.change_trust_level!(TrustLevel[4])
|
||||||
BadgeGranter.process_queue!
|
BadgeGranter.process_queue!
|
||||||
@ -1786,6 +1787,9 @@ describe UsersController do
|
|||||||
leader_title = I18n.t("badges.leader.name", locale: :fr)
|
leader_title = I18n.t("badges.leader.name", locale: :fr)
|
||||||
put "/u/#{user.username}.json", params: { title: leader_title }
|
put "/u/#{user.username}.json", params: { title: leader_title }
|
||||||
expect(user.reload.title).to eq(leader_title)
|
expect(user.reload.title).to eq(leader_title)
|
||||||
|
ensure
|
||||||
|
BadgeGranter.disable_queue
|
||||||
|
BadgeGranter.clear_queue!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -7,6 +7,15 @@ describe BadgeGranter do
|
|||||||
fab!(:badge) { Fabricate(:badge) }
|
fab!(:badge) { Fabricate(:badge) }
|
||||||
fab!(:user) { Fabricate(:user) }
|
fab!(:user) { Fabricate(:user) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
BadgeGranter.enable_queue
|
||||||
|
end
|
||||||
|
|
||||||
|
after do
|
||||||
|
BadgeGranter.disable_queue
|
||||||
|
BadgeGranter.clear_queue!
|
||||||
|
end
|
||||||
|
|
||||||
describe 'revoke_titles' do
|
describe 'revoke_titles' do
|
||||||
it 'can correctly revoke titles' do
|
it 'can correctly revoke titles' do
|
||||||
badge = Fabricate(:badge, allow_title: true)
|
badge = Fabricate(:badge, allow_title: true)
|
||||||
@ -269,10 +278,6 @@ describe BadgeGranter do
|
|||||||
fab!(:user) { Fabricate(:user) }
|
fab!(:user) { Fabricate(:user) }
|
||||||
fab!(:liker) { Fabricate(:user) }
|
fab!(:liker) { Fabricate(:user) }
|
||||||
|
|
||||||
before do
|
|
||||||
BadgeGranter.clear_queue!
|
|
||||||
end
|
|
||||||
|
|
||||||
it "grants autobiographer" do
|
it "grants autobiographer" do
|
||||||
user.user_profile.bio_raw = "THIS IS MY bio it a long bio I like my bio"
|
user.user_profile.bio_raw = "THIS IS MY bio it a long bio I like my bio"
|
||||||
user.uploaded_avatar_id = 10
|
user.uploaded_avatar_id = 10
|
||||||
|
@ -36,6 +36,8 @@ RSpec.describe "Redis rake tasks", type: :multisite do
|
|||||||
orphan_keys.each do |key|
|
orphan_keys.each do |key|
|
||||||
expect(redis.get(key)).to eq(nil)
|
expect(redis.get(key)).to eq(nil)
|
||||||
end
|
end
|
||||||
|
ensure
|
||||||
|
active_keys.each { |key| redis.del(key) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user