* Simplify functionalTracingEnabled variable logic

* Add `BeanFactoryAwareFunctionRegistryTests.testWrappedWithAroundAdviseNotMessageReturnConfiguration()`
to verify that non-Message return from the target function is wrapped to the `Message` before return
to the `FunctionAroundWrapper`
This commit is contained in:
Artem Bilan
2022-02-02 14:16:16 -05:00
committed by Oleg Zhurakousky
parent d4aa4f0e41
commit d37f603aa4
2 changed files with 38 additions and 3 deletions

View File

@@ -38,9 +38,8 @@ public abstract class FunctionAroundWrapper implements BiFunction<Object, Functi
@Override
public final Object apply(Object input, FunctionInvocationWrapper targetFunction) {
String functionalTracingEnabledStr = System.getProperty("spring.sleuth.function.enabled");
boolean functionalTracingEnabled =
!StringUtils.hasText(functionalTracingEnabledStr) || Boolean.parseBoolean(functionalTracingEnabledStr);
String functionalTracingEnabledStr = System.getProperty("spring.sleuth.function.enabled", "true");
boolean functionalTracingEnabled = Boolean.parseBoolean(functionalTracingEnabledStr);
if (functionalTracingEnabled) {
return this.doApply(input, targetFunction);
}

View File

@@ -566,6 +566,16 @@ public class BeanFactoryAwareFunctionRegistryTests {
assertThat(result.getHeaders().get("after")).isEqualTo("bar");
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testWrappedWithAroundAdviseNotMessageReturnConfiguration() {
FunctionCatalog catalog = this.configureCatalog(WrappedWithAroundAdviseNotMessageReturnConfiguration.class);
Function f = catalog.lookup("uppercase");
Message result = (Message) f.apply(MessageBuilder.withPayload("hello").setHeader("myHeader", "myValue").build());
assertThat(result.getHeaders()).containsEntry("myHeader", "myValue").containsEntry("advised", "true");
assertThat(result.getPayload()).isEqualTo("HELLO");
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testEachElementInFluxIsProcessed() {
@@ -917,6 +927,32 @@ public class BeanFactoryAwareFunctionRegistryTests {
}
}
@EnableAutoConfiguration
@Configuration
protected static class WrappedWithAroundAdviseNotMessageReturnConfiguration {
@Bean
public Function<Message<String>, String> uppercase() {
return v -> v.getPayload().toUpperCase();
}
@Bean
public FunctionAroundWrapper wrapper() {
return new FunctionAroundWrapper() {
@Override
protected Object doApply(Object input, FunctionInvocationWrapper targetFunction) {
// in this test we know input is a Message
Message<?> mInput = (Message<?>) input;
Message<?> advisedMessage = MessageBuilder.fromMessage(mInput).setHeader("advised", "true").build();
Object result = targetFunction.apply(advisedMessage);
assertThat(result).isInstanceOf(Message.class);
return result;
}
};
}
}
@EnableAutoConfiguration
@Configuration
protected static class SampleFunctionConfiguration {