2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-05 10:41:32 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe Users::ActivityService do
|
2018-07-12 07:21:08 -04:00
|
|
|
include ExclusiveLeaseHelpers
|
2016-11-25 11:10:25 -05:00
|
|
|
|
2018-07-12 07:21:08 -04:00
|
|
|
let(:user) { create(:user, last_activity_on: last_activity_on) }
|
2016-11-25 11:10:25 -05:00
|
|
|
|
2020-01-06 04:07:42 -05:00
|
|
|
subject { described_class.new(user) }
|
2016-10-05 10:41:32 -04:00
|
|
|
|
2017-07-10 23:35:47 -04:00
|
|
|
describe '#execute', :clean_gitlab_redis_shared_state do
|
2016-10-05 10:41:32 -04:00
|
|
|
context 'when last activity is nil' do
|
2018-07-12 07:21:08 -04:00
|
|
|
let(:last_activity_on) { nil }
|
2016-10-05 10:41:32 -04:00
|
|
|
|
2018-07-12 07:21:08 -04:00
|
|
|
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)
|
2016-11-25 11:10:25 -05:00
|
|
|
end
|
2018-07-12 07:21:08 -04:00
|
|
|
end
|
2016-11-25 11:10:25 -05:00
|
|
|
|
2018-07-12 07:21:08 -04:00
|
|
|
context 'when last activity is in the past' do
|
|
|
|
let(:last_activity_on) { Date.today - 1.week }
|
2016-11-25 11:10:25 -05:00
|
|
|
|
2018-07-12 07:21:08 -04:00
|
|
|
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)
|
2016-10-05 10:41:32 -04:00
|
|
|
end
|
2019-02-07 09:36:50 -05:00
|
|
|
|
|
|
|
it 'tries to obtain ExclusiveLease' do
|
2020-04-01 11:07:45 -04:00
|
|
|
expect(Gitlab::ExclusiveLease).to receive(:new).with("activity_service:#{user.id}", anything).and_call_original
|
2019-02-07 09:36:50 -05:00
|
|
|
|
|
|
|
subject.execute
|
|
|
|
end
|
2018-07-12 07:21:08 -04:00
|
|
|
end
|
2016-10-05 10:41:32 -04:00
|
|
|
|
2018-08-02 14:17:00 -04:00
|
|
|
context 'when a bad object is passed' do
|
|
|
|
let(:fake_object) { double(username: 'hello') }
|
|
|
|
|
|
|
|
it 'does not record activity' do
|
2020-01-06 04:07:42 -05:00
|
|
|
service = described_class.new(fake_object)
|
2018-08-02 14:17:00 -04:00
|
|
|
|
|
|
|
expect(service).not_to receive(:record_activity)
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-12 07:21:08 -04:00
|
|
|
context 'when last activity is today' do
|
|
|
|
let(:last_activity_on) { Date.today }
|
2016-10-05 10:41:32 -04:00
|
|
|
|
2018-07-12 07:21:08 -04:00
|
|
|
it 'does not update last_activity_on' do
|
|
|
|
expect { subject.execute }.not_to change(user, :last_activity_on)
|
2016-10-05 10:41:32 -04:00
|
|
|
end
|
2019-02-07 09:36:50 -05:00
|
|
|
|
|
|
|
it 'does not try to obtain ExclusiveLease' do
|
2020-04-01 11:07:45 -04:00
|
|
|
expect(Gitlab::ExclusiveLease).not_to receive(:new).with("activity_service:#{user.id}", anything)
|
2019-02-07 09:36:50 -05:00
|
|
|
|
|
|
|
subject.execute
|
|
|
|
end
|
2016-10-05 10:41:32 -04:00
|
|
|
end
|
2017-09-19 03:44:58 -04:00
|
|
|
|
|
|
|
context 'when in GitLab read-only instance' do
|
2018-07-12 07:21:08 -04:00
|
|
|
let(:last_activity_on) { nil }
|
|
|
|
|
2017-09-19 03:44:58 -04:00
|
|
|
before do
|
2021-08-02 05:10:09 -04:00
|
|
|
allow(Gitlab::Database).to receive(:read_only?).and_return(true)
|
2017-09-19 03:44:58 -04:00
|
|
|
end
|
|
|
|
|
2018-07-12 07:21:08 -04:00
|
|
|
it 'does not update last_activity_on' do
|
|
|
|
expect { subject.execute }.not_to change(user, :last_activity_on)
|
2017-09-19 03:44:58 -04:00
|
|
|
end
|
|
|
|
end
|
2017-03-07 13:34:43 -05:00
|
|
|
|
2018-07-12 07:21:08 -04:00
|
|
|
context 'when a lease could not be obtained' do
|
|
|
|
let(:last_activity_on) { nil }
|
|
|
|
|
|
|
|
it 'does not update last_activity_on' do
|
2019-04-11 18:38:28 -04:00
|
|
|
stub_exclusive_lease_taken("activity_service:#{user.id}", timeout: 1.minute.to_i)
|
2018-07-12 07:21:08 -04:00
|
|
|
|
|
|
|
expect { subject.execute }.not_to change(user, :last_activity_on)
|
|
|
|
end
|
|
|
|
end
|
2017-03-07 13:34:43 -05:00
|
|
|
end
|
2021-06-02 08:10:05 -04:00
|
|
|
|
2021-08-05 14:10:13 -04:00
|
|
|
context 'with DB Load Balancing' do
|
2021-06-02 08:10:05 -04:00
|
|
|
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-08-05 14:10:13 -04:00
|
|
|
context 'database load balancing is configured', :db_load_balancing do
|
2021-06-02 08:10:05 -04:00
|
|
|
before do
|
|
|
|
allow(ActiveRecord::Base).to receive(:connection).and_return(::Gitlab::Database::LoadBalancing.proxy)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:service) do
|
|
|
|
service = described_class.new(user)
|
|
|
|
|
|
|
|
::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
|
|
|
|
let(:service) { described_class.new(user) }
|
|
|
|
|
|
|
|
it 'updates user without error' do
|
|
|
|
service.execute
|
|
|
|
|
|
|
|
expect(user.last_activity_on).to eq(Date.today)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-10-05 10:41:32 -04:00
|
|
|
end
|