Skip to content

Add RowsFetchSpec<T>.allAsFlow() extension #91

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

Closed
wants to merge 1 commit into from
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 @@ -15,8 +15,11 @@
*/
package org.springframework.data.r2dbc.function

import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.reactive.awaitFirstOrNull
import kotlinx.coroutines.reactive.awaitSingle
import kotlinx.coroutines.reactive.flow.asFlow

/**
* Non-nullable Coroutines variant of [RowsFetchSpec.one].
Expand Down Expand Up @@ -50,5 +53,13 @@ suspend fun <T> RowsFetchSpec<T>.awaitFirst(): T =
suspend fun <T> RowsFetchSpec<T>.awaitFirstOrNull(): T? =
first().awaitFirstOrNull()

// TODO Coroutines variant of [RowsFetchSpec.all], depends on [kotlinx.coroutines#254](https://github.com/Kotlin/kotlinx.coroutines/issues/254).
// suspend fun <T> RowsFetchSpec<T>.awaitAll() = all()...
/**
* Coroutines [Flow] variant of [RowsFetchSpec.all].
*
* Backpressure is controlled by [batchSize] parameter that controls the size of in-flight elements
* and [org.reactivestreams.Subscription.request] size.
*
* @author Sebastien Deleuze
*/
@FlowPreview
fun <T: Any> RowsFetchSpec<T>.allAsFlow(batchSize: Int = 1): Flow<T> = all().asFlow(batchSize)
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ package org.springframework.data.r2dbc.function
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.runBlocking
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.junit.Test
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono

/**
Expand Down Expand Up @@ -150,4 +153,20 @@ class RowsFetchSpecExtensionsTests {
spec.first()
}
}

@Test
@FlowPreview
fun allAsFlow() {

val spec = mockk<RowsFetchSpec<String>>()
every { spec.all() } returns Flux.just("foo", "bar", "baz")

runBlocking {
assertThat(spec.allAsFlow().toList()).contains("foo", "bar", "baz")
}

verify {
spec.all()
}
}
}