Skip to content

Commit

Permalink
Removed contextCapture package private method that uses predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
chemicL committed Jun 6, 2023
1 parent 1e4c415 commit d8d9d11
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,36 +93,6 @@ static Context contextCaptureToEmpty() {
return contextCapture().apply(Context.empty());
}

/**
* Create a support function that takes a snapshot of thread locals and merges them with the
* provided {@link Context}, resulting in a new {@link Context} which includes entries
* captured from threadLocals by the Context-Propagation API.
* <p>
* The provided {@link Predicate} is used on keys associated to said thread locals
* by the Context-Propagation API to filter which entries should be captured in the
* first place.
* <p>
* This variant uses the implicit global {@code ContextRegistry} and captures only from
* available {@code ThreadLocalAccessors} that match the {@link Predicate}.
*
* @param captureKeyPredicate a {@link Predicate} used on keys to determine if each entry
* should be injected into the new {@link Context}
* @return a {@link Function} augmenting {@link Context} with captured entries
*/
static Function<Context, Context> contextCapture(Predicate<Object> captureKeyPredicate) {
if (!ContextPropagationSupport.isContextPropagationOnClasspath) {
return NO_OP;
}
// This method is actually used only in tests, so creating a new instance for
// each call is not an issue. If it's used in production code, a better
// strategy for configuring the Predicate needs to be chosen.
ContextSnapshotFactory factory = ContextSnapshotFactory
.builder()
.captureKeyPredicate(captureKeyPredicate)
.build();
return target -> factory.captureAll().updateContext(target);
}

/**
* When <a href="https://github.com/micrometer-metrics/context-propagation">context-propagation library</a>
* is available on the classpath, the provided {@link BiConsumer handler} will be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ void contextPropagationIsNotAvailable() {
assertThat(ContextPropagationSupport.isContextPropagationAvailable()).isFalse();
}

@Test
void contextCaptureIsNoOp() {
assertThat(ContextPropagation.contextCapture()).as("without predicate").isSameAs(ContextPropagation.NO_OP);
assertThat(ContextPropagation.contextCapture(v -> true)).as("with predicate").isSameAs(ContextPropagation.NO_OP);
}

@Test
void contextCaptureFluxApiIsNoOp() {
Flux<Integer> source = Flux.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,19 +334,6 @@ void contextCaptureWithNoPredicateReturnsTheConstantFunction() {
.isSameAs(ContextPropagation.WITH_GLOBAL_REGISTRY_NO_PREDICATE);
}

@Test
void contextCaptureWithPredicateReturnsNewFunctionWithGlobalRegistry() {
Function<Context, Context> test = ContextPropagation.contextCapture(ContextPropagation.PREDICATE_TRUE);

assertThat(test)
.as("predicate, no registry")
.isNotNull()
.isNotSameAs(ContextPropagation.WITH_GLOBAL_REGISTRY_NO_PREDICATE)
.isNotSameAs(ContextPropagation.NO_OP)
// as long as a predicate is supplied, the method creates new instances of the Function
.isNotSameAs(ContextPropagation.contextCapture(ContextPropagation.PREDICATE_TRUE));
}

@Test
void fluxApiUsesContextPropagationConstantFunction() {
Flux<Integer> source = Flux.empty();
Expand Down Expand Up @@ -411,22 +398,6 @@ void contextCaptureFunctionWithoutFiltering() {
.containsEntry(KEY2, "expected2")
.hasSize(2);
}

@Test
void captureWithFiltering() {
Function<Context, Context> test = ContextPropagation.contextCapture(k -> k.toString().equals(KEY2));

REF1.set("not_expected");
REF2.set("expected");

Context ctx = test.apply(Context.empty());
Map<Object, Object> asMap = new HashMap<>();
ctx.forEach(asMap::put); //easier to assert

assertThat(asMap)
.containsEntry(KEY2, "expected")
.hasSize(1);
}
}

@Nested
Expand Down

0 comments on commit d8d9d11

Please sign in to comment.