Commit 76c6926c authored by Alishan Ladhani's avatar Alishan Ladhani Committed by Heinrich Lee Yu

Ensure memoized values are cleared for all tests

- Some tests can fail when batch sizes are changed,
  because they rely on memoized values
parent 57af5fcd
...@@ -5,6 +5,13 @@ module EE ...@@ -5,6 +5,13 @@ module EE
module UsageData module UsageData
extend ActiveSupport::Concern extend ActiveSupport::Concern
EE_MEMOIZED_VALUES = %i(
approval_merge_request_rule_minimum_id
approval_merge_request_rule_maximum_id
merge_request_minimum_id
merge_request_maximum_id
).freeze
SECURE_PRODUCT_TYPES = { SECURE_PRODUCT_TYPES = {
container_scanning: { container_scanning: {
name: :container_scanning_jobs name: :container_scanning_jobs
...@@ -485,10 +492,7 @@ module EE ...@@ -485,10 +492,7 @@ module EE
def clear_memoized def clear_memoized
super super
clear_memoization(:approval_merge_request_rule_minimum_id) EE_MEMOIZED_VALUES.each { |v| clear_memoization(v) } # rubocop:disable UsageData/LargeTable
clear_memoization(:approval_merge_request_rule_maximum_id)
clear_memoization(:merge_request_minimum_id)
clear_memoization(:merge_request_maximum_id)
end end
end end
end end
......
...@@ -7,6 +7,7 @@ RSpec.describe Gitlab::UsageData do ...@@ -7,6 +7,7 @@ RSpec.describe Gitlab::UsageData do
before do before do
stub_usage_data_connections stub_usage_data_connections
clear_memoized_values(described_class::EE_MEMOIZED_VALUES + described_class::CE_MEMOIZED_VALUES)
end end
describe '.data' do describe '.data' do
...@@ -715,20 +716,12 @@ RSpec.describe Gitlab::UsageData do ...@@ -715,20 +716,12 @@ RSpec.describe Gitlab::UsageData do
end end
it 'clears memoized values' do it 'clears memoized values' do
values = %i(issue_minimum_id issue_maximum_id allow(described_class).to receive(:clear_memoization)
project_minimum_id project_maximum_id
user_minimum_id user_maximum_id unique_visit_service
deployment_minimum_id deployment_maximum_id
auth_providers
approval_merge_request_rule_minimum_id
approval_merge_request_rule_maximum_id
merge_request_minimum_id
merge_request_maximum_id)
values.each do |key|
expect(described_class).to receive(:clear_memoization).with(key)
end
described_class.uncached_data described_class.uncached_data
described_class::EE_MEMOIZED_VALUES.each do |key|
expect(described_class).to have_received(:clear_memoization).with(key)
end
end end
end end
...@@ -12,6 +12,19 @@ ...@@ -12,6 +12,19 @@
# redis_usage_data { ::Gitlab::UsageCounters::PodLogs.usage_totals[:total] } # redis_usage_data { ::Gitlab::UsageCounters::PodLogs.usage_totals[:total] }
module Gitlab module Gitlab
class UsageData class UsageData
CE_MEMOIZED_VALUES = %i(
issue_minimum_id
issue_maximum_id
project_minimum_id
project_maximum_id
user_minimum_id
user_maximum_id
unique_visit_service
deployment_minimum_id
deployment_maximum_id
auth_providers
).freeze
class << self class << self
include Gitlab::Utils::UsageData include Gitlab::Utils::UsageData
include Gitlab::Utils::StrongMemoize include Gitlab::Utils::StrongMemoize
...@@ -810,16 +823,7 @@ module Gitlab ...@@ -810,16 +823,7 @@ module Gitlab
end end
def clear_memoized def clear_memoized
clear_memoization(:issue_minimum_id) CE_MEMOIZED_VALUES.each { |v| clear_memoization(v) } # rubocop:disable UsageData/LargeTable
clear_memoization(:issue_maximum_id)
clear_memoization(:user_minimum_id)
clear_memoization(:user_maximum_id)
clear_memoization(:unique_visit_service)
clear_memoization(:deployment_minimum_id)
clear_memoization(:deployment_maximum_id)
clear_memoization(:project_minimum_id)
clear_memoization(:project_maximum_id)
clear_memoization(:auth_providers)
end end
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
......
...@@ -8,6 +8,7 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do ...@@ -8,6 +8,7 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
before do before do
stub_usage_data_connections stub_usage_data_connections
stub_object_store_settings stub_object_store_settings
clear_memoized_values(described_class::CE_MEMOIZED_VALUES)
end end
describe '.uncached_data' do describe '.uncached_data' do
...@@ -24,25 +25,13 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do ...@@ -24,25 +25,13 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
end end
it 'clears memoized values' do it 'clears memoized values' do
values = %i(issue_minimum_id issue_maximum_id allow(described_class).to receive(:clear_memoization)
project_minimum_id project_maximum_id
user_minimum_id user_maximum_id unique_visit_service
deployment_minimum_id deployment_maximum_id
auth_providers)
if Gitlab.ee? subject
values << %i(approval_merge_request_rule_minimum_id
approval_merge_request_rule_maximum_id
merge_request_minimum_id
merge_request_maximum_id)
values.flatten!
end
values.each do |key| described_class::CE_MEMOIZED_VALUES.each do |key|
expect(described_class).to receive(:clear_memoization).with(key) expect(described_class).to have_received(:clear_memoization).with(key)
end end
subject
end end
it 'merge_requests_users is included only in montly counters' do it 'merge_requests_users is included only in montly counters' do
...@@ -175,8 +164,6 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do ...@@ -175,8 +164,6 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
describe 'usage_activity_by_stage_manage' do describe 'usage_activity_by_stage_manage' do
it 'includes accurate usage_activity_by_stage data' do it 'includes accurate usage_activity_by_stage data' do
described_class.clear_memoization(:auth_providers)
stub_config( stub_config(
omniauth: omniauth:
{ providers: omniauth_providers } { providers: omniauth_providers }
...@@ -1142,8 +1129,6 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do ...@@ -1142,8 +1129,6 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
subject { described_class.compliance_unique_visits_data } subject { described_class.compliance_unique_visits_data }
before do before do
described_class.clear_memoization(:unique_visit_service)
allow_next_instance_of(::Gitlab::Analytics::UniqueVisits) do |instance| allow_next_instance_of(::Gitlab::Analytics::UniqueVisits) do |instance|
::Gitlab::Analytics::UniqueVisits.compliance_events.each do |target| ::Gitlab::Analytics::UniqueVisits.compliance_events.each do |target|
allow(instance).to receive(:unique_visits_for).with(targets: target).and_return(123) allow(instance).to receive(:unique_visits_for).with(targets: target).and_return(123)
...@@ -1174,7 +1159,6 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do ...@@ -1174,7 +1159,6 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
subject { described_class.search_unique_visits_data } subject { described_class.search_unique_visits_data }
before do before do
described_class.clear_memoization(:unique_visit_service)
events = ::Gitlab::UsageDataCounters::HLLRedisCounter.events_for_category('search') events = ::Gitlab::UsageDataCounters::HLLRedisCounter.events_for_category('search')
events.each do |event| events.each do |event|
allow(::Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:unique_events).with(event_names: event, start_date: 7.days.ago.to_date, end_date: Date.current).and_return(123) allow(::Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:unique_events).with(event_names: event, start_date: 7.days.ago.to_date, end_date: Date.current).and_return(123)
......
...@@ -173,6 +173,10 @@ module UsageDataHelpers ...@@ -173,6 +173,10 @@ module UsageDataHelpers
allow(Gitlab::Prometheus::Internal).to receive(:prometheus_enabled?).and_return(false) allow(Gitlab::Prometheus::Internal).to receive(:prometheus_enabled?).and_return(false)
end end
def clear_memoized_values(values)
values.each { |v| described_class.clear_memoization(v) }
end
def stub_object_store_settings def stub_object_store_settings
allow(Settings).to receive(:[]).with('artifacts') allow(Settings).to receive(:[]).with('artifacts')
.and_return( .and_return(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment