Skip to content

Use `ref` in personal access tokens finder specs

Peter Leitzen requested to merge pl-spec-static-let-where into master

What does this MR do and why?

Use ref in personal access tokens finder specs

Previously, using tokens.keys in where made RSpec persist records in the database during load time (even with rspec --dry-run ...). Those records are run outside of a transaction and were never cleaned up.

This commit uses ref to reference the tokens keys lazily.

Found while running rspec --dry-run in #430870.

RSpec Profiling

Here's the output from test-prof when run with FPROF=1. Note:

  • Total amount of factories created went down from 850 to 13 🚀
  • Total events went down from 7697 to 447
  • Queries saved: 7250

Before

[TEST PROF INFO] RSpecDissect report

Total time: 00:27.909

Total `let` time: 00:26.030
Total `before(:each)` time: 00:22.584

Top 5 slowest suites (by `let` time):

PersonalAccessTokensFinder (./spec/finders/personal_access_tokens_finder_spec.rb:5) – 00:26.030 of 00:27.909 (51)
 ↳ tokens – 114
 ↳ subject – 51
 ↳ expected_tokens – 42

Top 5 slowest suites (by `before(:each)` time):

PersonalAccessTokensFinder (./spec/finders/personal_access_tokens_finder_spec.rb:5) – 00:22.584 of 00:27.909 (51)

[TEST PROF INFO] FactoryDoctor report

Total (potentially) bad examples: 3
Total wasted time: 00:01.276

PersonalAccessTokensFinder (./spec/finders/personal_access_tokens_finder_spec.rb:5) (35 records created, 00:01.276)
  returns tokens by users (./spec/finders/personal_access_tokens_finder_spec.rb:119) – 12 records created, 00:00.433
  returns no tokens (./spec/finders/personal_access_tokens_finder_spec.rb:64) – 12 records created, 00:00.469
  returns no tokens (./spec/finders/personal_access_tokens_finder_spec.rb:84) – 11 records created, 00:00.373


[TEST PROF INFO] EventProf results for sql.active_record

Total time: 00:12.721 of 00:28.271 (45.0%)
Total events: 7697

Top 5 slowest suites (by time):

PersonalAccessTokensFinder (./spec/finders/personal_access_tokens_finder_spec.rb:5) – 00:12.721 (7697 / 51) of 00:28.271 (45.0%)


Finished in 31.38 seconds (files took 33.75 seconds to load)
51 examples, 0 failures

Randomized with seed 2420

[TEST PROF INFO] Time spent in factories: 00:33.440 (59.1% of total time)
[TEST PROF INFO] Factories usage

 Total: 850
 Total top-level: 850
 Total time: 00:33.440 (out of 01:02.443)
 Total uniq factories: 4

   total   top-level     total time      time per call      top-level time               name

     528         528        5.9490s            0.0113s             5.9490s personal_access_token
     273         273       22.5661s            0.0827s            22.5661s               user
      48          48        4.9119s            0.1023s             4.9119s              admin
       1           1        0.0131s            0.0131s             0.0131s            license

After

Total time: 00:02.306

Total `let` time: 00:00.067
Total `before(:each)` time: 00:01.784

Top 5 slowest suites (by `let` time):

PersonalAccessTokensFinder (./spec/finders/personal_access_tokens_finder_spec.rb:5) – 00:00.067 of 00:02.306 (51)
 ↳ tokens – 63
 ↳ subject – 51
 ↳ expected_tokens – 42

Top 5 slowest suites (by `before(:each)` time):

PersonalAccessTokensFinder (./spec/finders/personal_access_tokens_finder_spec.rb:5) – 00:01.784 of 00:02.306 (51)

[TEST PROF INFO] FactoryDoctor says: "Looks good to me!"
[TEST PROF INFO] EventProf results for sql.active_record

Total time: 00:00.800 of 00:03.760 (21.29%)
Total events: 447

Top 5 slowest suites (by time):

PersonalAccessTokensFinder (./spec/finders/personal_access_tokens_finder_spec.rb:5) – 00:00.800 (447 / 51) of 00:03
.760 (21.29%)

Finished in 6.36 seconds (files took 12.08 seconds to load)
51 examples, 0 failures

Randomized with seed 59707

[TEST PROF INFO] Time spent in factories: 00:01.176 (12.84% of total time)
[TEST PROF INFO] Factories usage

 Total: 13
 Total top-level: 13
 Total time: 00:01.176 (out of 00:15.243)
 Total uniq factories: 4

   total   top-level     total time      time per call      top-level time               name

       8           8        0.1212s            0.0152s             0.1212s personal_access_token
       3           3        0.3077s            0.1026s             0.3077s               user
       1           1        0.7369s            0.7369s             0.7369s              admin
       1           1        0.0107s            0.0107s             0.0107s            license

How to set up and validate locally

  1. Bring test database into clean slate: rake db:reset RAILS_ENV=test
  2. Verify the database is empty:
$ gdk psql -d gitlabhq_test -c 'select count(*) from personal_access_tokens'
 count
-------
     0
(1 row)
  1. Run specs in dry mode via rspec --dry-run spec/finders/personal_access_tokens_finder_spec.rb
  2. Verify the database is empty:
$ gdk psql -d gitlabhq_test -c 'select count(*) from personal_access_tokens'
 count
-------
     0
(1 row)
  1. Checkout out master and run steps 1, 2, 3. Step 4 gives 120 rows
$ gdk psql -d gitlabhq_test -c 'select count(*) from personal_access_tokens'
 count
-------
   120
(1 row)

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Peter Leitzen

Merge request reports