Skip to content
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

[Enhancement] verify context handling #117

Open
pohly opened this issue Nov 1, 2023 · 4 comments
Open

[Enhancement] verify context handling #117

pohly opened this issue Nov 1, 2023 · 4 comments
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@pohly
Copy link

pohly commented Nov 1, 2023

Is your feature request related to a problem? Please describe.

In Kubernetes, we encourage writing tests so that each It callback gets a context from Ginkgo and then passed that context on to gomega.Eventually/Consistently. This is easy to get wrong, so we have instructions: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-testing/writing-good-e2e-tests.md#interrupting-tests

I am not sure how many of those gotchas can be detected through static analysis.

Not mentioned there (but probably should be):

It(func(ctx context.Context) {
   // Should pass ctx!
   Eventually(func() {
      // Should not use ctx from It, but rather from func(ctx context.Context) { ... }
      ... Get(ctx, ...)
   }).Should(...)
})

Describe the solution you'd like

  • Optional: require that Ginkgo callbacks take a context if the callback needs a context (i.e. has a context.TODO or context.Background or calls gomega.Eventually/Consistently)
  • Optional: require that gomega.Eventually/Consistently are called with a context (whether it is as first parameter or through WithContext (I think there is such a thing...)
  • Always: if gomega.Eventually/Consistently is called with a context, then it's callback should also accept a context if it needs one.
@pohly pohly added the enhancement New feature or request label Nov 1, 2023
@nunnatsa
Copy link
Owner

nunnatsa commented Nov 2, 2023

Hi @pohly - I think this is too specific to K8s. Not sure it's a generic ginkgo & gomega issue.

@pohly
Copy link
Author

pohly commented Nov 2, 2023

Is only Kubernetes using the context feature in Ginkgo? All other projects using it will have the same challenges.

@nunnatsa nunnatsa added the help wanted Extra attention is needed label Jan 2, 2024
@thediveo
Copy link

thediveo commented Feb 5, 2024

Another one who likes to shoot his own context feet. Yes, checking that if a context is used inside a function passed to Eventually should take the context from the function, not the closure; cherry on top: kick the stupid dev forgetting to pass the context into Eventually using WithContext...

@pohly
Copy link
Author

pohly commented Feb 6, 2024

@thediveo: Eventually(ctx, func(...)) also works. I find that more readable than Eventually(func(...)).WithContext(ctx).

Doesn't make the linter's job any easier, of course 😢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants