Skip to content

Feature request: support clearing/scoped caches for testing #1092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 of 2 tasks
huonw opened this issue Apr 5, 2022 · 7 comments
Closed
1 of 2 tasks

Feature request: support clearing/scoped caches for testing #1092

huonw opened this issue Apr 5, 2022 · 7 comments
Assignees
Labels
feature-request feature request

Comments

@huonw
Copy link
Contributor

huonw commented Apr 5, 2022

Use case

We're testing our code with mocked AWS services using moto (rather than replacing/patching the calls entirely), and it works well. However, powertools provides various caches that are great for production, but cause unintended linkage/interference between tests. We're hitting this particularly in aws_lambda_powertools.utilities.parameters.get_secret, but it applies to other parts of that module, and likely any other global caches built into this package.

It'd be nice to be able to run tests isolated without having to manually write force_fetch=running_in_tests() or similar everywhere.

Solution/User Experience

Some method for clearing caches. For instance, a global aws_powertools_lambda.clear_caches() or aws_powertools_lambda.utilities.parameters.clear_caches(). Tests could then have set-up that calls that to ensure each one has a fresh environment.

Alternative solutions

Alternatives:

  • an environment variable that disables caching entirely, which can be set during tests runs (not so good because the deployed behaviour differs)
  • using contextvars to have the "global" caches actually be scope-able (this seems complicated)

Acknowledgment

@huonw huonw added feature-request feature request triage Pending triage from maintainers labels Apr 5, 2022
@huonw
Copy link
Contributor Author

huonw commented Apr 5, 2022

As workaround for now we've got a pytest fixture that pokes into the internals of this package:

import aws_lambda_powertools.utilities.parameters.base

# FIXME: https://github.com/awslabs/aws-lambda-powertools-python/issues/1092
@pytest.fixture(autouse=True)
def clear_powertools_cache() -> None:
    for v in aws_lambda_powertools.utilities.parameters.base.DEFAULT_PROVIDERS.values():
        v.store.clear()

@michaelbrewer
Copy link
Contributor

Uh, @huonw that makes sense, i have run into the same kind of issue, and did implement force_fetch option.

@michaelbrewer
Copy link
Contributor

This is closer to a bug, as testing should be something that is easier to do for parameters feature.

@huonw huonw changed the title Feature request: support clearing scoped/caches for testing Feature request: support clearing/scoped caches for testing Apr 12, 2022
@heitorlessa heitorlessa removed the triage Pending triage from maintainers label Apr 20, 2022
@heitorlessa
Copy link
Contributor

thanks for flagging it @huonw :) @mploski is working on our end-to-end tests POC, and we should be able to address it as part of a much wider testing improvements we're looking to make.

Thanks a lot again

@heitorlessa
Copy link
Contributor

heitorlessa commented Apr 29, 2022

Michal encountered a few performance challenges in the E2E POC which will delay working on this task. I'm gonna expedite it for today's release; PR incoming soon

@heitorlessa heitorlessa linked a pull request Apr 29, 2022 that will close this issue
7 tasks
@heitorlessa heitorlessa assigned heitorlessa and unassigned mploski Apr 29, 2022
@heitorlessa heitorlessa added the pending-release Fix or implementation already in dev waiting to be released label Apr 29, 2022
@heitorlessa
Copy link
Contributor

Done, should be available in today's release shortly.

image

@github-actions
Copy link
Contributor

This is now released under 1.25.10 version!

@github-actions github-actions bot removed the pending-release Fix or implementation already in dev waiting to be released label Apr 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request feature request
Projects
None yet
Development

No branches or pull requests

4 participants