Commit 563533c6 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch 'ali/clear-usage-ping-memoization' into 'master'

[RUN AS-IF-FOSS] Ensure memoized values are cleared for `UsageData` tests

See merge request gitlab-org/gitlab!44522
parents 5808f27e 76c6926c
......@@ -5,6 +5,13 @@ module EE
module UsageData
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 = {
container_scanning: {
name: :container_scanning_jobs
......@@ -485,10 +492,7 @@ module EE
def clear_memoized
super
clear_memoization(:approval_merge_request_rule_minimum_id)
clear_memoization(:approval_merge_request_rule_maximum_id)
clear_memoization(:merge_request_minimum_id)
clear_memoization(:merge_request_maximum_id)
EE_MEMOIZED_VALUES.each { |v| clear_memoization(v) } # rubocop:disable UsageData/LargeTable
end
end
end
......
......@@ -7,6 +7,7 @@ RSpec.describe Gitlab::UsageData do
before do
stub_usage_data_connections
clear_memoized_values(described_class::EE_MEMOIZED_VALUES + described_class::CE_MEMOIZED_VALUES)
end
describe '.data' do
......@@ -715,20 +716,12 @@ RSpec.describe Gitlab::UsageData do
end
it 'clears memoized values' do
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
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
allow(described_class).to receive(:clear_memoization)
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
......@@ -12,6 +12,19 @@
# redis_usage_data { ::Gitlab::UsageCounters::PodLogs.usage_totals[:total] }
module Gitlab
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
include Gitlab::Utils::UsageData
include Gitlab::Utils::StrongMemoize
......@@ -810,16 +823,7 @@ module Gitlab
end
def clear_memoized
clear_memoization(:issue_minimum_id)
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)
CE_MEMOIZED_VALUES.each { |v| clear_memoization(v) } # rubocop:disable UsageData/LargeTable
end
# rubocop: disable CodeReuse/ActiveRecord
......
......@@ -8,6 +8,7 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
before do
stub_usage_data_connections
stub_object_store_settings
clear_memoized_values(described_class::CE_MEMOIZED_VALUES)
end
describe '.uncached_data' do
......@@ -24,25 +25,13 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
end
it 'clears memoized values' do
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)
allow(described_class).to receive(:clear_memoization)
if Gitlab.ee?
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
subject
values.each do |key|
expect(described_class).to receive(:clear_memoization).with(key)
described_class::CE_MEMOIZED_VALUES.each do |key|
expect(described_class).to have_received(:clear_memoization).with(key)
end
subject
end
it 'merge_requests_users is included only in montly counters' do
......@@ -175,8 +164,6 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
describe 'usage_activity_by_stage_manage' do
it 'includes accurate usage_activity_by_stage data' do
described_class.clear_memoization(:auth_providers)
stub_config(
omniauth:
{ providers: omniauth_providers }
......@@ -1142,8 +1129,6 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
subject { described_class.compliance_unique_visits_data }
before do
described_class.clear_memoization(:unique_visit_service)
allow_next_instance_of(::Gitlab::Analytics::UniqueVisits) do |instance|
::Gitlab::Analytics::UniqueVisits.compliance_events.each do |target|
allow(instance).to receive(:unique_visits_for).with(targets: target).and_return(123)
......@@ -1174,7 +1159,6 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
subject { described_class.search_unique_visits_data }
before do
described_class.clear_memoization(:unique_visit_service)
events = ::Gitlab::UsageDataCounters::HLLRedisCounter.events_for_category('search')
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)
......
......@@ -173,6 +173,10 @@ module UsageDataHelpers
allow(Gitlab::Prometheus::Internal).to receive(:prometheus_enabled?).and_return(false)
end
def clear_memoized_values(values)
values.each { |v| described_class.clear_memoization(v) }
end
def stub_object_store_settings
allow(Settings).to receive(:[]).with('artifacts')
.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