Add Kotlin suspend function support

Use suspendCoroutineUninterceptedOrReturn to avoid using not fully implemented Function2.reflect()

Mapping of Function, Consumer and Supplier to kotlin suspend flow lambda

Fix MR review

Resolves #655
This commit is contained in:
Adrien Poupard
2021-02-20 19:21:48 +01:00
committed by Oleg Zhurakousky
parent 3c16efc6a4
commit e30a091f82
10 changed files with 649 additions and 9 deletions

View File

@@ -0,0 +1,126 @@
/*
* Copyright 2019-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.kotlin;
import java.lang.reflect.ParameterizedType;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import kotlin.jvm.functions.Function2;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.cloud.function.context.catalog.FunctionTypeUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.GenericApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* @author Adrien Poupard
*/
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class ContextFunctionCatalogAutoConfigurationKotlinSuspendTests {
private GenericApplicationContext context;
private FunctionCatalog catalog;
@AfterEach
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void typeDiscoveryTests() {
create(new Class[] { KotlinSuspendFlowLambdasConfiguration.class,
ContextFunctionCatalogAutoConfigurationKotlinTests.SimpleConfiguration.class });
Object function = this.context.getBean("kotlinFunction");
ParameterizedType functionType = (ParameterizedType) FunctionTypeUtils.discoverFunctionType(function, "kotlinFunction", this.context);
assertThat(functionType.getRawType().getTypeName()).isEqualTo(Function.class.getName());
assertThat(functionType.getActualTypeArguments().length).isEqualTo(2);
assertThat(functionType.getActualTypeArguments()[0].getTypeName()).isEqualTo("reactor.core.publisher.Flux<java.lang.String>");
assertThat(functionType.getActualTypeArguments()[1].getTypeName()).isEqualTo("reactor.core.publisher.Flux<java.lang.String>");
function = this.context.getBean("kotlinConsumer");
functionType = (ParameterizedType) FunctionTypeUtils.discoverFunctionType(function, "kotlinConsumer", this.context);
assertThat(functionType.getRawType().getTypeName()).isEqualTo(Consumer.class.getName());
assertThat(functionType.getActualTypeArguments().length).isEqualTo(1);
assertThat(functionType.getActualTypeArguments()[0].getTypeName()).isEqualTo("reactor.core.publisher.Flux<java.lang.String>");
function = this.context.getBean("kotlinSupplier");
functionType = (ParameterizedType) FunctionTypeUtils.discoverFunctionType(function, "kotlinSupplier", this.context);
assertThat(functionType.getRawType().getTypeName()).isEqualTo(Supplier.class.getName());
assertThat(functionType.getActualTypeArguments().length).isEqualTo(1);
assertThat(functionType.getActualTypeArguments()[0].getTypeName()).isEqualTo("reactor.core.publisher.Flux<java.lang.String>");
function = this.context.getBean("kotlinPojoFunction");
functionType = (ParameterizedType) FunctionTypeUtils.discoverFunctionType(function, "kotlinPojoFunction", this.context);
assertThat(functionType.getRawType().getTypeName()).isEqualTo(Function.class.getName());
assertThat(functionType.getActualTypeArguments().length).isEqualTo(2);
assertThat(functionType.getActualTypeArguments()[0].getTypeName()).isEqualTo("reactor.core.publisher.Flux<org.springframework.cloud.function.kotlin.Person>");
assertThat(functionType.getActualTypeArguments()[1].getTypeName()).isEqualTo("reactor.core.publisher.Flux<java.lang.String>");
}
@Test
public void shouldNotLoadKotlinSuspendLambasNotUsingFlow() {
create(new Class[] { KotlinSuspendLambdasConfiguration.class,
ContextFunctionCatalogAutoConfigurationKotlinTests.SimpleConfiguration.class });
assertThat(this.context.getBean("kotlinFunction")).isInstanceOf(Function2.class);
assertThatThrownBy(() -> {
this.catalog.lookup(Function.class, "kotlinFunction");
}).isInstanceOf(BeanCreationException.class);
assertThatThrownBy(() -> {
this.catalog.lookup(Function.class, "kotlinConsumer");
}).isInstanceOf(BeanCreationException.class);
assertThatThrownBy(() -> {
this.catalog.lookup(Supplier.class, "kotlinSupplier");
}).isInstanceOf(BeanCreationException.class);
}
private void create(Class<?>[] types, String... props) {
this.context = (GenericApplicationContext) new SpringApplicationBuilder(types).properties(props).run();
this.catalog = this.context.getBean(FunctionCatalog.class);
}
@EnableAutoConfiguration
@Configuration
protected static class SimpleConfiguration {
@Bean
public Function<String, String> function2() {
return value -> value + "function2";
}
}
}

View File

@@ -32,7 +32,7 @@ class KotlinLambdasConfiguration {
fun kotlinFunction(): (String) -> String {
return { it.toUpperCase() }
}
@Bean
fun kotlinPojoFunction(): (Person) -> String {
return { it.name.toString()}
@@ -52,4 +52,5 @@ class KotlinLambdasConfiguration {
fun javaFunction(): Function<String, String> {
return Function { x -> x }
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.kotlin
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import reactor.core.publisher.Flux
import java.util.function.Function
/**
* @author Adrien Poupard
*
*/
@EnableAutoConfiguration
@Configuration
class KotlinSuspendFlowLambdasConfiguration {
@Bean
fun kotlinFunction(): suspend (Flow<String>) -> Flow<String> = { flow ->
flow.map { value -> value.toUpperCase() }
}
@Bean
fun kotlinPojoFunction(): suspend (Flow<Person>) -> Flow<String> = { flow ->
flow.map(Person::toString)
}
@Bean
fun kotlinConsumer(): suspend (Flow<String>) -> Unit = { flow ->
flow.collect(::println)
}
@Bean
fun kotlinSupplier(): suspend () -> Flow<String> = {
flow {
emit("Hello")
}
}
@Bean
fun javaFunction(): Function<Flux<String>, Flux<String>> {
return Function { x -> x }
}
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.kotlin
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
/**
* @author Adrien Poupard
*
*/
@EnableAutoConfiguration
@Configuration
class KotlinSuspendLambdasConfiguration {
@Bean
fun kotlinFunction(): suspend (Person) -> String {
return { it.name.toString()}
}
@Bean
fun kotlinConsumer(): suspend (String) -> Unit {
return { println(it) }
}
@Bean
fun kotlinSupplier(): suspend () -> String {
return { "Hello" }
}
}

View File

@@ -0,0 +1,108 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.kotlin.web
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment
import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.cloud.function.web.RestApplication
import org.springframework.context.annotation.Bean
import org.springframework.http.MediaType
import org.springframework.http.RequestEntity
import org.springframework.messaging.Message
import org.springframework.messaging.support.MessageBuilder
import org.springframework.test.context.ContextConfiguration
import java.net.URI
/**
* @author Adrien Poupard
*/
@SpringBootTest(
webEnvironment = WebEnvironment.RANDOM_PORT,
properties = ["spring.cloud.function.web.path=/functions", "spring.main.web-application-type=reactive"]
)
@ContextConfiguration(classes = [RestApplication::class, HeadersToMessageSuspendTests.TestConfiguration::class])
class HeadersToMessageSuspendTests {
@Autowired
private val rest: TestRestTemplate? = null
@Test
@Throws(Exception::class)
fun testBodyAndCustomHeaderFromMessagePropagation() {
// test POJO paylod
var postForEntity = rest!!
.exchange(
RequestEntity.post(URI("/functions/employeeSuspend"))
.contentType(MediaType.APPLICATION_JSON)
.body("[{\"name\":\"Bob\",\"age\":25}]"), String::class.java
)
Assertions.assertThat(postForEntity.body).isEqualTo("[{\"name\":\"Bob\",\"age\":25}]")
Assertions.assertThat(postForEntity.headers.containsKey("x-content-type")).isTrue
Assertions.assertThat(postForEntity.headers["x-content-type"]!![0])
.isEqualTo("application/xml")
Assertions.assertThat(postForEntity.headers["foo"]!![0]).isEqualTo("bar")
// test simple type payload
postForEntity = rest.postForEntity(
URI("/functions/stringSuspend"),
"HELLO", String::class.java
)
Assertions.assertThat(postForEntity.body).isEqualTo("[\"HELLO\"]")
Assertions.assertThat(postForEntity.headers.containsKey("x-content-type")).isTrue
Assertions.assertThat(postForEntity.headers["x-content-type"]!![0])
.isEqualTo("application/xml")
Assertions.assertThat(postForEntity.headers["foo"]!![0]).isEqualTo("bar")
}
@EnableAutoConfiguration
@org.springframework.boot.test.context.TestConfiguration
class TestConfiguration {
@Bean("stringSuspend")
fun functiono():suspend (employee: Flow<Message<String>>) -> Flow<Message<String>> = { flow: Flow<Message<String>> ->
flow.map { request ->
val message =
MessageBuilder.withPayload(request.payload)
.setHeader("X-Content-Type", "application/xml")
.setHeader("foo", "bar").build()
message
}
}
@Bean("employeeSuspend")
fun function1(): suspend (employee: Flow<Message<Employee>>) -> Flow<Message<Employee>> = { flow ->
flow.map { request ->
val message =
MessageBuilder
.withPayload(request.payload)
.setHeader("X-Content-Type", "application/xml")
.setHeader("foo", "bar")
.build()
message
}
}
}
class Employee {
var name: String? = null
var age = 0
}
}

View File

@@ -0,0 +1,103 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.kotlin.web
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment
import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.cloud.function.web.RestApplication
import org.springframework.context.annotation.Bean
import org.springframework.http.MediaType
import org.springframework.http.RequestEntity
import org.springframework.messaging.Message
import org.springframework.messaging.support.MessageBuilder
import org.springframework.test.context.ContextConfiguration
import java.lang.Exception
import java.net.URI
/**
* @author Dave Syer
* @author Oleg Zhurakousky
*/
@SpringBootTest(
webEnvironment = WebEnvironment.RANDOM_PORT,
properties = ["spring.cloud.function.web.path=/functions", "spring.main.web-application-type=reactive"]
)
@ContextConfiguration(classes = [RestApplication::class, HeadersToMessageTests.TestConfiguration::class])
class HeadersToMessageTests {
@Autowired
private val rest: TestRestTemplate? = null
@Test
@Throws(Exception::class)
fun testBodyAndCustomHeaderFromMessagePropagation() {
// test POJO paylod
var postForEntity = rest!!
.exchange(
RequestEntity.post(URI("/functions/employee"))
.contentType(MediaType.APPLICATION_JSON)
.body("{\"name\":\"Bob\",\"age\":25}"), String::class.java
)
Assertions.assertThat(postForEntity.body).isEqualTo("{\"name\":\"Bob\",\"age\":25}")
Assertions.assertThat(postForEntity.headers.containsKey("x-content-type")).isTrue
Assertions.assertThat(postForEntity.headers["x-content-type"]!![0])
.isEqualTo("application/xml")
Assertions.assertThat(postForEntity.headers["foo"]!![0]).isEqualTo("bar")
// test simple type payload
postForEntity = rest.postForEntity(
URI("/functions/string"),
"{\"name\":\"Bob\",\"age\":25}", String::class.java
)
Assertions.assertThat(postForEntity.body).isEqualTo("{\"name\":\"Bob\",\"age\":25}")
Assertions.assertThat(postForEntity.headers.containsKey("x-content-type")).isTrue
Assertions.assertThat(postForEntity.headers["x-content-type"]!![0])
.isEqualTo("application/xml")
Assertions.assertThat(postForEntity.headers["foo"]!![0]).isEqualTo("bar")
}
@EnableAutoConfiguration
@org.springframework.boot.test.context.TestConfiguration
class TestConfiguration {
@Bean("string")
fun functiono(): (message: Message<String?>) -> Message<String> = { request: Message<String?> ->
val message =
MessageBuilder.withPayload(request.payload)
.setHeader("X-Content-Type", "application/xml")
.setHeader("foo", "bar").build()
message
}
@Bean("employee")
fun function1(): (employee: Message<Employee>) -> Message<Employee> = { request ->
val message =
MessageBuilder
.withPayload(request.payload)
.setHeader("X-Content-Type", "application/xml")
.setHeader("foo", "bar").build()
message
}
}
// used by json converter
class Employee {
var name: String? = null
var age = 0
}
}