2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
RSpec.describe Users::ActivityService, feature_category: :user_profile do
|
2018-11-18 11:00:15 +05:30
|
|
|
include ExclusiveLeaseHelpers
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
let(:user) { create(:user, last_activity_on: last_activity_on) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
subject { described_class.new(author: user) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
describe '#execute', :clean_gitlab_redis_shared_state do
|
2023-04-23 21:23:45 +05:30
|
|
|
shared_examples 'does not update last_activity_on' do
|
|
|
|
it 'does not update user attribute' do
|
|
|
|
expect { subject.execute }.not_to change(user, :last_activity_on)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not track Snowplow event' do
|
|
|
|
subject.execute
|
|
|
|
|
|
|
|
expect_no_snowplow_event
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when last activity is nil' do
|
2018-11-18 11:00:15 +05:30
|
|
|
let(:last_activity_on) { nil }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
it 'updates last_activity_on for the user' do
|
|
|
|
expect { subject.execute }
|
|
|
|
.to change(user, :last_activity_on).from(last_activity_on).to(Date.today)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
context 'when last activity is in the past' do
|
|
|
|
let(:last_activity_on) { Date.today - 1.week }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
it 'updates last_activity_on for the user' do
|
|
|
|
expect { subject.execute }
|
|
|
|
.to change(user, :last_activity_on)
|
|
|
|
.from(last_activity_on)
|
|
|
|
.to(Date.today)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
it 'tries to obtain ExclusiveLease' do
|
2020-04-22 19:07:51 +05:30
|
|
|
expect(Gitlab::ExclusiveLease).to receive(:new).with("activity_service:#{user.id}", anything).and_call_original
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
subject.execute
|
|
|
|
end
|
2022-08-13 15:12:31 +05:30
|
|
|
|
|
|
|
it 'tracks RedisHLL event' do
|
|
|
|
expect(Gitlab::UsageDataCounters::HLLRedisCounter).to receive(:track_event)
|
|
|
|
.with('unique_active_user', values: user.id)
|
|
|
|
|
|
|
|
subject.execute
|
|
|
|
end
|
2023-04-23 21:23:45 +05:30
|
|
|
|
|
|
|
it_behaves_like 'Snowplow event tracking with RedisHLL context' do
|
|
|
|
subject(:record_activity) { described_class.new(author: user, namespace: namespace, project: project).execute }
|
|
|
|
|
|
|
|
let(:feature_flag_name) { :route_hll_to_snowplow_phase3 }
|
|
|
|
let(:category) { described_class.name }
|
|
|
|
let(:action) { 'perform_action' }
|
|
|
|
let(:label) { 'redis_hll_counters.manage.unique_active_users_monthly' }
|
|
|
|
let(:namespace) { build(:group) }
|
|
|
|
let(:project) { build(:project) }
|
|
|
|
let(:context) do
|
|
|
|
payload = Gitlab::Tracking::ServicePingContext.new(data_source: :redis_hll,
|
|
|
|
event: 'unique_active_user').to_context
|
|
|
|
[Gitlab::Json.dump(payload)]
|
|
|
|
end
|
|
|
|
end
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a bad object is passed' do
|
|
|
|
let(:fake_object) { double(username: 'hello') }
|
|
|
|
|
|
|
|
it 'does not record activity' do
|
2023-04-23 21:23:45 +05:30
|
|
|
service = described_class.new(author: fake_object)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
expect(service).not_to receive(:record_activity)
|
|
|
|
|
|
|
|
service.execute
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
context 'when last activity is today' do
|
|
|
|
let(:last_activity_on) { Date.today }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
it_behaves_like 'does not update last_activity_on'
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
it 'does not try to obtain ExclusiveLease' do
|
2020-04-22 19:07:51 +05:30
|
|
|
expect(Gitlab::ExclusiveLease).not_to receive(:new).with("activity_service:#{user.id}", anything)
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
subject.execute
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
context 'when in GitLab read-only instance' do
|
2018-11-18 11:00:15 +05:30
|
|
|
let(:last_activity_on) { nil }
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
before do
|
|
|
|
allow(Gitlab::Database).to receive(:read_only?).and_return(true)
|
|
|
|
end
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
it_behaves_like 'does not update last_activity_on'
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
context 'when a lease could not be obtained' do
|
|
|
|
let(:last_activity_on) { nil }
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
before do
|
2019-07-31 22:56:46 +05:30
|
|
|
stub_exclusive_lease_taken("activity_service:#{user.id}", timeout: 1.minute.to_i)
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
2023-04-23 21:23:45 +05:30
|
|
|
|
|
|
|
it_behaves_like 'does not update last_activity_on'
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
context 'with DB Load Balancing' do
|
2021-09-04 01:27:46 +05:30
|
|
|
let(:user) { create(:user, last_activity_on: last_activity_on) }
|
|
|
|
|
|
|
|
context 'when last activity is in the past' do
|
|
|
|
let(:user) { create(:user, last_activity_on: Date.today - 1.week) }
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
context 'database load balancing is configured' do
|
2021-09-04 01:27:46 +05:30
|
|
|
before do
|
2021-11-18 22:05:49 +05:30
|
|
|
::Gitlab::Database::LoadBalancing::Session.clear_session
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
let(:service) do
|
2023-04-23 21:23:45 +05:30
|
|
|
service = described_class.new(author: user)
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
::Gitlab::Database::LoadBalancing::Session.clear_session
|
|
|
|
|
|
|
|
service
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not stick to primary' do
|
|
|
|
expect(::Gitlab::Database::LoadBalancing::Session.current).not_to be_performed_write
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
|
|
|
|
expect(user.last_activity_on).to eq(Date.today)
|
|
|
|
expect(::Gitlab::Database::LoadBalancing::Session.current).to be_performed_write
|
|
|
|
expect(::Gitlab::Database::LoadBalancing::Session.current).not_to be_using_primary
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'database load balancing is not configured' do
|
2023-04-23 21:23:45 +05:30
|
|
|
let(:service) { described_class.new(author: user) }
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
it 'updates user without error' do
|
|
|
|
service.execute
|
|
|
|
|
|
|
|
expect(user.last_activity_on).to eq(Date.today)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|