Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ String normalizeFunctionDefinition(String functionDefinition) {
? functionDefinition.replaceAll(",", "|")
: System.getProperty(FunctionProperties.FUNCTION_DEFINITION, "");

Set<String> names = this.getNames(null);
if (this.isSingleFunctionFallbackEnabled() && !names.contains(functionDefinition)) {
if (this.isSingleFunctionFallbackEnabled() && !this.isFunctionDefinitionRegistered(functionDefinition)) {
Set<String> names = this.getNames(null);
List<String> eligibleFunction = names.stream()
.filter(name -> !RoutingFunction.FUNCTION_NAME.equals(name))
.filter(name -> !RoutingFunction.DEFAULT_ROUTE_HANDLER.equals(name))
Expand All @@ -289,6 +289,28 @@ String normalizeFunctionDefinition(String functionDefinition) {
return functionDefinition;
}

private boolean isFunctionDefinitionRegistered(String functionDefinition) {
if (!StringUtils.hasText(functionDefinition)) {
return false;
}
synchronized (cacheLock) {
if (this.wrappedFunctionDefinitions.containsKey(functionDefinition)) {
return true;
}
}
String[] functionNames = StringUtils.delimitedListToStringArray(functionDefinition, "|");
if (functionNames.length == 0) {
return false;
}
for (String functionName : functionNames) {
if (!StringUtils.hasText(functionName)
|| this.functionRegistrations.stream().noneMatch(registration -> registration.getNames().contains(functionName))) {
return false;
}
}
return true;
}

private boolean isSingleFunctionFallbackEnabled() {
return this.functionProperties == null || this.functionProperties.isSingleFunctionFallbackEnabled();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand All @@ -60,6 +61,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.cloud.function.context.FunctionProperties;
import org.springframework.cloud.function.context.FunctionRegistration;
import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper;
import org.springframework.cloud.function.context.config.RoutingFunction;
Expand Down Expand Up @@ -118,6 +120,26 @@ public void before() {
System.clearProperty("spring.cloud.function.definition");
}

@Test
public void registeredFunctionLookupDoesNotRescanBeanFactoryNames() {
FunctionCatalog functionCatalog = this.configureCatalog(SampleFunctionConfiguration.class);
BeanFactoryAwareFunctionRegistry registry = Mockito.spy((BeanFactoryAwareFunctionRegistry) functionCatalog);
try {
RoutingFunction routingFunction = new RoutingFunction(registry, new FunctionProperties());
Message<String> message = MessageBuilder.withPayload("hello")
.setHeader(FunctionProperties.FUNCTION_DEFINITION, "uppercase")
.build();
assertThat(routingFunction.apply(message)).isEqualTo("HELLO");
Mockito.clearInvocations(registry);

assertThat(routingFunction.apply(message)).isEqualTo("HELLO");
Mockito.verify(registry, Mockito.never()).getNames(null);
}
finally {
((ConfigurableApplicationContext) this.context).close();
}
}

//@Test
public void testBoundedFunctionCache() throws Exception {
FunctionCatalog catalog = this.configureCatalogWithProperties(
Expand Down