Use FactoryDefault and let_it_be to speed up merge_request_spec
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
Merge request reports
Activity
changed milestone to %13.3
1 Warning Please add a throughput label to this merge request. 1 Message CHANGELOG missing: If you want to create a changelog entry for GitLab FOSS, run the following:
bin/changelog -m 39996 "Use FactoryDefault and let_it_be to speed up merge_request_spec"
If you want to create a changelog entry for GitLab EE, run the following instead:
bin/changelog --ee -m 39996 "Use FactoryDefault and let_it_be to speed up merge_request_spec"
If this merge request doesn’t need a CHANGELOG entry, feel free to ignore this message.
Reviewer roulette
Changes that require review have been detected! A merge request is normally reviewed by both a reviewer and a maintainer in its primary category (e.g. frontend or backend), and by a maintainer in all other categories.
To spread load more evenly across eligible reviewers, Danger has picked a candidate for each review slot, based on their timezone. Feel free to override these selections if you think someone else would be better-suited, or the chosen person is unavailable.
To read more on how to use the reviewer roulette, please take a look at the Engineering workflow and code review guidelines. Please consider assigning a reviewer or maintainer who is a domain expert in the area of the merge request.
Once you've decided who will review this merge request, mention them as you normally would! Danger does not automatically notify them for you.
Category Reviewer Maintainer backend Nicolas Dular ( @nicolasdular
) (UTC+2, 1 hour behind@igor.drozdov
)Ash McKenzie ( @ashmckenzie
) (UTC+10, 7 hours ahead of@igor.drozdov
)If needed, you can retry the
danger-review
job that generated this comment.Generated by
DangerEdited by 🤖 GitLab Bot 🤖Hi @igor.drozdov,
Please add labels to your merge request, this helps triage community merge requests.
Thanks for your help!
You are welcome to help improve this comment.
added auto updated label
Setting label groupsource code based on
@igor.drozdov
's group.added groupsource code label
added sectiondev label
changed milestone to %13.4
added missed:13.3 label
removed missed:13.3 label
added devopscreate rspec profiling labels and removed auto updated label
added 771 commits
-
aa7fea7e...54a24fb6 - 771 commits from branch
id-use-factory-default-for-pipelines
-
aa7fea7e...54a24fb6 - 771 commits from branch
added 167 commits
-
54a24fb6...b9c42a28 - 166 commits from branch
master
- 3b30daca - Use FactoryDefault to speed up merge_request_spec
-
54a24fb6...b9c42a28 - 166 commits from branch
- Resolved by Thong Kuah
@allison.browne could you please review this merge request?
assigned to @allison.browne
- Resolved by Igor Drozdov
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) } 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 filebin/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 KuahThis 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 tolet_it_be
because it creates a shared instance. We have a note regarding this (aboutreload
andrefind
): 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 thoughAlso, 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
I just ran into some confusion due to this. It seems like changes to thesubject.project
in earlier tests persist for later tests.Is this due to
factory_default: :keep
andcreate_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
.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 DuncalfeWell, 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
- Resolved by Thong Kuah
- Resolved by Igor Drozdov
unassigned @allison.browne
@tkuah could you please review this merge request?
assigned to @tkuah
For my reference: !39809 (merged) introduced
FactoryDefault
- Resolved by Thong Kuah
- Resolved by Thong Kuah
Thanks @igor.drozdov ! I see no issues with the MR itself - just have a question overall, which probably can be done later. See !39996 (comment 403552620)
unassigned @tkuah
assigned to @tkuah
enabled an automatic merge when the pipeline for fa7b3938 succeeds
mentioned in commit 5ec10aa4
added workflowstaging label
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.
This comment is generated automatically using the Release Tools project.added published label
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.
This comment is generated automatically using the Release Tools project.mentioned in merge request !51852 (merged)
mentioned in commit 957d598c
mentioned in merge request !53196 (merged)
mentioned in commit 628370a8