Add support for Coroutines Flow
Flow is a Kotlin Coroutines related cold asynchronous stream of the data, that emits from zero to N (where N can be unbounded) values and completes normally or with an exception. It is conceptually the Coroutines equivalent of Flux with an extension oriented API design, easy custom operator capabilities and some suspending methods. This commit leverages Flow <-> Flux interoperability to support Flow on controller handler method parameters or return values, and also adds Flow based extensions to WebFlux.fn. It allows to reach a point when we can consider Spring Framework officially supports Coroutines even if some additional work remains to be done like adding interoperability between Reactor and Coroutines contexts. Flow is currently an experimental API that is expected to become final before Spring Framework 5.2 GA. Close gh-19975
This commit is contained in:
@@ -19,6 +19,7 @@ package org.springframework.web.reactive.function.client
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import kotlinx.coroutines.FlowPreview
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
@@ -49,6 +50,13 @@ class ClientResponseExtensionsTests {
|
||||
verify { response.bodyToFlux(object : ParameterizedTypeReference<List<Foo>>() {}) }
|
||||
}
|
||||
|
||||
@Test
|
||||
@FlowPreview
|
||||
fun `bodyToFlow with reified type parameters`() {
|
||||
response.bodyToFlow<List<Foo>>()
|
||||
verify { response.bodyToFlux(object : ParameterizedTypeReference<List<Foo>>() {}) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toEntity with reified type parameters`() {
|
||||
response.toEntity<List<Foo>>()
|
||||
|
||||
@@ -19,6 +19,8 @@ package org.springframework.web.reactive.function.client
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import kotlinx.coroutines.FlowPreview
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
@@ -45,6 +47,14 @@ class WebClientExtensionsTests {
|
||||
verify { requestBodySpec.body(body, object : ParameterizedTypeReference<List<Foo>>() {}) }
|
||||
}
|
||||
|
||||
@Test
|
||||
@FlowPreview
|
||||
fun `RequestBodySpec#body with Flow and reified type parameters`() {
|
||||
val body = mockk<Flow<List<Foo>>>()
|
||||
requestBodySpec.body(body)
|
||||
verify { requestBodySpec.body(ofType<Publisher<List<Foo>>>(), object : ParameterizedTypeReference<List<Foo>>() {}) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ResponseSpec#bodyToMono with reified type parameters`() {
|
||||
responseSpec.bodyToMono<List<Foo>>()
|
||||
@@ -57,6 +67,13 @@ class WebClientExtensionsTests {
|
||||
verify { responseSpec.bodyToFlux(object : ParameterizedTypeReference<List<Foo>>() {}) }
|
||||
}
|
||||
|
||||
@Test
|
||||
@FlowPreview
|
||||
fun `bodyToFlow with reified type parameters`() {
|
||||
responseSpec.bodyToFlow<List<Foo>>()
|
||||
verify { responseSpec.bodyToFlux(object : ParameterizedTypeReference<List<Foo>>() {}) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun awaitExchange() {
|
||||
val response = mockk<ClientResponse>()
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.web.reactive.function.client
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import kotlinx.coroutines.FlowPreview
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
@@ -52,6 +53,13 @@ class ServerRequestExtensionsTests {
|
||||
verify { request.bodyToFlux(object : ParameterizedTypeReference<List<Foo>>() {}) }
|
||||
}
|
||||
|
||||
@Test
|
||||
@FlowPreview
|
||||
fun `bodyToFlow with reified type parameters`() {
|
||||
request.bodyToFlow<List<Foo>>()
|
||||
verify { request.bodyToFlux(object : ParameterizedTypeReference<List<Foo>>() {}) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun awaitBody() {
|
||||
every { request.bodyToMono<String>() } returns Mono.just("foo")
|
||||
|
||||
@@ -19,12 +19,16 @@ package org.springframework.web.reactive.function.server
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import kotlinx.coroutines.FlowPreview
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import org.reactivestreams.Publisher
|
||||
import org.springframework.core.ParameterizedTypeReference
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.http.MediaType.*
|
||||
import reactor.core.publisher.Flux
|
||||
import reactor.core.publisher.Mono
|
||||
|
||||
/**
|
||||
@@ -48,7 +52,7 @@ class ServerResponseExtensionsTests {
|
||||
fun `BodyBuilder#bodyToServerSentEvents with Publisher and reified type parameters`() {
|
||||
val body = mockk<Publisher<List<Foo>>>()
|
||||
bodyBuilder.bodyToServerSentEvents(body)
|
||||
verify { bodyBuilder.contentType(TEXT_EVENT_STREAM) }
|
||||
verify { bodyBuilder.contentType(TEXT_EVENT_STREAM).body(ofType<Publisher<List<Foo>>>(), object : ParameterizedTypeReference<List<Foo>>() {}) }
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -92,6 +96,32 @@ class ServerResponseExtensionsTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@FlowPreview
|
||||
fun `BodyBuilder#body with Flow and reified type parameters`() {
|
||||
val response = mockk<ServerResponse>()
|
||||
val body = mockk<Flow<List<Foo>>>()
|
||||
every { bodyBuilder.body(ofType<Publisher<List<Foo>>>()) } returns Mono.just(response)
|
||||
runBlocking {
|
||||
bodyBuilder.bodyAndAwait(body)
|
||||
}
|
||||
verify { bodyBuilder.body(ofType<Publisher<List<Foo>>>(), object : ParameterizedTypeReference<List<Foo>>() {}) }
|
||||
}
|
||||
|
||||
@Test
|
||||
@FlowPreview
|
||||
fun `BodyBuilder#bodyToServerSentEvents with Flow and reified type parameters`() {
|
||||
val response = mockk<ServerResponse>()
|
||||
val body = mockk<Flow<List<Foo>>>()
|
||||
every { bodyBuilder.contentType(ofType()) } returns bodyBuilder
|
||||
every { bodyBuilder.body(ofType<Publisher<List<Foo>>>()) } returns Mono.just(response)
|
||||
runBlocking {
|
||||
bodyBuilder.bodyToServerSentEventsAndAwait(body)
|
||||
}
|
||||
verify { bodyBuilder.body(ofType<Publisher<List<Foo>>>(), object : ParameterizedTypeReference<List<Foo>>() {}) }
|
||||
verify { bodyBuilder.contentType(TEXT_EVENT_STREAM) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `renderAndAwait with a vararg parameter`() {
|
||||
val response = mockk<ServerResponse>()
|
||||
|
||||
Reference in New Issue
Block a user