Feature: Allow user to specify to not reset boto3-Sessions #7287
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When creating boto3-client for the first time,
boto3
will create a default session that caches all kinds of things - including credentials. Subsequent boto3-clients will reuse thatSession
.If the first test in your test suite is mocked, the default
Session
created in that test will have fake credentials, as supplied by Moto. But if the next test is not mocked and should reach out to AWS, it would do so with the mocked credentials from our cachedSession
.That is why Moto resets the
boto3-Session
, to make sure that it is recreated with the correct credentials (either fake or real) everytime. (This was introduced in #2578) It does come at a cost though, as instantiating a new boto3-Session is an expensive operation.If all of your tests use Moto, and you never want to reach out to AWS, you can choose to not reset the
boto3-session
. New boto3-clients that are created will reuse theboto3-Session
(with fake credentials), making Moto much more performant.@mock_aws(config={"core": {"reset_boto3_session": False}})
All of our tests are mocked, so it's safe to disable this for every test - but we really don't want to set this value for every single test individually. That's why, as part of this PR, we just override the default config to always set this value to false (in
tests/__init__.py
).Test runtime drops from ~30 mins to <15 mins with this change.