-
Notifications
You must be signed in to change notification settings - Fork 39
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
fix: updated context not passed to all hooks #1049
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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 |
---|---|---|
|
@@ -2,7 +2,6 @@ | |
|
||
import static dev.openfeature.sdk.DoSomethingProvider.DEFAULT_METADATA; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatCode; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
@@ -20,6 +19,7 @@ | |
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import org.awaitility.Awaitility; | ||
import org.junit.jupiter.api.AfterEach; | ||
|
@@ -336,11 +336,25 @@ public void initialize(EvaluationContext evaluationContext) throws Exception { | |
FeatureProviderTestUtils.setFeatureProvider(provider); | ||
TransactionContextPropagator transactionContextPropagator = new ThreadLocalTransactionContextPropagator(); | ||
api.setTransactionContextPropagator(transactionContextPropagator); | ||
Hook<Boolean> hook = spy(new Hook<Boolean>() { | ||
@Override | ||
public Optional<EvaluationContext> before(HookContext<Boolean> ctx, Map<String, Object> hints) { | ||
Map<String, Value> attrs = ctx.getCtx().asMap(); | ||
attrs.put("before", new Value("5")); | ||
attrs.put("common7", new Value("5")); | ||
return Optional.ofNullable(new ImmutableContext(attrs)); | ||
} | ||
@Override | ||
public void after(HookContext<Boolean> ctx, FlagEvaluationDetails<Boolean> details, Map<String, Object> hints) { | ||
Hook.super.after(ctx, details, hints); | ||
} | ||
}); | ||
|
||
Map<String, Value> apiAttributes = new HashMap<>(); | ||
apiAttributes.put("common1", new Value("1")); | ||
apiAttributes.put("common2", new Value("1")); | ||
apiAttributes.put("common3", new Value("1")); | ||
apiAttributes.put("common7", new Value("1")); | ||
apiAttributes.put("api", new Value("1")); | ||
EvaluationContext apiCtx = new ImmutableContext(apiAttributes); | ||
|
||
|
@@ -377,21 +391,55 @@ public void initialize(EvaluationContext evaluationContext) throws Exception { | |
invocationAttributes.put("invocation", new Value("4")); | ||
EvaluationContext invocationCtx = new ImmutableContext(invocationAttributes); | ||
|
||
c.getBooleanValue("key", false, invocationCtx); | ||
|
||
// assert the connect overrides | ||
c.getBooleanValue("key", false, invocationCtx, FlagEvaluationOptions.builder().hook(hook).build()); | ||
|
||
// assert the correct overrides in before hook | ||
verify(hook).before(argThat((arg) -> { | ||
EvaluationContext evaluationContext = arg.getCtx(); | ||
return evaluationContext.getValue("api").asString().equals("1") && | ||
evaluationContext.getValue("transaction").asString().equals("2") && | ||
evaluationContext.getValue("client").asString().equals("3") && | ||
evaluationContext.getValue("invocation").asString().equals("4") && | ||
evaluationContext.getValue("common1").asString().equals("2") && | ||
evaluationContext.getValue("common2").asString().equals("3") && | ||
evaluationContext.getValue("common3").asString().equals("4") && | ||
evaluationContext.getValue("common4").asString().equals("3") && | ||
evaluationContext.getValue("common5").asString().equals("4") && | ||
evaluationContext.getValue("common6").asString().equals("4"); | ||
}), any()); | ||
|
||
// assert the correct overrides in evaluation | ||
verify(provider).getBooleanEvaluation(any(), any(), argThat((arg) -> { | ||
return arg.getValue("api").asString().equals("1") && | ||
arg.getValue("transaction").asString().equals("2") && | ||
arg.getValue("client").asString().equals("3") && | ||
arg.getValue("invocation").asString().equals("4") && | ||
arg.getValue("before").asString().equals("5") && | ||
arg.getValue("common1").asString().equals("2") && | ||
arg.getValue("common2").asString().equals("3") && | ||
arg.getValue("common3").asString().equals("4") && | ||
arg.getValue("common4").asString().equals("3") && | ||
arg.getValue("common5").asString().equals("4") && | ||
arg.getValue("common6").asString().equals("4"); | ||
arg.getValue("common6").asString().equals("4") && | ||
arg.getValue("common7").asString().equals("5"); | ||
})); | ||
|
||
// assert the correct overrides in after hook | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This failed before the fix. |
||
verify(hook).after(argThat((arg) -> { | ||
EvaluationContext evaluationContext = arg.getCtx(); | ||
return evaluationContext.getValue("api").asString().equals("1") && | ||
evaluationContext.getValue("transaction").asString().equals("2") && | ||
evaluationContext.getValue("client").asString().equals("3") && | ||
evaluationContext.getValue("invocation").asString().equals("4") && | ||
evaluationContext.getValue("before").asString().equals("5") && | ||
evaluationContext.getValue("common1").asString().equals("2") && | ||
evaluationContext.getValue("common2").asString().equals("3") && | ||
evaluationContext.getValue("common3").asString().equals("4") && | ||
evaluationContext.getValue("common4").asString().equals("3") && | ||
evaluationContext.getValue("common5").asString().equals("4") && | ||
evaluationContext.getValue("common6").asString().equals("4") && | ||
evaluationContext.getValue("common7").asString().equals("5"); | ||
}), any(), any()); | ||
} | ||
|
||
@Specification(number="3.3.1.1", text="The API SHOULD have a method for setting a transaction context propagator.") | ||
|
This file contains 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 |
---|---|---|
|
@@ -484,10 +484,11 @@ public void finallyAfter(HookContext<Boolean> ctx, Map<String, Object> hints) { | |
@Specification(number = "4.1.4", text = "The evaluation context MUST be mutable only within the before hook.") | ||
@Specification(number = "4.3.4", text = "Any `evaluation context` returned from a `before` hook MUST be passed to subsequent `before` hooks (via `HookContext`).") | ||
@Test void beforeContextUpdated() { | ||
EvaluationContext ctx = new ImmutableContext(); | ||
Hook hook = mockBooleanHook(); | ||
String targetingKey = "test-key"; | ||
EvaluationContext ctx = new ImmutableContext(targetingKey); | ||
Hook<Boolean> hook = mockBooleanHook(); | ||
when(hook.before(any(), any())).thenReturn(Optional.of(ctx)); | ||
Hook hook2 = mockBooleanHook(); | ||
Hook<Boolean> hook2 = mockBooleanHook(); | ||
when(hook.before(any(), any())).thenReturn(Optional.empty()); | ||
InOrder order = inOrder(hook, hook2); | ||
|
||
|
@@ -499,11 +500,11 @@ public void finallyAfter(HookContext<Boolean> ctx, Map<String, Object> hints) { | |
.build()); | ||
|
||
order.verify(hook).before(any(), any()); | ||
ArgumentCaptor<HookContext> captor = ArgumentCaptor.forClass(HookContext.class); | ||
ArgumentCaptor<HookContext<Boolean>> captor = ArgumentCaptor.forClass(HookContext.class); | ||
order.verify(hook2).before(captor.capture(), any()); | ||
|
||
HookContext hc = captor.getValue(); | ||
assertEquals(hc.getCtx(), ctx); | ||
HookContext<Boolean> hc = captor.getValue(); | ||
assertEquals(hc.getCtx().getTargetingKey(), targetingKey); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The instance of the evaluation context now changes here, but that's not something we guarantee. |
||
|
||
} | ||
|
||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This failed before the fix.