-
Notifications
You must be signed in to change notification settings - Fork 25
feat: Implement transaction context #312
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
Merged
Merged
Changes from 17 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
264d8c9
Changing API client and added Context Propagator.
askpt c15192a
Merge branch 'main' into askpt/243-feature-implement-transaction-context
askpt 3539674
Merge branch 'main' into askpt/243-feature-implement-transaction-context
askpt 63c3f62
Merge branch 'main' into askpt/243-feature-implement-transaction-context
askpt 6a870ff
Adding NoOpTransactionContextPropagator.
askpt 8bcfc99
Making field readonly.
askpt 50613fe
Added an empty context return.
askpt b67b659
Adding unit tests.
askpt b0a20d7
Merge branch 'main' into askpt/243-feature-implement-transaction-context
askpt 02f93dd
Fix unit test.
askpt 8d45264
Adding clear transaction context to shutdown.
askpt 8e3789b
Merge branch 'main' into askpt/243-feature-implement-transaction-context
askpt d9ed730
Merging transaction context at evaluation.
askpt 9f75a44
Adding an AsyncLocalTransactionContextPropagator.
askpt e473ccc
Merge branch 'main' into askpt/243-feature-implement-transaction-context
askpt 4146f0e
Adding unit tests.
askpt 64f5c68
Adding missing tests.
askpt f461152
fixup: add docs
toddbaert bf4dbb4
Change visibility of GetPropagator.
askpt e480050
Merge branch 'main' into askpt/243-feature-implement-transaction-context
askpt 7a60997
Fixed comment.
askpt 1769be8
Replaced the context.
askpt 59ddca7
Fixing the context merging order. Updated unit test.
askpt 6d2d318
Merge branch 'main' into askpt/243-feature-implement-transaction-context
askpt 95c1fb7
Merge branch 'main' into askpt/243-feature-implement-transaction-context
askpt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| using System.Threading; | ||
| using OpenFeature.Model; | ||
|
|
||
| namespace OpenFeature; | ||
|
|
||
| /// <summary> | ||
| /// It is a no-op implementation of <see cref="ITransactionContextPropagator"/> | ||
beeme1mr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// It uses the <see cref="AsyncLocal{T}"/> to store the transaction context. | ||
| /// </summary> | ||
| public sealed class AsyncLocalTransactionContextPropagator : ITransactionContextPropagator | ||
| { | ||
| private readonly AsyncLocal<EvaluationContext> _transactionContext = new(); | ||
|
|
||
| /// <inheritdoc /> | ||
| public EvaluationContext GetTransactionContext() | ||
| { | ||
| return this._transactionContext.Value ?? EvaluationContext.Empty; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void SetTransactionContext(EvaluationContext evaluationContext) | ||
| { | ||
| this._transactionContext.Value = evaluationContext; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| namespace OpenFeature.Model; | ||
|
|
||
| /// <summary> | ||
| /// <see cref="ITransactionContextPropagator"/> is responsible for persisting a transactional context | ||
| /// for the duration of a single transaction. | ||
| /// Examples of potential transaction specific context include: a user id, user agent, IP. | ||
| /// Transaction context is merged with evaluation context prior to flag evaluation. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// The precedence of merging context can be seen in | ||
| /// <a href="https://openfeature.dev/specification/sections/evaluation-context#requirement-323">the specification</a>. | ||
| /// </remarks> | ||
| public interface ITransactionContextPropagator | ||
| { | ||
| /// <summary> | ||
| /// Returns the currently defined transaction context using the registered transaction context propagator. | ||
| /// </summary> | ||
| /// <returns><see cref="EvaluationContext"/>The current transaction context</returns> | ||
| EvaluationContext GetTransactionContext(); | ||
|
|
||
| /// <summary> | ||
| /// Sets the transaction context. | ||
| /// </summary> | ||
| /// <param name="evaluationContext">The transaction context to be set</param> | ||
| void SetTransactionContext(EvaluationContext evaluationContext); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| using OpenFeature.Model; | ||
|
|
||
| namespace OpenFeature; | ||
|
|
||
| internal class NoOpTransactionContextPropagator : ITransactionContextPropagator | ||
| { | ||
| public EvaluationContext GetTransactionContext() | ||
| { | ||
| return EvaluationContext.Empty; | ||
| } | ||
|
|
||
| public void SetTransactionContext(EvaluationContext evaluationContext) | ||
| { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
test/OpenFeature.Tests/AsyncLocalTransactionContextPropagatorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| using OpenFeature.Model; | ||
| using Xunit; | ||
|
|
||
| namespace OpenFeature.Tests; | ||
|
|
||
| public class AsyncLocalTransactionContextPropagatorTests | ||
| { | ||
| [Fact] | ||
| public void GetTransactionContext_ReturnsEmpty_WhenNoContextIsSet() | ||
| { | ||
| // Arrange | ||
| var propagator = new AsyncLocalTransactionContextPropagator(); | ||
|
|
||
| // Act | ||
| var context = propagator.GetTransactionContext(); | ||
|
|
||
| // Assert | ||
| Assert.Equal(EvaluationContext.Empty, context); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetTransactionContext_SetsAndGetsContextCorrectly() | ||
| { | ||
| // Arrange | ||
| var propagator = new AsyncLocalTransactionContextPropagator(); | ||
| var evaluationContext = EvaluationContext.Empty; | ||
beeme1mr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Act | ||
| propagator.SetTransactionContext(evaluationContext); | ||
| var context = propagator.GetTransactionContext(); | ||
|
|
||
| // Assert | ||
| Assert.Equal(evaluationContext, context); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetTransactionContext_OverridesPreviousContext() | ||
| { | ||
| // Arrange | ||
| var propagator = new AsyncLocalTransactionContextPropagator(); | ||
|
|
||
| var initialContext = EvaluationContext.Builder() | ||
| .Set("initial", "yes") | ||
| .Build(); | ||
| var newContext = EvaluationContext.Empty; | ||
|
|
||
| // Act | ||
| propagator.SetTransactionContext(initialContext); | ||
| propagator.SetTransactionContext(newContext); | ||
| var context = propagator.GetTransactionContext(); | ||
|
|
||
| // Assert | ||
| Assert.Equal(newContext, context); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.