Skip to content

Commit

Permalink
Issue #1316: Kotlin - Remove unnecessary suspend modifier from asFlow
Browse files Browse the repository at this point in the history
  • Loading branch information
heubeck committed Jul 25, 2023
1 parent 56848d6 commit 926bb28
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import kotlinx.coroutines.launch
* @property bufferCapacity the type (defined by [Channel] constants) or the capacity (>0 <[Int.MAX_VALUE]) of the channel, defaulting to [Channel.UNLIMITED]
* @property bufferOverflowStrategy action strategy on exceeding the [bufferCapacity], see [BufferOverflow].
*/
suspend fun <T> Multi<T>.asFlow(
fun <T> Multi<T>.asFlow(
bufferCapacity: Int = Channel.UNLIMITED,
bufferOverflowStrategy: BufferOverflow = BufferOverflow.SUSPEND
): Flow<T> = callbackFlow<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ class MultiAsFlowTest {

@Test
fun `test immediate failure`() {
runBlocking {
// Given
val multi = Multi.createFrom().failure<Any>(Exception("kaboom"))
// Given
val multi = Multi.createFrom().failure<Any>(Exception("kaboom"))

// When
val flow = multi.asFlow()

// When & Then
val flow = multi.asFlow()
// Then
runBlocking {
assertFailsWith<Exception>("kaboom") {
flow.toList()
}
Expand Down Expand Up @@ -94,15 +96,15 @@ class MultiAsFlowTest {
}

// When & Then
runBlocking {
val flow = multi.asFlow()
val durationMillis = measureTimeMillis {
assertFailsWith<Exception>("boom") {
val flow = multi.asFlow()
val durationMillis = measureTimeMillis {
assertFailsWith<Exception>("boom") {
runBlocking {
flow.toList()
}
}
assertThat(durationMillis).isGreaterThanOrEqualTo(delayMillis)
}
assertThat(durationMillis).isGreaterThanOrEqualTo(delayMillis)
}

@Test
Expand Down Expand Up @@ -130,11 +132,11 @@ class MultiAsFlowTest {
emitter.set(em)
}

runBlocking {
val flow = multi.asFlow()
val eventItems = mutableListOf<Int>()
val flow = multi.asFlow()
val eventItems = mutableListOf<Int>()

// When
// When
runBlocking {
val assertJob = async {
assertFails {
flow.collect { eventItems.add(it) }
Expand Down Expand Up @@ -228,46 +230,46 @@ class MultiAsFlowTest {

@Test
fun `test empty Multi`() {
testBlocking {
// Given
val multi = Multi.createFrom().empty<Any>()
// Given
val multi = Multi.createFrom().empty<Any>()

// When
val items = multi.asFlow().toList()
// When
val items = multi.asFlow()

// Then
assertThat(items).isEmpty()
// Then
testBlocking {
assertThat(items.toList()).isEmpty()
}
}

@Test
fun `test null item`() {
testBlocking {
// Given
val multi = Multi.createFrom().item { null }
// Given
val multi = Multi.createFrom().item { null }

// When
val items = multi.asFlow().toList()
// When
val items = multi.asFlow()

/// Then
assertThat(items).hasSize(0)
/// Then
testBlocking {
assertThat(items.toList()).hasSize(0)
}
}

@Test
fun `test buffered flow`() {
testBlocking {
// Given
val items = (1..10_000).toList()
val multi = Multi.createFrom().iterable(items)

// When
val flow = multi.asFlow(bufferCapacity = 5)
val eventItems = flow.toList()
// Given
val items = (1..10_000).toList()
val multi = Multi.createFrom().iterable(items)

// Then
assertThat(eventItems).containsExactlyElementsOf((1..10_000).toList())
// When
val flow = multi.asFlow(bufferCapacity = 5)
val eventItems = testBlocking {
flow.toList()
}

// Then
assertThat(eventItems).containsExactlyElementsOf((1..10_000).toList())
}

@Test
Expand Down

0 comments on commit 926bb28

Please sign in to comment.