-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce missing backpressure drop Flow operator
- Loading branch information
1 parent
2ff0bbb
commit e0eab61
Showing
5 changed files
with
269 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
kotlinx-coroutines-core/common/src/flow/internal/DropWhileBusyCollector.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package kotlinx.coroutines.flow.internal | ||
|
||
import kotlinx.coroutines.InternalCoroutinesApi | ||
import kotlinx.coroutines.channels.SendChannel | ||
import kotlinx.coroutines.flow.FlowCollector | ||
|
||
/** | ||
* A [FlowCollector] specifically built to be used for `Flow.dropWhileBusy` | ||
* | ||
* This collector ensures that the fist element is always sent. It then offers subsequent elements, | ||
* allowing them to be dropped. | ||
*/ | ||
@InternalCoroutinesApi | ||
internal class DropWhileBusyCollector<T>(private val channel: SendChannel<T>) : FlowCollector<T> { | ||
|
||
private var hasSentFirstValue: Boolean = false | ||
|
||
override suspend fun emit(value: T) { | ||
if (hasSentFirstValue) { | ||
channel.offer(value) | ||
} else { | ||
channel.send(value) | ||
hasSentFirstValue = true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
kotlinx-coroutines-core/common/test/flow/operators/DropWhileBusyTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package kotlinx.coroutines.flow.operators | ||
|
||
import kotlinx.coroutines.TestBase | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.* | ||
import kotlinx.coroutines.withVirtualTime | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class DropWhileBusyTest : TestBase() { | ||
|
||
@Test | ||
fun `elements are dropped during slow consumption`() = withVirtualTime { | ||
expect(1) | ||
|
||
val flow = (1..30).asFlow().onEach { delay(100) } | ||
|
||
val result = flow.dropWhileBusy().onEach { delay(1000) }.toList() | ||
|
||
assertEquals(listOf(1, 11, 21), result) | ||
|
||
finish(2) | ||
} | ||
|
||
@Test | ||
fun `buffering a flow upstream of dropWhileBusy fuses to dropWhileBusy`() = withVirtualTime { | ||
expect(1) | ||
|
||
val flow = (1..30).asFlow().onEach { delay(100) }.buffer(4) | ||
|
||
val result = flow.dropWhileBusy().onEach { delay(1000) }.toList() | ||
|
||
assertEquals(listOf(1, 11, 21), result) | ||
|
||
finish(2) | ||
} | ||
|
||
@Test | ||
fun `buffering a flow downstream of dropWhileBusy buffers elements`() = withVirtualTime { | ||
expect(1) | ||
|
||
val flow = (1..30).asFlow().onEach { delay(100) } | ||
val result = flow.dropWhileBusy().buffer(capacity = 1).onEach { delay(1000) }.toList() | ||
|
||
assertEquals(listOf(1, 2, 11, 21), result) | ||
|
||
finish(2) | ||
} | ||
|
||
@Test | ||
fun `conflating a flow downstream of dropWhileBusy fuses to conflate`() = withVirtualTime { | ||
expect(1) | ||
|
||
val flow = (1..30).asFlow().onEach { delay(100) } | ||
val result = flow.dropWhileBusy().conflate().onEach { delay(1000) }.toList() | ||
|
||
assertEquals(listOf(1, 10, 20, 30), result) | ||
|
||
finish(2) | ||
} | ||
|
||
@Test | ||
fun `conflating a flow upstream of dropWhileBusy fuses to dropWhileBusy`() = withVirtualTime { | ||
expect(1) | ||
|
||
val flow = (1..30).asFlow().onEach { delay(100) } | ||
val result = flow.conflate().dropWhileBusy().onEach { delay(1000) }.toList() | ||
|
||
assertEquals(listOf(1, 11, 21), result) | ||
|
||
finish(2) | ||
} | ||
|
||
@Test | ||
fun `dropWhileBusy can fuse with a normal produce ChannelFlow`() = withVirtualTime { | ||
expect(1) | ||
|
||
val flow = channelFlow { | ||
(1..30).forEach { | ||
send(it) | ||
delay(100) | ||
} | ||
} | ||
val result = flow.dropWhileBusy().onEach { delay(1000) }.toList() | ||
|
||
assertEquals(listOf(1, 11, 21), result) | ||
|
||
finish(2) | ||
} | ||
} |