diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-04-19 15:09:04 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-04-19 15:09:04 +0300 |
commit | c6af94ea4ea649171ff930b6bf94c73a5d03edb9 (patch) | |
tree | ceef77238b3a275a3a32b4e9f982b6d2f27e0c6b /spec/services | |
parent | 3257ae3af07a4ad026be3c868e74ff82866fc400 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/services')
-rw-r--r-- | spec/services/todo_service_spec.rb | 18 | ||||
-rw-r--r-- | spec/services/users/update_todo_count_cache_service_spec.rb | 61 |
2 files changed, 69 insertions, 10 deletions
diff --git a/spec/services/todo_service_spec.rb b/spec/services/todo_service_spec.rb index 227f39abd0b..59f936509df 100644 --- a/spec/services/todo_service_spec.rb +++ b/spec/services/todo_service_spec.rb @@ -3,6 +3,8 @@ require 'spec_helper' RSpec.describe TodoService do + include AfterNextHelpers + let_it_be(:project) { create(:project, :repository) } let_it_be(:author) { create(:user) } let_it_be(:assignee) { create(:user) } @@ -343,19 +345,19 @@ RSpec.describe TodoService do describe '#destroy_target' do it 'refreshes the todos count cache for users with todos on the target' do - create(:todo, target: issue, user: john_doe, author: john_doe, project: issue.project) + create(:todo, state: :pending, target: issue, user: john_doe, author: john_doe, project: issue.project) - expect_any_instance_of(User).to receive(:update_todos_count_cache).and_call_original + expect_next(Users::UpdateTodoCountCacheService, [john_doe]).to receive(:execute) - service.destroy_target(issue) { } + service.destroy_target(issue) { issue.destroy! } end it 'does not refresh the todos count cache for users with only done todos on the target' do create(:todo, :done, target: issue, user: john_doe, author: john_doe, project: issue.project) - expect_any_instance_of(User).not_to receive(:update_todos_count_cache) + expect(Users::UpdateTodoCountCacheService).not_to receive(:new) - service.destroy_target(issue) { } + service.destroy_target(issue) { issue.destroy! } end it 'yields the target to the caller' do @@ -1099,13 +1101,9 @@ RSpec.describe TodoService do it 'updates cached counts when a todo is created' do issue = create(:issue, project: project, assignees: [john_doe], author: author) - expect(john_doe.todos_pending_count).to eq(0) - expect(john_doe).to receive(:update_todos_count_cache).and_call_original + expect_next(Users::UpdateTodoCountCacheService, [john_doe]).to receive(:execute) service.new_issue(issue, author) - - expect(Todo.where(user_id: john_doe.id, state: :pending).count).to eq 1 - expect(john_doe.todos_pending_count).to eq(1) end shared_examples 'updating todos state' do |state, new_state, new_resolved_by = nil| diff --git a/spec/services/users/update_todo_count_cache_service_spec.rb b/spec/services/users/update_todo_count_cache_service_spec.rb new file mode 100644 index 00000000000..3e3618b1291 --- /dev/null +++ b/spec/services/users/update_todo_count_cache_service_spec.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Users::UpdateTodoCountCacheService do + describe '#execute' do + let_it_be(:user1) { create(:user) } + let_it_be(:user2) { create(:user) } + + let_it_be(:todo1) { create(:todo, user: user1, state: :done) } + let_it_be(:todo2) { create(:todo, user: user1, state: :done) } + let_it_be(:todo3) { create(:todo, user: user1, state: :pending) } + let_it_be(:todo4) { create(:todo, user: user2, state: :done) } + let_it_be(:todo5) { create(:todo, user: user2, state: :pending) } + let_it_be(:todo6) { create(:todo, user: user2, state: :pending) } + + it 'updates the todos_counts for users', :use_clean_rails_memory_store_caching do + Rails.cache.write(['users', user1.id, 'todos_done_count'], 0) + Rails.cache.write(['users', user1.id, 'todos_pending_count'], 0) + Rails.cache.write(['users', user2.id, 'todos_done_count'], 0) + Rails.cache.write(['users', user2.id, 'todos_pending_count'], 0) + + expect { described_class.new([user1, user2]).execute } + .to change(user1, :todos_done_count).from(0).to(2) + .and change(user1, :todos_pending_count).from(0).to(1) + .and change(user2, :todos_done_count).from(0).to(1) + .and change(user2, :todos_pending_count).from(0).to(2) + + Todo.delete_all + + expect { described_class.new([user1, user2]).execute } + .to change(user1, :todos_done_count).from(2).to(0) + .and change(user1, :todos_pending_count).from(1).to(0) + .and change(user2, :todos_done_count).from(1).to(0) + .and change(user2, :todos_pending_count).from(2).to(0) + end + + it 'avoids N+1 queries' do + control_count = ActiveRecord::QueryRecorder.new { described_class.new([user1]).execute }.count + + expect { described_class.new([user1, user2]).execute }.not_to exceed_query_limit(control_count) + end + + it 'executes one query per batch of users' do + stub_const("#{described_class}::QUERY_BATCH_SIZE", 1) + + expect(ActiveRecord::QueryRecorder.new { described_class.new([user1]).execute }.count).to eq(1) + expect(ActiveRecord::QueryRecorder.new { described_class.new([user1, user2]).execute }.count).to eq(2) + end + + it 'sets the cache expire time to the users count_cache_validity_period' do + allow(user1).to receive(:count_cache_validity_period).and_return(1.minute) + allow(user2).to receive(:count_cache_validity_period).and_return(1.hour) + + expect(Rails.cache).to receive(:write).with(['users', user1.id, anything], anything, expires_in: 1.minute).twice + expect(Rails.cache).to receive(:write).with(['users', user2.id, anything], anything, expires_in: 1.hour).twice + + described_class.new([user1, user2]).execute + end + end +end |