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