-
Notifications
You must be signed in to change notification settings - Fork 38.7k
Add RSocketServiceMethod
support for Kotlin suspending functions
#35473
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* Copyright 2002-present 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.messaging.rsocket.service | ||
|
||
import io.rsocket.util.DefaultPayload | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flowOf | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.toList | ||
import kotlinx.coroutines.reactive.asFlow | ||
import kotlinx.coroutines.runBlocking | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.messaging.rsocket.RSocketRequester | ||
import org.springframework.messaging.rsocket.RSocketStrategies | ||
import org.springframework.messaging.rsocket.TestRSocket | ||
import org.springframework.util.MimeTypeUtils.TEXT_PLAIN | ||
import reactor.core.publisher.Flux | ||
import reactor.core.publisher.Mono | ||
|
||
/** | ||
* Kotlin tests for [RSocketServiceMethod]. | ||
* | ||
* @author Dmitry Sulman | ||
*/ | ||
class RSocketServiceMethodKotlinTests { | ||
|
||
private lateinit var rsocket: TestRSocket | ||
|
||
private lateinit var proxyFactory: RSocketServiceProxyFactory | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
rsocket = TestRSocket() | ||
val requester = RSocketRequester.wrap(rsocket, TEXT_PLAIN, TEXT_PLAIN, RSocketStrategies.create()) | ||
proxyFactory = RSocketServiceProxyFactory.builder(requester).build() | ||
} | ||
|
||
@Test | ||
fun fireAndForget(): Unit = runBlocking { | ||
val service = proxyFactory.createClient(SuspendingFunctionsService::class.java) | ||
|
||
val requestPayload = "request" | ||
service.fireAndForget(requestPayload) | ||
|
||
assertThat(rsocket.savedMethodName).isEqualTo("fireAndForget") | ||
assertThat(rsocket.savedPayload?.metadataUtf8).isEqualTo("ff") | ||
assertThat(rsocket.savedPayload?.dataUtf8).isEqualTo(requestPayload) | ||
} | ||
|
||
@Test | ||
fun requestResponse(): Unit = runBlocking { | ||
val service = proxyFactory.createClient(SuspendingFunctionsService::class.java) | ||
|
||
val requestPayload = "request" | ||
val responsePayload = "response" | ||
rsocket.setPayloadMonoToReturn(Mono.just(DefaultPayload.create(responsePayload))) | ||
val response = service.requestResponse(requestPayload) | ||
|
||
assertThat(response).isEqualTo(responsePayload) | ||
assertThat(rsocket.savedMethodName).isEqualTo("requestResponse") | ||
assertThat(rsocket.savedPayload?.metadataUtf8).isEqualTo("rr") | ||
assertThat(rsocket.savedPayload?.dataUtf8).isEqualTo(requestPayload) | ||
} | ||
|
||
@Test | ||
fun requestStream(): Unit = runBlocking { | ||
val service = proxyFactory.createClient(SuspendingFunctionsService::class.java) | ||
|
||
val requestPayload = "request" | ||
val responsePayload1 = "response1" | ||
val responsePayload2 = "response2" | ||
rsocket.setPayloadFluxToReturn( | ||
Flux.just(DefaultPayload.create(responsePayload1), DefaultPayload.create(responsePayload2))) | ||
val response = service.requestStream(requestPayload).toList() | ||
|
||
assertThat(response).containsExactly(responsePayload1, responsePayload2) | ||
assertThat(rsocket.savedMethodName).isEqualTo("requestStream") | ||
assertThat(rsocket.savedPayload?.metadataUtf8).isEqualTo("rs") | ||
assertThat(rsocket.savedPayload?.dataUtf8).isEqualTo(requestPayload) | ||
} | ||
|
||
@Test | ||
fun requestChannel(): Unit = runBlocking { | ||
val service = proxyFactory.createClient(SuspendingFunctionsService::class.java) | ||
|
||
val requestPayload1 = "request1" | ||
val requestPayload2 = "request2" | ||
val responsePayload1 = "response1" | ||
val responsePayload2 = "response2" | ||
rsocket.setPayloadFluxToReturn( | ||
Flux.just(DefaultPayload.create(responsePayload1), DefaultPayload.create(responsePayload2))) | ||
val response = service.requestChannel(flowOf(requestPayload1, requestPayload2)).toList() | ||
|
||
assertThat(response).containsExactly(responsePayload1, responsePayload2) | ||
assertThat(rsocket.savedMethodName).isEqualTo("requestChannel") | ||
|
||
val savedPayloads = rsocket.savedPayloadFlux | ||
?.asFlow() | ||
?.map { it.dataUtf8 } | ||
?.toList() | ||
assertThat(savedPayloads).containsExactly(requestPayload1, requestPayload2) | ||
} | ||
|
||
private interface SuspendingFunctionsService { | ||
|
||
@RSocketExchange("ff") | ||
suspend fun fireAndForget(input: String) | ||
|
||
@RSocketExchange("rr") | ||
suspend fun requestResponse(input: String): String | ||
|
||
@RSocketExchange("rs") | ||
suspend fun requestStream(input: String): Flow<String> | ||
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. @dmitrysulman By the way, here it is tricky part whether to let suspend functions for "Flow returning" methods (requestStream / requestChannel) because the Flow itself does not require to be in suspend function, all asynchronous work happens in flow {} / Flow.collect {} callback. And despite I personally prefer to keep such functions also suspend, the big open source thinks other way
@sdeleuze please correct me if I'm am wrong 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 can be easily fixed after (if) the current PR is merged (the fix of the |
||
|
||
@RSocketExchange("rc") | ||
suspend fun requestChannel(input: Flow<String>): Flow<String> | ||
} | ||
} |
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.
returnParam.getParameterType().getName()
can be replaced withreturnType.getName()
.@sdeleuze I can update the branch or it can be fixed in the polishing commit.