From cf9ee8fda7dbb42f388f0044b22bd3ff2b30048c Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Mon, 19 Sep 2016 19:59:29 -0700 Subject: [PATCH] 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. --- app/models/event.rb | 20 +++++++++++++++----- spec/factories/events.rb | 5 +++-- spec/models/project_spec.rb | 13 ++++++++----- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/app/models/event.rb b/app/models/event.rb index b6e8bef3f67..55a76e26f3c 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -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. - new("project:update_last_activity_at:#{project.id}", - timeout: RESET_PROJECT_ACTIVITY_INTERVAL.to_i). - try_obtain + 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 + end end diff --git a/spec/factories/events.rb b/spec/factories/events.rb index 90788f30ac9..8820d527c61 100644 --- a/spec/factories/events.rb +++ b/spec/factories/events.rb @@ -1,10 +1,11 @@ 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 diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 7ca1bd1e5c9..a388ff703a6 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -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