diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2024-01-22 21:10:33 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2024-01-22 21:10:33 +0300 |
commit | a9a2f9257eae40935e03ca4185d5263bcb7ba45f (patch) | |
tree | f12875873819442e10ab04bd15fd975bf4bb7b64 /spec/lib | |
parent | 917d93d86da4dffd96abcfcf3aa83b0d6fa45286 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib')
7 files changed, 304 insertions, 123 deletions
diff --git a/spec/lib/gitlab/instrumentation/redis_client_middleware_spec.rb b/spec/lib/gitlab/instrumentation/redis_client_middleware_spec.rb index eca75d93c80..a8bded69696 100644 --- a/spec/lib/gitlab/instrumentation/redis_client_middleware_spec.rb +++ b/spec/lib/gitlab/instrumentation/redis_client_middleware_spec.rb @@ -74,14 +74,14 @@ RSpec.describe Gitlab::Instrumentation::RedisClientMiddleware, :request_store, f context 'when encountering exceptions' do before do allow(redis_client.instance_variable_get(:@raw_connection)).to receive(:call).and_raise( - RedisClient::ConnectionError, 'Connection was closed or lost') + RedisClient::Error) end it 'counts exception' do expect(instrumentation_class).to receive(:instance_count_exception) - .with(instance_of(RedisClient::ConnectionError)).and_call_original + .with(instance_of(RedisClient::Error)).and_call_original expect(instrumentation_class).to receive(:log_exception) - .with(instance_of(RedisClient::ConnectionError)).and_call_original + .with(instance_of(RedisClient::Error)).and_call_original expect(instrumentation_class).to receive(:instance_count_request).and_call_original expect do diff --git a/spec/lib/gitlab/instrumentation/redis_helper_spec.rb b/spec/lib/gitlab/instrumentation/redis_helper_spec.rb index 54659ca2c02..84a5886bc4e 100644 --- a/spec/lib/gitlab/instrumentation/redis_helper_spec.rb +++ b/spec/lib/gitlab/instrumentation/redis_helper_spec.rb @@ -37,20 +37,27 @@ RSpec.describe Gitlab::Instrumentation::RedisHelper, :request_store, feature_cat subject(:minimal_test_class_instance) { MinimalTestClass.new } describe '.instrument_call' do + let(:pipelined) { false } + let(:command) { [[:set, 'foo', 'bar']] } + + subject(:instrumented_command) { minimal_test_class_instance.check_command(command, pipelined) } + it 'instruments request count' do expect(Gitlab::Instrumentation::Redis::Cache).to receive(:instance_count_request).with(1) expect(Gitlab::Instrumentation::Redis::Cache).not_to receive(:instance_count_pipelined_request) - minimal_test_class_instance.check_command([[:set, 'foo', 'bar']], false) + instrumented_command end it 'performs cluster validation' do expect(Gitlab::Instrumentation::Redis::Cache).to receive(:redis_cluster_validate!).once - minimal_test_class_instance.check_command([[:set, 'foo', 'bar']], false) + instrumented_command end context 'when command is not valid for Redis Cluster' do + let(:command) { [[:mget, 'foo', 'bar']] } + before do allow(Gitlab::Instrumentation::Redis::Cache).to receive(:redis_cluster_validate!).and_return(false) end @@ -58,7 +65,7 @@ RSpec.describe Gitlab::Instrumentation::RedisHelper, :request_store, feature_cat it 'reports cross slot request' do expect(Gitlab::Instrumentation::Redis::Cache).to receive(:increment_cross_slot_request_count).once - minimal_test_class_instance.check_command([[:mget, 'foo', 'bar']], false) + instrumented_command end end @@ -71,21 +78,52 @@ RSpec.describe Gitlab::Instrumentation::RedisHelper, :request_store, feature_cat end it 'ensures duration is tracked' do - commands = [[:set, 'foo', 'bar']] allow(Gitlab::Instrumentation::Redis::Cache).to receive(:instance_observe_duration).once allow(Gitlab::Instrumentation::Redis::Cache).to receive(:increment_request_count).with(1).once allow(Gitlab::Instrumentation::Redis::Cache).to receive(:add_duration).once - allow(Gitlab::Instrumentation::Redis::Cache).to receive(:add_call_details).with(anything, commands).once + allow(Gitlab::Instrumentation::Redis::Cache).to receive(:add_call_details).with(anything, command).once + + expect { instrumented_command }.to raise_error(StandardError) + end + end + + context 'when a RedisClient::ConnectionError is raised' do + before do + allow(Gitlab::Instrumentation::Redis::Cache).to receive(:instance_count_request) + .and_raise(RedisClient::ConnectionError) + end + + it 'silences connection errors raised during the first attempt' do + expect(Gitlab::Instrumentation::Redis::Cache).not_to receive(:log_exception).with(RedisClient::ConnectionError) + + expect { instrumented_command }.to raise_error(StandardError) - expect { minimal_test_class_instance.check_command(commands, false) }.to raise_error(StandardError) + expect(Thread.current[:redis_client_error_count]).to eq(1) + end + + context 'when error is raised on the second attempt' do + before do + Thread.current[:redis_client_error_count] = 1 + end + + it 'instruments errors on second attempt' do + expect(Gitlab::Instrumentation::Redis::Cache).to receive(:log_exception).with(RedisClient::ConnectionError) + + expect { instrumented_command }.to raise_error(StandardError) + + expect(Thread.current[:redis_client_error_count]).to eq(2) + end end end context 'when pipelined' do + let(:command) { [[:get, '{user1}:bar'], [:get, '{user1}:foo']] } + let(:pipelined) { true } + it 'instruments pipelined request count' do expect(Gitlab::Instrumentation::Redis::Cache).to receive(:instance_count_pipelined_request) - minimal_test_class_instance.check_command([[:get, '{user1}:bar'], [:get, '{user1}:foo']], true) + instrumented_command end end end diff --git a/spec/lib/gitlab/instrumentation/redis_interceptor_spec.rb b/spec/lib/gitlab/instrumentation/redis_interceptor_spec.rb index e9bd0056e5f..2a160a9d316 100644 --- a/spec/lib/gitlab/instrumentation/redis_interceptor_spec.rb +++ b/spec/lib/gitlab/instrumentation/redis_interceptor_spec.rb @@ -117,6 +117,8 @@ RSpec.describe Gitlab::Instrumentation::RedisInterceptor, :request_store, featur expect do redis_store_class.with { |redis| redis.call(:auth, 'foo', 'bar') } end.to raise_exception(Redis::CommandError) + + expect(Thread.current[:redis_client_error_count]).to eq(0) end end end diff --git a/spec/lib/gitlab/patch/redis_client_spec.rb b/spec/lib/gitlab/patch/redis_client_spec.rb new file mode 100644 index 00000000000..af094e9e0d2 --- /dev/null +++ b/spec/lib/gitlab/patch/redis_client_spec.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Patch::RedisClient, feature_category: :redis do + include RedisHelpers + + let_it_be(:redis_store_class) { define_helper_redis_store_class } + let_it_be(:redis_client) { RedisClient.new(redis_store_class.redis_client_params) } + + before do + Thread.current[:redis_client_error_count] = 1 + end + + it 'resets tracking count after each call' do + expect { redis_client.call("ping") } + .to change { Thread.current[:redis_client_error_count] } + .from(1).to(0) + end + + it 'resets tracking count after each blocking call' do + expect { redis_client.blocking_call(false, "ping") } + .to change { Thread.current[:redis_client_error_count] } + .from(1).to(0) + end + + it 'resets tracking count after pipelined' do + expect { redis_client.pipelined { |p| p.call("ping") } } + .to change { Thread.current[:redis_client_error_count] } + .from(1).to(0) + end + + it 'resets tracking count after multi' do + expect { redis_client.multi { |p| p.call("ping") } } + .to change { Thread.current[:redis_client_error_count] } + .from(1).to(0) + end +end diff --git a/spec/lib/gitlab/process_supervisor_spec.rb b/spec/lib/gitlab/process_supervisor_spec.rb index 94535e96843..5b9878df456 100644 --- a/spec/lib/gitlab/process_supervisor_spec.rb +++ b/spec/lib/gitlab/process_supervisor_spec.rb @@ -42,7 +42,7 @@ RSpec.describe Gitlab::ProcessSupervisor, feature_category: :cloud_connector do pids_killed = [] supervisor.supervise(process_ids) do |dead_pids| - pids_killed = dead_pids + pids_killed += dead_pids [] end @@ -60,7 +60,7 @@ RSpec.describe Gitlab::ProcessSupervisor, feature_category: :cloud_connector do pids_killed = [] supervisor.supervise(process_ids) do |dead_pids| - pids_killed = dead_pids + pids_killed += dead_pids [42] # Fake starting a new process in place of the terminated one. end @@ -68,7 +68,7 @@ RSpec.describe Gitlab::ProcessSupervisor, feature_category: :cloud_connector do Process.kill('TERM', process_ids.first) await_condition(sleep_sec: health_check_interval_seconds) do - pids_killed == [process_ids.first] + pids_killed.include?(process_ids.first) end expect(Gitlab::ProcessManagement.process_alive?(process_ids.first)).to be(false) @@ -81,7 +81,7 @@ RSpec.describe Gitlab::ProcessSupervisor, feature_category: :cloud_connector do pids_killed = [] supervisor.supervise(process_ids) do |dead_pids| - pids_killed = dead_pids + pids_killed += dead_pids # Fake a new process having the same pid as one that was just terminated. [process_ids.last] end @@ -90,7 +90,7 @@ RSpec.describe Gitlab::ProcessSupervisor, feature_category: :cloud_connector do Process.kill('TERM', process_ids.first) await_condition(sleep_sec: health_check_interval_seconds) do - pids_killed == [process_ids.first] + pids_killed.include?(process_ids.first) end expect(supervisor.supervised_pids).to contain_exactly(process_ids.last) @@ -101,7 +101,7 @@ RSpec.describe Gitlab::ProcessSupervisor, feature_category: :cloud_connector do pids_killed = [] supervisor.supervise(process_ids) do |dead_pids| - pids_killed = dead_pids + pids_killed += dead_pids 42 end @@ -109,7 +109,7 @@ RSpec.describe Gitlab::ProcessSupervisor, feature_category: :cloud_connector do Process.kill('TERM', process_ids.first) await_condition(sleep_sec: health_check_interval_seconds) do - pids_killed == [process_ids.first] + pids_killed.include?(process_ids.first) end expect(supervisor.supervised_pids).to contain_exactly(42, process_ids.last) diff --git a/spec/lib/gitlab/usage_data_counters/editor_unique_counter_spec.rb b/spec/lib/gitlab/usage_data_counters/editor_unique_counter_spec.rb deleted file mode 100644 index cbf4d3c8261..00000000000 --- a/spec/lib/gitlab/usage_data_counters/editor_unique_counter_spec.rb +++ /dev/null @@ -1,80 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Gitlab::UsageDataCounters::EditorUniqueCounter, :clean_gitlab_redis_shared_state do - let(:user) { build(:user, id: 1) } - let(:user2) { build(:user, id: 2) } - let(:user3) { build(:user, id: 3) } - let(:project) { build(:project) } - let(:namespace) { project.namespace } - let(:time) { Time.zone.now } - - shared_examples 'tracks and counts action' do - subject { track_action(author: user, project: project) } - - before do - stub_application_setting(usage_ping_enabled: true) - end - - specify do - aggregate_failures do - track_action(author: user, project: project) - track_action(author: user2, project: project) - track_action(author: user3, project: project) - - expect(count_unique(date_from: time.beginning_of_week, date_to: 1.week.from_now)).to eq(3) - end - end - - it_behaves_like 'internal event tracking' - - it 'does not track edit actions if author is not present' do - track_action(author: nil, project: project) - - expect(count_unique(date_from: time.beginning_of_week, date_to: 1.week.from_now)).to eq(0) - end - end - - context 'for web IDE edit actions' do - let(:event) { described_class::EDIT_BY_WEB_IDE } - - it_behaves_like 'tracks and counts action' do - def track_action(params) - described_class.track_web_ide_edit_action(**params) - end - - def count_unique(params) - described_class.count_web_ide_edit_actions(**params) - end - end - end - - context 'for SFE edit actions' do - let(:event) { described_class::EDIT_BY_SFE } - - it_behaves_like 'tracks and counts action' do - def track_action(params) - described_class.track_sfe_edit_action(**params) - end - - def count_unique(params) - described_class.count_sfe_edit_actions(**params) - end - end - end - - context 'for snippet editor edit actions' do - let(:event) { described_class::EDIT_BY_SNIPPET_EDITOR } - - it_behaves_like 'tracks and counts action' do - def track_action(params) - described_class.track_snippet_editor_edit_action(**params) - end - - def count_unique(params) - described_class.count_snippet_editor_edit_actions(**params) - end - end - end -end diff --git a/spec/lib/gitlab/usage_data_counters/hll_redis_counter_spec.rb b/spec/lib/gitlab/usage_data_counters/hll_redis_counter_spec.rb index da8098bfee1..5077c3532ef 100644 --- a/spec/lib/gitlab/usage_data_counters/hll_redis_counter_spec.rb +++ b/spec/lib/gitlab/usage_data_counters/hll_redis_counter_spec.rb @@ -62,15 +62,15 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s describe 'known_events' do let(:weekly_event) { 'g_analytics_contribution' } - let(:daily_event) { 'g_analytics_search' } + let(:daily_event) { 'g_analytics_issues' } let(:analytics_slot_event) { 'g_analytics_contribution' } let(:compliance_slot_event) { 'g_compliance_dashboard' } - let(:category_analytics_event) { 'g_analytics_search' } + let(:category_analytics_event) { 'g_analytics_issues' } let(:category_productivity_event) { 'g_analytics_productivity' } let(:no_slot) { 'no_slot' } let(:different_aggregation) { 'different_aggregation' } let(:custom_daily_event) { 'g_analytics_custom' } - + let(:event_overridden_for_user) { 'user_created_custom_dashboard' } let(:global_category) { 'global' } let(:compliance_category) { 'compliance' } let(:productivity_category) { 'productivity' } @@ -84,7 +84,8 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s { name: category_productivity_event }, { name: compliance_slot_event }, { name: no_slot }, - { name: different_aggregation } + { name: different_aggregation }, + { name: event_overridden_for_user } ].map(&:with_indifferent_access) end @@ -216,6 +217,97 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s end end end + + describe "property_name" do + before do + stub_feature_flags(redis_hll_property_name_tracking: property_name_flag_enabled) + end + + context "with enabled feature flag" do + let(:property_name_flag_enabled) { true } + + context "with a property_name for an overridden event" do + context "with a property_name sent as a symbol" do + it "tracks the events using the Redis key override" do + expected_key = "{hll_counters}_#{event_overridden_for_user}-2020-23" + expect(Gitlab::Redis::HLL).to receive(:add).with(hash_including(key: expected_key)) + + described_class.track_event(event_overridden_for_user, values: entity1, property_name: :user) + end + end + + context "with a property_name sent in string format" do + it "tracks the events using the Redis key override" do + expected_key = "{hll_counters}_#{event_overridden_for_user}-2020-23" + expect(Gitlab::Redis::HLL).to receive(:add).with(hash_including(key: expected_key)) + + described_class.track_event(event_overridden_for_user, values: entity1, property_name: 'user.id') + end + end + end + + context "with a property_name for an overridden event that doesn't include this property_name" do + it "tracks the events using a Redis key with the property_name" do + expected_key = "{hll_counters}_#{no_slot}-user-2020-23" + expect(Gitlab::Redis::HLL).to receive(:add).with(hash_including(key: expected_key)) + + described_class.track_event(no_slot, values: entity1, property_name: 'user') + end + end + + context "with a property_name for a new event" do + it "tracks the events using a Redis key with the property_name" do + expected_key = "{hll_counters}_#{no_slot}-project-2020-23" + expect(Gitlab::Redis::HLL).to receive(:add).with(hash_including(key: expected_key)) + + described_class.track_event(no_slot, values: entity1, property_name: 'project') + end + end + + context "with no property_name for an overridden event" do + it "tracks the events using a Redis key with no property_name" do + expected_key = "{hll_counters}_#{event_overridden_for_user}-2020-23" + expect(Gitlab::Redis::HLL).to receive(:add).with(hash_including(key: expected_key)) + + described_class.track_event(event_overridden_for_user, values: entity1) + end + end + + context "with no property_name for a new event" do + it "tracks the events using a Redis key with no property_name" do + expected_key = "{hll_counters}_#{no_slot}-2020-23" + expect(Gitlab::Redis::HLL).to receive(:add).with(hash_including(key: expected_key)) + + described_class.track_event(no_slot, values: entity1) + end + end + end + + context "with disabled feature flag" do + let(:property_name_flag_enabled) { false } + + it "uses old Redis key for overridden events" do + expected_key = "{hll_counters}_#{event_overridden_for_user}-2020-23" + expect(Gitlab::Redis::HLL).to receive(:add).with(hash_including(key: expected_key)) + + described_class.track_event(event_overridden_for_user, values: entity1, property_name: 'user') + end + + it "uses old Redis key for new events" do + expected_key = "{hll_counters}_#{no_slot}-2020-23" + expect(Gitlab::Redis::HLL).to receive(:add).with(hash_including(key: expected_key)) + + described_class.track_event(no_slot, values: entity1, property_name: 'project') + end + + it "uses old Redis key for new events when no property name sent" do + expected_key = "{hll_counters}_#{no_slot}-2020-23" + expect(Gitlab::Redis::HLL).to receive(:add).with(hash_including(key: expected_key)) + + described_class.track_event(no_slot, values: entity1) + end + end + end end describe '.unique_events' do @@ -227,7 +319,7 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s # Events last week described_class.track_event(weekly_event, values: entity1, time: 2.days.ago) described_class.track_event(weekly_event, values: entity1, time: 2.days.ago) - described_class.track_event(no_slot, values: entity1, time: 2.days.ago) + described_class.track_event(no_slot, values: entity1, property_name: 'user.id', time: 2.days.ago) # Events 2 weeks ago described_class.track_event(weekly_event, values: entity1, time: 2.weeks.ago) @@ -274,7 +366,7 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s end context 'when no slot is set' do - it { expect(described_class.unique_events(event_names: [no_slot], start_date: 7.days.ago, end_date: Date.current)).to eq(1) } + it { expect(described_class.unique_events(event_names: [no_slot], property_name: 'user.id', start_date: 7.days.ago, end_date: Date.current)).to eq(1) } end context 'when data crosses into new year' do @@ -283,6 +375,97 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s .not_to raise_error end end + + describe "property_names" do + before do + stub_feature_flags(redis_hll_property_name_tracking: property_name_flag_enabled) + end + + context "with enabled feature flag" do + let(:property_name_flag_enabled) { true } + + context "with a property_name for an overridden event" do + context "with a property_name sent as a symbol" do + it "tracks the events using the Redis key override" do + expected_key = "{hll_counters}_#{event_overridden_for_user}-2020-22" + expect(Gitlab::Redis::HLL).to receive(:count).with(keys: [expected_key]) + + described_class.unique_events(event_names: [event_overridden_for_user], property_name: :user, start_date: 7.days.ago, end_date: Date.current) + end + end + + context "with a property_name sent in string format" do + it "tracks the events using the Redis key override" do + expected_key = "{hll_counters}_#{event_overridden_for_user}-2020-22" + expect(Gitlab::Redis::HLL).to receive(:count).with(keys: [expected_key]) + + described_class.unique_events(event_names: [event_overridden_for_user], property_name: 'user.id', start_date: 7.days.ago, end_date: Date.current) + end + end + end + + context "with a property_name for an overridden event that doesn't include this property_name" do + it "tracks the events using a Redis key with the property_name" do + expected_key = "{hll_counters}_#{no_slot}-user-2020-22" + expect(Gitlab::Redis::HLL).to receive(:count).with(keys: [expected_key]) + + described_class.unique_events(event_names: [no_slot], property_name: 'user', start_date: 7.days.ago, end_date: Date.current) + end + end + + context "with a property_name for a new event" do + it "tracks the events using a Redis key with the property_name" do + expected_key = "{hll_counters}_#{no_slot}-project-2020-22" + expect(Gitlab::Redis::HLL).to receive(:count).with(keys: [expected_key]) + + described_class.unique_events(event_names: [no_slot], property_name: 'project', start_date: 7.days.ago, end_date: Date.current) + end + end + + context "with no property_name for a overridden event" do + it "tracks the events using a Redis key with no property_name" do + expected_key = "{hll_counters}_#{event_overridden_for_user}-2020-22" + expect(Gitlab::Redis::HLL).to receive(:count).with(keys: [expected_key]) + + described_class.unique_events(event_names: [event_overridden_for_user], start_date: 7.days.ago, end_date: Date.current) + end + end + + context "with no property_name for a new event" do + it "tracks the events using a Redis key with no property_name" do + expected_key = "{hll_counters}_#{no_slot}-2020-22" + expect(Gitlab::Redis::HLL).to receive(:count).with(keys: [expected_key]) + + described_class.unique_events(event_names: [no_slot], start_date: 7.days.ago, end_date: Date.current) + end + end + end + + context "with disabled feature flag" do + let(:property_name_flag_enabled) { false } + + it "uses old Redis key for overridden events" do + expected_key = "{hll_counters}_#{event_overridden_for_user}-2020-22" + expect(Gitlab::Redis::HLL).to receive(:count).with(keys: [expected_key]) + + described_class.unique_events(event_names: [event_overridden_for_user], property_name: 'user', start_date: 7.days.ago, end_date: Date.current) + end + + it "uses old Redis key for new events" do + expected_key = "{hll_counters}_#{no_slot}-2020-22" + expect(Gitlab::Redis::HLL).to receive(:count).with(keys: [expected_key]) + + described_class.unique_events(event_names: [no_slot], property_name: 'project', start_date: 7.days.ago, end_date: Date.current) + end + + it "uses old Redis key for new events when no property name sent" do + expected_key = "{hll_counters}_#{no_slot}-2020-22" + expect(Gitlab::Redis::HLL).to receive(:count).with(keys: [expected_key]) + + described_class.unique_events(event_names: [no_slot], start_date: 7.days.ago, end_date: Date.current) + end + end + end end describe 'key overrides file' do @@ -341,43 +524,43 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s let(:time_range) { { start_date: 7.days.ago, end_date: DateTime.current } } let(:known_events) do [ - { name: 'event1_slot' }, - { name: 'event2_slot' }, - { name: 'event3_slot' }, - { name: 'event5_slot' }, - { name: 'event4' } + { name: 'g_compliance_dashboard' }, + { name: 'g_project_management_epic_created' }, + { name: 'g_project_management_epic_closed' }, + { name: 'g_project_management_epic_reopened' }, + { name: 'g_project_management_epic_issue_added' } ].map(&:with_indifferent_access) end before do allow(described_class).to receive(:known_events).and_return(known_events) - described_class.track_event('event1_slot', values: entity1, time: 2.days.ago) - described_class.track_event('event1_slot', values: entity2, time: 2.days.ago) - described_class.track_event('event1_slot', values: entity3, time: 2.days.ago) - described_class.track_event('event2_slot', values: entity1, time: 2.days.ago) - described_class.track_event('event2_slot', values: entity2, time: 3.days.ago) - described_class.track_event('event2_slot', values: entity3, time: 3.days.ago) - described_class.track_event('event3_slot', values: entity1, time: 3.days.ago) - described_class.track_event('event3_slot', values: entity2, time: 3.days.ago) - described_class.track_event('event5_slot', values: entity2, time: 3.days.ago) + described_class.track_event('g_compliance_dashboard', values: entity1, time: 2.days.ago) + described_class.track_event('g_compliance_dashboard', values: entity2, time: 2.days.ago) + described_class.track_event('g_compliance_dashboard', values: entity3, time: 2.days.ago) + described_class.track_event('g_project_management_epic_created', values: entity1, time: 2.days.ago) + described_class.track_event('g_project_management_epic_created', values: entity2, time: 3.days.ago) + described_class.track_event('g_project_management_epic_created', values: entity3, time: 3.days.ago) + described_class.track_event('g_project_management_epic_closed', values: entity1, time: 3.days.ago) + described_class.track_event('g_project_management_epic_closed', values: entity2, time: 3.days.ago) + described_class.track_event('g_project_management_epic_reopened', values: entity2, time: 3.days.ago) # events out of time scope - described_class.track_event('event2_slot', values: entity4, time: 8.days.ago) + described_class.track_event('g_project_management_epic_created', values: entity4, time: 8.days.ago) # events in different slots - described_class.track_event('event4', values: entity1, time: 2.days.ago) - described_class.track_event('event4', values: entity2, time: 2.days.ago) + described_class.track_event('g_project_management_epic_issue_added', values: entity1, time: 2.days.ago) + described_class.track_event('g_project_management_epic_issue_added', values: entity2, time: 2.days.ago) end it 'calculates union of given events', :aggregate_failures do - expect(described_class.calculate_events_union(**time_range.merge(event_names: %w[event4]))).to eq 2 - expect(described_class.calculate_events_union(**time_range.merge(event_names: %w[event1_slot event2_slot event3_slot]))).to eq 3 + expect(described_class.calculate_events_union(**time_range.merge(event_names: %w[g_project_management_epic_issue_added]))).to eq 2 + expect(described_class.calculate_events_union(**time_range.merge(event_names: %w[g_compliance_dashboard g_project_management_epic_created g_project_management_epic_closed]))).to eq 3 end it 'returns 0 if there are no keys for given events' do expect(Gitlab::Redis::HLL).not_to receive(:count) - expect(described_class.calculate_events_union(event_names: %w[event1_slot event2_slot event3_slot], start_date: Date.current, end_date: 4.weeks.ago)).to eq(-1) + expect(described_class.calculate_events_union(event_names: %w[g_compliance_dashboard g_project_management_epic_created g_project_management_epic_closed], start_date: Date.current, end_date: 4.weeks.ago)).to eq(-1) end end |