Commit 7ff6f507 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'sh-fix-last-activity-at-specs' into 'master'

Fix broken spec due to last_activity_at updates being throttled

In https://gitlab.com/gitlab-org/gitlab-ce/builds/4218398, the build failed because the last_activity_at column was only being updated once per hour. We
can fix this spec by stubbing out the throttling and adjusting the spec to test the right event timestamp.

See merge request !6424
parents ff49d28c cf9ee8fd
......@@ -330,13 +330,23 @@ class Event < ActiveRecord::Base
# Don't even bother obtaining a lock if the last update happened less than
# 60 minutes ago.
return if project.last_activity_at > RESET_PROJECT_ACTIVITY_INTERVAL.ago
return if recent_update?
return unless Gitlab::ExclusiveLease.
return unless try_obtain_lease
project.update_column(:last_activity_at, created_at)
end
private
def recent_update?
project.last_activity_at > RESET_PROJECT_ACTIVITY_INTERVAL.ago
end
def try_obtain_lease
Gitlab::ExclusiveLease.
new("project:update_last_activity_at:#{project.id}",
timeout: RESET_PROJECT_ACTIVITY_INTERVAL.to_i).
try_obtain
project.update_column(:last_activity_at, created_at)
end
end
FactoryGirl.define do
factory :event do
project
author factory: :user
factory :closed_issue_event do
project
action { Event::CLOSED }
target factory: :closed_issue
author factory: :user
end
end
end
......@@ -308,20 +308,23 @@ describe Project, models: true do
end
describe 'last_activity methods' do
let(:project) { create(:project) }
let(:last_event) { double(created_at: Time.now) }
let(:timestamp) { Time.now - 2.hours }
let(:project) { create(:project, created_at: timestamp, updated_at: timestamp) }
describe 'last_activity' do
it 'alias last_activity to last_event' do
allow(project).to receive(:last_event).and_return(last_event)
last_event = create(:event, project: project)
expect(project.last_activity).to eq(last_event)
end
end
describe 'last_activity_date' do
it 'returns the creation date of the project\'s last event if present' do
create(:event, project: project)
expect(project.last_activity_at.to_i).to eq(last_event.created_at.to_i)
expect_any_instance_of(Event).to receive(:try_obtain_lease).and_return(true)
new_event = create(:event, project: project, created_at: Time.now)
expect(project.last_activity_at.to_i).to eq(new_event.created_at.to_i)
end
it 'returns the project\'s last update date if it has no events' do
......
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