Avoid policy stubbing in ProtectedBranch related specs
What does this MR do and why?
- Stub
Ability#allowed?
instead of policy classes directly - Speed up specs by using
let_it_be
andbefore_all
(see below)
Extracted from !94135 (merged).
RSpec Profiling
Here's the output from test-prof
when run with FPROF=1
. Note:
-
Total amount
of factories created went down from 210 to 92🚀 -
Total events
went down from 8363 to 2537 - Queries saved: 5826
Before
[TEST PROF INFO] FactoryDoctor report
Total (potentially) bad examples: 3
Total wasted time: 00:01.016
ProtectedBranches::DestroyService (./spec/services/protected_branches/destroy_service_spec.rb:5) (3 records created, 00:00.348)
prevents deletion of the protected branch rule (./spec/services/protected_branches/destroy_service_spec.rb:33) – 3 records created, 00:00.348
ProtectedBranches::UpdateService (./spec/services/protected_branches/update_service_spec.rb:5) (3 records created, 00:00.399)
prevents creation of the protected branch rule (./spec/services/protected_branches/update_service_spec.rb:54) – 3 records created, 00:00.399
ProtectedBranches::CreateService (./spec/services/protected_branches/create_service_spec.rb:5) (2 records created, 00:00.268)
prevents creation of the protected branch rule (./spec/services/protected_branches/create_service_spec.rb:68) – 2 records created, 00:00.268
[TEST PROF INFO] EventProf results for sql.active_record
Total time: 00:12.025 of 01:08.025 (17.68%)
Total events: 8363
Top 5 slowest suites (by time):
API::ProtectedBranches (./spec/requests/api/protected_branches_spec.rb:5) – 00:07.627 (5412 / 28) of 00:49.409 (15.44%)
Projects::Pro...chesController (./spec/controllers/projects/protected_branches_controller_spec.rb:5) – 00:01.810 (1248 / 7) of 00:10.752 (16.83%)
ProtectedBran...:CreateService (./spec/services/protected_branches/create_service_spec.rb:5) – 00:00.955 (762 / 6) of 00:02.397 (39.87%)
ProtectedBran...DestroyService (./spec/services/protected_branches/destroy_service_spec.rb:5) – 00:00.827 (390 / 3) of 00:02.989 (27.69%)
ProtectedBran...:UpdateService (./spec/services/protected_branches/update_service_spec.rb:5) – 00:00.803 (551 / 5) of 00:02.475 (32.47%)
Finished in 1 minute 15.62 seconds (files took 7.42 seconds to load)
49 examples, 0 failures
Randomized with seed 8109
[TEST PROF INFO] Factories usage
Total: 210
Total top-level: 153
Total time: 00:53.391 (out of 01:20.760)
Total uniq factories: 7
total top-level total time time per call top-level time name
49 0 6.2922s 0.1284s 0.0000s namespace
48 40 47.7162s 0.9941s 44.1263s project
41 41 4.8645s 0.1186s 4.8645s protected_branch
38 38 3.6768s 0.0968s 3.6768s user
28 28 0.2773s 0.0099s 0.2773s personal_access_token
5 5 0.0418s 0.0084s 0.0418s license
1 1 0.4046s 0.4046s 0.4046s project_empty_repo
After
[TEST PROF INFO] FactoryDoctor says: "Looks good to me!"
[TEST PROF INFO] EventProf results for sql.active_record
Total time: 00:03.513 of 00:18.440 (19.05%)
Total events: 2537
Top 5 slowest suites (by time):
API::ProtectedBranches (./spec/requests/api/protected_branches_spec.rb:5) – 00:01.724 (1285 / 28) of 00:11.271 (15.3%)
ProtectedBran...:CreateService (./spec/services/protected_branches/create_service_spec.rb:5) – 00:00.783 (408 / 6) of 00:02.444 (32.04%)
Projects::Pro...chesController (./spec/controllers/projects/protected_branches_controller_spec.rb:5) – 00:00.505 (451 / 7) of 00:03.035 (16.64%)
ProtectedBran...:UpdateService (./spec/services/protected_branches/update_service_spec.rb:5) – 00:00.255 (221 / 5) of 00:00.961 (26.61%)
ProtectedBran...DestroyService (./spec/services/protected_branches/destroy_service_spec.rb:5) – 00:00.245 (172 / 3) of 00:00.726 (33.72%)
Finished in 30.32 seconds (files took 9 seconds to load)
49 examples, 0 failures
Randomized with seed 40434
[TEST PROF INFO] Factories usage
Total: 92
Total top-level: 86
Total time: 00:06.771 (out of 00:36.834)
Total uniq factories: 7
total top-level total time time per call top-level time name
41 41 1.1618s 0.0283s 1.1618s protected_branch
28 28 0.2808s 0.0100s 0.2808s personal_access_token
6 0 0.7869s 0.1312s 0.0000s namespace
6 6 0.5683s 0.0947s 0.5683s user
5 5 0.0416s 0.0083s 0.0416s license
5 5 4.4265s 0.8853s 4.4265s project
1 1 0.2923s 0.2923s 0.2923s project_empty_repo
How to set up and validate locally
bin/rspec spec/controllers/projects/protected_branches_controller_spec.rb spec/requests/api/protected_branches_spec.rb spec/services/protected_branches/create_service_spec.rb spec/services/protected_branches/destroy_service_spec.rb spec/services/protected_branches/update_service_spec.rb
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.
Edited by Peter Leitzen