mirror of
https://github.com/discourse/discourse.git
synced 2025-05-25 09:57:25 +08:00
DEV: stop leaking data into tables during test (#21403)
This amends it so our cached counting reliant specs run in synchronize mode When running async there are situations where data is left over in the table after a transactional test. This means that repeat runs of the test suite fail.
This commit is contained in:
@ -83,9 +83,11 @@ module CachedCounting
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.flush
|
def self.flush
|
||||||
@flush = true
|
if @thread
|
||||||
@thread.wakeup
|
@flush = true
|
||||||
sleep 0.001 while @flush
|
@thread.wakeup
|
||||||
|
sleep 0.001 while @flush
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
COUNTER_REDIS_HASH = "CounterCacheHash"
|
COUNTER_REDIS_HASH = "CounterCacheHash"
|
||||||
@ -163,9 +165,30 @@ module CachedCounting
|
|||||||
end
|
end
|
||||||
|
|
||||||
class_methods do
|
class_methods do
|
||||||
def perform_increment!(key)
|
if Rails.env.test?
|
||||||
CachedCounting.ensure_thread!
|
# perform increment is a risky call in test,
|
||||||
CachedCounting.queue(key, self)
|
# it shifts stuff to background threads and leaks
|
||||||
|
# data in the DB
|
||||||
|
# Require caller is deliberate if they want that
|
||||||
|
#
|
||||||
|
# Splitting implementation to avoid any perf impact
|
||||||
|
# given this is a method that is called a lot
|
||||||
|
def perform_increment!(key, async: false)
|
||||||
|
if async
|
||||||
|
CachedCounting.ensure_thread!
|
||||||
|
CachedCounting.queue(key, self)
|
||||||
|
else
|
||||||
|
CachedCounting.queue(key, self)
|
||||||
|
CachedCounting.clear_flush_to_db_lock!
|
||||||
|
CachedCounting.flush_in_memory
|
||||||
|
CachedCounting.flush_to_db
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
def perform_increment!(key)
|
||||||
|
CachedCounting.ensure_thread!
|
||||||
|
CachedCounting.queue(key, self)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def write_cache!(key, count, date)
|
def write_cache!(key, count, date)
|
||||||
|
@ -80,15 +80,15 @@ RSpec.describe CachedCounting do
|
|||||||
freeze_time
|
freeze_time
|
||||||
d1 = Time.now.utc.to_date
|
d1 = Time.now.utc.to_date
|
||||||
|
|
||||||
RailsCacheCounter.perform_increment!("a,a")
|
RailsCacheCounter.perform_increment!("a,a", async: true)
|
||||||
RailsCacheCounter.perform_increment!("b")
|
RailsCacheCounter.perform_increment!("b", async: true)
|
||||||
20.times { RailsCacheCounter.perform_increment!("a,a") }
|
20.times { RailsCacheCounter.perform_increment!("a,a", async: true) }
|
||||||
|
|
||||||
freeze_time 2.days.from_now
|
freeze_time 2.days.from_now
|
||||||
d2 = Time.now.utc.to_date
|
d2 = Time.now.utc.to_date
|
||||||
|
|
||||||
RailsCacheCounter.perform_increment!("a,a")
|
RailsCacheCounter.perform_increment!("a,a", async: true)
|
||||||
RailsCacheCounter.perform_increment!("d")
|
RailsCacheCounter.perform_increment!("d", async: true)
|
||||||
|
|
||||||
CachedCounting.flush
|
CachedCounting.flush
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@ RSpec.describe Middleware::RequestTracker do
|
|||||||
end
|
end
|
||||||
|
|
||||||
after do
|
after do
|
||||||
|
CachedCounting.reset
|
||||||
ApplicationRequest.disable
|
ApplicationRequest.disable
|
||||||
CachedCounting.disable
|
CachedCounting.disable
|
||||||
end
|
end
|
||||||
|
@ -1309,9 +1309,11 @@ RSpec.describe Report do
|
|||||||
end
|
end
|
||||||
|
|
||||||
after do
|
after do
|
||||||
|
CachedCounting.reset
|
||||||
ApplicationRequest.disable
|
ApplicationRequest.disable
|
||||||
CachedCounting.disable
|
CachedCounting.disable
|
||||||
end
|
end
|
||||||
|
|
||||||
it "works" do
|
it "works" do
|
||||||
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) }
|
||||||
|
@ -6,7 +6,10 @@ RSpec.describe WebCrawlerRequest do
|
|||||||
CachedCounting.enable
|
CachedCounting.enable
|
||||||
end
|
end
|
||||||
|
|
||||||
after { CachedCounting.disable }
|
after do
|
||||||
|
CachedCounting.reset
|
||||||
|
CachedCounting.disable
|
||||||
|
end
|
||||||
|
|
||||||
it "can log crawler requests" do
|
it "can log crawler requests" do
|
||||||
freeze_time
|
freeze_time
|
||||||
|
Reference in New Issue
Block a user