Skip to content

fix(testing): make test provider context-aware for goroutine#470

Open
erka wants to merge 5 commits intomainfrom
rd/testing-context
Open

fix(testing): make test provider context-aware for goroutine#470
erka wants to merge 5 commits intomainfrom
rd/testing-context

Conversation

@erka
Copy link
Member

@erka erka commented Feb 4, 2026

This PR

  • Added context-based value resolution for TestProvider.
  • Preserved existing goroutine-local behavior as a fallback.

Related issues

closes #436

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @erka, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refines the TestProvider in the OpenFeature Go SDK to correctly manage test contexts across goroutines. By making the provider context-aware and integrating with Go's context package, it resolves potential issues where flag evaluations in concurrent operations might fail to retrieve the correct test-specific flag configurations. This change improves the reliability and usability of the testing framework for asynchronous and concurrent test scenarios.

Highlights

  • Context-aware TestProvider: The TestProvider has been enhanced to be context-aware, allowing it to correctly identify the test scope even when flag evaluations occur in different goroutines.
  • UsingFlags returns Context: The TestProvider.UsingFlags method now returns a context.Context which should be used for flag evaluations within goroutines to ensure proper test context propagation.
  • Automatic Cleanup Integration: The TestProvider now integrates with testing.TB.Cleanup, automatically cleaning up the provider at the end of a test, reducing boilerplate.
  • Documentation Update: The README.md has been updated with new documentation and code examples demonstrating how to use the context-aware TestProvider for tests involving goroutines.
  • New Test Cases: New test cases have been added to testprovider_test.go to validate the context-aware behavior of the TestProvider in concurrent scenarios, such as HTTP handlers and multiple goroutines.
Changelog
  • README.md
    • Added a new section with code examples explaining how to use TestProvider.UsingFlags with goroutines for context-aware flag evaluations.
    • Introduced contextAwareTest struct in documentation for demonstrating context propagation.
    • Performed minor formatting adjustments to tables and spacing.
  • openfeature/testing/testprovider.go
    • Changed testNameKey to a contextKey type for improved type safety when storing in context.Context.
    • Modified TestProvider.UsingFlags to return a context.Context containing the test name.
    • Introduced testFrameworkWithContext interface to allow testing.T to provide a context for UsingFlags.
    • Implemented automatic cleanup for TestProvider by integrating with testing.TB.Cleanup.
    • Updated all Evaluation methods to pass the context.Context to getProvider.
    • Modified getProvider to prioritize retrieving the test name from the provided context.Context before falling back to goroutine-local storage.
  • openfeature/testing/testprovider_test.go
    • Added new imports for net/http, net/http/httptest, sync, time, assert, and require.
    • Introduced TestServeWithAnotherGoroutine with two sub-tests to verify context propagation in concurrent HTTP handler and multiple goroutine scenarios.
Activity
  • The pull request introduces a fix to make the test provider context-aware, specifically for scenarios involving goroutines.
  • The core changes involve modifying the TestProvider to return and utilize context.Context for test name resolution.
  • Documentation has been updated to guide users on how to leverage this new context-aware functionality.
  • New tests have been added to validate the fix and demonstrate correct usage in concurrent environments.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@codecov
Copy link

codecov bot commented Feb 4, 2026

Codecov Report

❌ Patch coverage is 62.96296% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.65%. Comparing base (3bfdb3e) to head (f36e3f2).

Files with missing lines Patch % Lines
openfeature/testing/testprovider.go 62.96% 6 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #470      +/-   ##
==========================================
- Coverage   83.21%   82.65%   -0.57%     
==========================================
  Files          27       27              
  Lines        2109     2127      +18     
==========================================
+ Hits         1755     1758       +3     
- Misses        305      311       +6     
- Partials       49       58       +9     
Flag Coverage Δ
e2e 82.65% <62.96%> (-0.57%) ⬇️
unit 82.65% <62.96%> (-0.57%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request makes the TestProvider context-aware, which is a great improvement for writing tests with goroutines. The implementation correctly uses context.Context to pass test-specific data, and maintains backward compatibility with the previous goroutine-local storage mechanism. The addition of automatic cleanup via t.Cleanup is also a nice touch for usability. I have a couple of suggestions to improve the documentation and fix a minor typo.

Comment on lines +38 to +40
// testFrameworkWithContext is an optional interface for tests that can provide
// a context, enabling context-aware evaluation when flags are used across goroutines.
type testFrameworkWithContext interface {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the purpose of this secondary interface? why not just update TestFramework to contain Context() context.Context? it'd be much simpler that way...

for that matter, why not use testing.TB instead? the TestFramework interface seems like an unnecessary abstraction.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would love to use testing.TB, but there will likely be someone who doesn’t use it in a specific case, and their tests or code might fail. Since go-sdk is already at v1, that could make people unhappy. So I work around...

erka and others added 5 commits March 6, 2026 00:27
Signed-off-by: Roman Dmytrenko <rdmytrenko@gmail.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: Roman Dmytrenko <rdmytrenko@gmail.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: Roman Dmytrenko <rdmytrenko@gmail.com>
Signed-off-by: Roman Dmytrenko <rdmytrenko@gmail.com>
Signed-off-by: Roman Dmytrenko <rdmytrenko@gmail.com>
@erka erka force-pushed the rd/testing-context branch from c01a6ff to f36e3f2 Compare March 6, 2026 00:29
@erka erka marked this pull request as ready for review March 6, 2026 00:37
@erka erka requested review from a team as code owners March 6, 2026 00:37
@erka erka requested a review from hairyhenderson March 6, 2026 00:37
@erka erka added v1 bug Something isn't working labels Mar 6, 2026
Comment on lines +105 to +108
//
if ctx == nil {
ctx = context.Background()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove this. If ctx is nil here, then it's a programmer error that would be masked by silent substitution with context.Background().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working v1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] testing.TestProvider can't reliably be used when tests invoke separate goroutines

3 participants