Skip to content
Snippets Groups Projects

Use FactoryDefault and let_it_be to speed up merge_request_spec

Merged Igor Drozdov requested to merge id-speed-up-merge-request-spec into master
1 unresolved thread

What does this MR do?

Before

  • Total events: 55131
  • Finished in 4 minutes 16.6 seconds
[TEST PROF INFO] Factories usage

 Total: 1534
 Total top-level: 810
 Total time: 151.3479s
 Total uniq factories: 26

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

     413           8       16.0975s            0.0390s             0.3009s          namespace
     405         185       68.4908s            0.1691s            31.2849s            project
     305         303      105.0823s            0.3445s           104.9606s      merge_request
      74          74        2.3823s            0.0322s             2.3823s               user
      60          28        1.5549s            0.0259s             0.9230s        ci_pipeline
      46          15        1.4492s            0.0315s             0.6800s           ci_build
      41          39        1.2494s            0.0305s             1.1276s              issue
      33          33        0.4072s            0.0123s             0.4072s        environment
      31          31        2.2126s            0.0714s             2.2126s         deployment
      29           8        1.3583s            0.0468s             0.0925s    ci_job_artifact
      23          23        0.4061s            0.0177s             0.4061s              group
      15          15        1.6344s            0.1090s             1.6344s  ci_empty_pipeline
      14          10        1.4333s            0.1024s             0.9896s diff_note_on_merge_request
       6           6        0.0540s            0.0090s             0.0540s draft_note_on_text_diff
       5           5        0.3547s            0.0709s             0.3547s closed_merge_request
       5           3        0.5551s            0.1110s             0.0532s               note
       5           5        0.1836s            0.0367s             0.1836s     note_on_commit
       4           4        0.4350s            0.1087s             0.4350s diff_note_on_commit
       4           4        0.1586s            0.0396s             0.1586s note_on_merge_request
       4           4        1.4422s            0.3605s             1.4422s merge_request_with_diff_notes
       3           3        0.1540s            0.0513s             0.1540s       jira_service
       3           0        0.0746s            0.0249s             0.0000s  jira_tracker_data
       2           2        0.9297s            0.4648s             0.9297s track_mr_picking_note
       2           0        0.5190s            0.2595s             0.0000s system_note_metadata
       1           1        0.0214s            0.0214s             0.0214s            license
       1           1        0.1601s            0.1601s             0.1601s      commit_status

After

  • Total events: 32709
  • Finished in 2 minutes 25.4 seconds
[TEST PROF INFO] Factories usage

 Total: 816
 Total top-level: 717
 Total time: 93.7881s
 Total uniq factories: 26

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

     298         296       65.5852s            0.2201s            65.3247s      merge_request
     106         106       14.8807s            0.1404s            14.8807s            project
      74          74        2.9002s            0.0392s             2.9002s               user
      60          28        1.2537s            0.0209s             0.8123s        ci_pipeline
      46          15        1.5796s            0.0343s             0.6796s           ci_build
      41          39        1.4867s            0.0363s             1.3043s              issue
      33          33        0.4179s            0.0127s             0.4179s        environment
      31          31        2.2461s            0.0725s             2.2461s         deployment
      29           8        1.3080s            0.0451s             0.0879s    ci_job_artifact
      23          23        0.4282s            0.0186s             0.4282s              group
      15          15        0.2465s            0.0164s             0.2465s  ci_empty_pipeline
      14          10        1.4360s            0.1026s             1.0658s diff_note_on_merge_request
       6           6        0.0575s            0.0096s             0.0575s draft_note_on_text_diff
       5           5        0.3683s            0.0737s             0.3683s closed_merge_request
       5           3        0.2939s            0.0588s             0.0611s               note
       5           5        0.1891s            0.0378s             0.1891s     note_on_commit
       4           4        0.4537s            0.1134s             0.4537s diff_note_on_commit
       4           4        0.0915s            0.0229s             0.0915s note_on_merge_request
       4           4        1.2535s            0.3134s             1.2535s merge_request_with_diff_notes
       3           3        0.1434s            0.0478s             0.1434s       jira_service
       3           0        0.0672s            0.0224s             0.0000s  jira_tracker_data
       2           2        0.5545s            0.2772s             0.5545s track_mr_picking_note
       2           0        0.2494s            0.1247s             0.0000s system_note_metadata
       1           1        0.0203s            0.0203s             0.0203s            license
       1           1        0.0220s            0.0220s             0.0220s      commit_status
       1           1        0.1792s            0.1792s             0.1792s          namespace

Queries saved: 22422

Edited by Igor Drozdov

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • 2991 2988 end
    2992 2989
    2993 2990 describe '#branch_merge_base_commit' do
    2991 let(:project) { create(:project, :repository) }
    2992
    2993 subject { create(:merge_request, :with_diffs, source_project: project) }
    • can this one use the default project?

    • Author Maintainer

      we using a separate project for this one because 3002th line changes the repository of the project and we don't want it to be done for the default project :sweat_smile:

    • Yeah, this is very interesting. If I use the default project on it's own (diff below), the spec passes when run by itself (bin/rspec spec/models/merge_request_spec.rb:2293) but the when running the whole file bin/rspec spec/models/merge_request_spec.rb, I see failures.

      This is potentially a source for confusion. Perhaps we can clearly document this failure mode in the guidelines for FactoryDefault ?

      Also, I wonder if we should restrict FactoryDefault to a few well-known factories like Project, Users, and Groups.

      $ git diff
      diff --git a/spec/models/merge_request_spec.rb b/spec/models/merge_request_spec.rb
      index f7caf13f729..ff7f67d5a9d 100644
      --- a/spec/models/merge_request_spec.rb
      +++ b/spec/models/merge_request_spec.rb
      @@ -2988,8 +2988,6 @@ def set_compare(merge_request)
         end
       
         describe '#branch_merge_base_commit' do
      -    let(:project) { create(:project, :repository) }
      -
           subject { create(:merge_request, :with_diffs, source_project: project) }
       
           context 'source and target branch exist' do
      Edited by Thong Kuah
    • Author Maintainer

      This is potentially a source for confusion. Perhaps we can clearly document this failure mode in the guidelines for FactoryDefault ?

      Actually, this problem is not specific to FactoryDefault, I'd say it's specific to let_it_be because it creates a shared instance. We have a note regarding this (about reload and refind): https://docs.gitlab.com/ee/development/testing_guide/best_practices.html#common-test-setup, but I guess refind/reload won't help if we deal with repository changes. It's probably worth noting in the docs though

      Also, I wonder if we should restrict FactoryDefault to a few well-known factories like Project, Users, and Groups.

      I think it's fine to get away without restrictions as long as we have it scoped to a single test suite :thinking:

    • I think it's fine to get away without restrictions as long as we have it scoped to a single test suite :thinking:

      Ok, let's give it a try and see what feedback we get, if any!

    • @tkuah @igor.drozdov

      :raised_back_of_hand: I just ran into some confusion due to this. It seems like changes to the subject.project in earlier tests persist for later tests.

      Is this due to factory_default: :keep and create_default(:project)?

      Just like @tkuah noticed, running the test in isolation will pass, but running as part of the whole spec will fail.

      All of this seems pretty bad to me :grimacing:.

      An example is:

      diff --git a/spec/models/merge_request_spec.rb b/spec/models/merge_request_spec.rb
      index 1cf197322f5..f88244a3bc3 100644
      --- a/spec/models/merge_request_spec.rb
      +++ b/spec/models/merge_request_spec.rb
      @@ -14,6 +14,17 @@
      
         subject { create(:merge_request) }
      
      +  it 'will always pass' do
      +    subject.project.description = 'test'
      +    subject.project.save
      +
      +    expect(subject.project.description).to eq('test')
      +  end
      +
      +  it 'fails when run in the full spec, but passes when run in isolation, ' \
      +     'or if `factory_default: :keep` is removed' do 
      +    expect(subject.project.description).not_to eq('test')
      +  end
      +
         describe 'associations' do
           subject { build_stubbed(:merge_request) }

      There are 3 tests that fail if we remove factory_default: :keep and I wonder whether those tests are poorly written and passing due to the accident of changes persisting in previous tests.

      Edited by Luke Duncalfe
    • @.luke @igor.drozdov

      Well, that sounds bad if there are specs that now depend on previous specs. Can we fix them ?

      https://github.com/test-prof/test-prof/blob/master/docs/recipes/factory_default.md has more about factory defaults

    • Please register or sign in to reply
  • Allison Browne
  • Allison Browne
  • Allison Browne approved this merge request

    approved this merge request

  • Author Maintainer

    @tkuah could you please review this merge request?

  • assigned to @tkuah

  • For my reference: !39809 (merged) introduced FactoryDefault

  • Thong Kuah
  • unassigned @tkuah

  • assigned to @tkuah

  • Thong Kuah resolved all threads

    resolved all threads

  • Thong Kuah approved this merge request

    approved this merge request

  • Thong Kuah enabled an automatic merge when the pipeline for fa7b3938 succeeds

    enabled an automatic merge when the pipeline for fa7b3938 succeeds

  • merged

  • Thong Kuah mentioned in commit 5ec10aa4

    mentioned in commit 5ec10aa4

  • added workflowcanary label and removed workflowstaging label

  • added workflowproduction label and removed workflowcanary label

  • This merge request has been deployed to the pre.gitlab.com environment, and will be included in the upcoming self-managed GitLab 13.5.0 release.


    :robot: This comment is generated automatically using the Release Tools project.

  • This merge request has been deployed to the release.gitlab.net environment, and will be included in the upcoming self-managed GitLab 13.5.0 release.


    :robot: This comment is generated automatically using the Release Tools project.

  • Luke Duncalfe mentioned in merge request !51852 (merged)

    mentioned in merge request !51852 (merged)

  • Luke Duncalfe mentioned in commit 957d598c

    mentioned in commit 957d598c

  • Luke Duncalfe mentioned in merge request !53196 (merged)

    mentioned in merge request !53196 (merged)

  • Luke Duncalfe mentioned in commit 628370a8

    mentioned in commit 628370a8

  • Please register or sign in to reply
    Loading