diff options
author | James Lopez <james@jameslopez.es> | 2016-11-25 19:10:25 +0300 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2017-04-14 16:20:55 +0300 |
commit | 3cb84e06b7a118fb46b4e1e0d4885026c9d4a4d1 (patch) | |
tree | ef622687724fd429f6f7fb02a19a022550bf8608 /spec | |
parent | 2951a8543ef97ceb1bcaca5f5140d822729c950b (diff) |
Remove user activities table and use redis instead of PG for recording activities
Refactored specs and added a post deployment migration to remove the activity users table.
Diffstat (limited to 'spec')
-rw-r--r-- | spec/controllers/sessions_controller_spec.rb | 6 | ||||
-rw-r--r-- | spec/factories/user_activities.rb | 6 | ||||
-rw-r--r-- | spec/requests/api/internal_spec.rb | 20 | ||||
-rw-r--r-- | spec/services/event_create_service_spec.rb | 28 | ||||
-rw-r--r-- | spec/services/users/activity_service_spec.rb | 34 | ||||
-rw-r--r-- | spec/support/user_activities_helpers.rb | 17 |
6 files changed, 72 insertions, 39 deletions
diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb index 3459f30ef42..d817099394a 100644 --- a/spec/controllers/sessions_controller_spec.rb +++ b/spec/controllers/sessions_controller_spec.rb @@ -16,7 +16,9 @@ describe SessionsController do end end - context 'when using valid password' do + context 'when using valid password', :redis do + include UserActivitiesHelpers + let(:user) { create(:user) } it 'authenticates user correctly' do @@ -41,7 +43,7 @@ describe SessionsController do it 'updates the user activity' do expect do post(:create, user: { login: user.username, password: user.password }) - end.to change { user.reload.last_activity_at }.from(nil) + end.to change { user_score }.from(0) end end end diff --git a/spec/factories/user_activities.rb b/spec/factories/user_activities.rb deleted file mode 100644 index 32ad8c6a3b2..00000000000 --- a/spec/factories/user_activities.rb +++ /dev/null @@ -1,6 +0,0 @@ -FactoryGirl.define do - factory :user_activity do - last_activity_at { Time.now } - user - end -end diff --git a/spec/requests/api/internal_spec.rb b/spec/requests/api/internal_spec.rb index 63f566da7a8..eee8bd51bff 100644 --- a/spec/requests/api/internal_spec.rb +++ b/spec/requests/api/internal_spec.rb @@ -147,7 +147,9 @@ describe API::Internal, api: true do end end - describe "POST /internal/allowed" do + describe "POST /internal/allowed", :redis do + include UserActivitiesHelpers + context "access granted" do before do project.team << [user, :developer] @@ -181,7 +183,7 @@ describe API::Internal, api: true do expect(response).to have_http_status(200) expect(json_response["status"]).to be_truthy expect(json_response["repository_path"]).to eq(project.wiki.repository.path_to_repo) - expect(key.user.reload.last_activity_at.to_i).to eq(Time.now.to_i) + expect(user_score).to be_zero end end @@ -192,7 +194,7 @@ describe API::Internal, api: true do expect(response).to have_http_status(200) expect(json_response["status"]).to be_truthy expect(json_response["repository_path"]).to eq(project.wiki.repository.path_to_repo) - expect(key.user.reload.last_activity_at.to_i).to eq(Time.now.to_i) + expect(user_score).not_to be_zero end end @@ -203,7 +205,7 @@ describe API::Internal, api: true do expect(response).to have_http_status(200) expect(json_response["status"]).to be_truthy expect(json_response["repository_path"]).to eq(project.repository.path_to_repo) - expect(key.user.reload.last_activity_at.to_i).to eq(Time.now.to_i) + expect(user_score).not_to be_zero end end @@ -214,7 +216,7 @@ describe API::Internal, api: true do expect(response).to have_http_status(200) expect(json_response["status"]).to be_truthy expect(json_response["repository_path"]).to eq(project.repository.path_to_repo) - expect(key.user.reload.last_activity_at.to_i).to eq(Time.now.to_i) + expect(user_score).to be_zero end context 'project as /namespace/project' do @@ -250,7 +252,7 @@ describe API::Internal, api: true do expect(response).to have_http_status(200) expect(json_response["status"]).to be_falsey - expect(key.user.reload.last_activity_at).to be_nil + expect(user_score).to be_zero end end @@ -260,7 +262,7 @@ describe API::Internal, api: true do expect(response).to have_http_status(200) expect(json_response["status"]).to be_falsey - expect(key.user.reload.last_activity_at).to be_nil + expect(user_score).to be_zero end end end @@ -278,7 +280,7 @@ describe API::Internal, api: true do expect(response).to have_http_status(200) expect(json_response["status"]).to be_falsey - expect(key.user.reload.last_activity_at).to be_nil + expect(user_score).to be_zero end end @@ -288,7 +290,7 @@ describe API::Internal, api: true do expect(response).to have_http_status(200) expect(json_response["status"]).to be_falsey - expect(key.user.reload.last_activity_at).to be_nil + expect(user_score).to be_zero end end end diff --git a/spec/services/event_create_service_spec.rb b/spec/services/event_create_service_spec.rb index 54e5c0b236b..13c0aac2363 100644 --- a/spec/services/event_create_service_spec.rb +++ b/spec/services/event_create_service_spec.rb @@ -1,6 +1,8 @@ require 'spec_helper' describe EventCreateService, services: true do + include UserActivitiesHelpers + let(:service) { EventCreateService.new } describe 'Issues' do @@ -111,6 +113,19 @@ describe EventCreateService, services: true do end end + describe '#push', :redis do + let(:project) { create(:empty_project) } + let(:user) { create(:user) } + + it 'creates a new event' do + expect { service.push(project, user, {}) }.to change { Event.count } + end + + it 'updates user last activity' do + expect { service.push(project, user, {}) }.to change { user_score } + end + end + describe 'Project' do let(:user) { create :user } let(:project) { create(:empty_project) } @@ -129,17 +144,4 @@ describe EventCreateService, services: true do it { expect { subject }.to change { Event.count }.from(0).to(1) } end end - - describe '#push' do - let(:project) { create(:empty_project) } - let(:user) { create(:user) } - - it 'creates a new event' do - expect { service.push(project, user, {}) }.to change { Event.count } - end - - it 'updates user last activity' do - expect { service.push(project, user, {}) }.to change { user.last_activity_at } - end - end end diff --git a/spec/services/users/activity_service_spec.rb b/spec/services/users/activity_service_spec.rb index 68399118579..07715ad4ca0 100644 --- a/spec/services/users/activity_service_spec.rb +++ b/spec/services/users/activity_service_spec.rb @@ -1,24 +1,40 @@ require 'spec_helper' describe Users::ActivityService, services: true do + include UserActivitiesHelpers + let(:user) { create(:user) } + subject(:service) { described_class.new(user, 'type') } - describe '#execute' do + describe '#execute', :redis do context 'when last activity is nil' do - it 'sets the last activity timestamp' do + before do service.execute + end - expect(user.last_activity_at).not_to be_nil + it 'sets the last activity timestamp for the user' do + expect(last_hour_members).to eq([user.username]) + end + + it 'updates the same user' do + service.execute + + expect(last_hour_members).to eq([user.username]) + end + + it 'updates the timestamp of an existing user' do + Timecop.freeze(Date.tomorrow) do + expect { service.execute }.to change { user_score }.to(Time.now.to_i) + end end - end - context 'when activity_at is not nil' do - it 'updates the activity multiple times' do - activity = create(:user_activity, user: user) + describe 'other user' do + it 'updates other user' do + other_user = create(:user) + described_class.new(other_user, 'type').execute - Timecop.travel(activity.last_activity_at + 1.minute) do - expect { service.execute }.to change { user.reload.last_activity_at } + expect(last_hour_members).to match_array([user.username, other_user.username]) end end end diff --git a/spec/support/user_activities_helpers.rb b/spec/support/user_activities_helpers.rb new file mode 100644 index 00000000000..ef88dab6c91 --- /dev/null +++ b/spec/support/user_activities_helpers.rb @@ -0,0 +1,17 @@ +module UserActivitiesHelpers + def last_hour_members + Gitlab::Redis.with do |redis| + redis.zrangebyscore(user_activities_key, 1.hour.ago.to_i, Time.now.to_i) + end + end + + def user_score + Gitlab::Redis.with do |redis| + redis.zscore(user_activities_key, user.username).to_i + end + end + + def user_activities_key + 'user/activities' + end +end |