-
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.
Make Flow more cancellation friendly:
* flow builder is cancellable by default * New unambiguous currentContext top-level function * New Flow.cancellable() operator * Set of lint to catch programmer's errors in compile time Fixes #2026
- Loading branch information
Showing
13 changed files
with
153 additions
and
7 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
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
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
29 changes: 29 additions & 0 deletions
29
kotlinx-coroutines-core/common/test/flow/operators/CancellableTest.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,29 @@ | ||
/* | ||
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.flow.operators | ||
|
||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.flow.* | ||
import kotlin.test.* | ||
|
||
class CancellableTest : TestBase() { | ||
|
||
@Test | ||
fun testCancellable() = runTest { | ||
var sum = 0 | ||
val flow = (0..1000).asFlow() | ||
.onEach { | ||
if (it != 0) currentContext().cancel() | ||
sum += it | ||
} | ||
|
||
flow.launchIn(this).join() | ||
assertEquals(500500, sum) | ||
|
||
sum = 0 | ||
flow.cancellable().launchIn(this).join() | ||
assertEquals(1, sum) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -58,6 +58,7 @@ class FlatMapMergeTest : FlatMapMergeBaseTest() { | |
} | ||
launch { | ||
expect(2) | ||
yield() | ||
job.cancel() | ||
} | ||
} | ||
|
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
46 changes: 46 additions & 0 deletions
46
kotlinx-coroutines-core/jvm/test/flow/FlowCancellationTest.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,46 @@ | ||
/* | ||
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.flow | ||
|
||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.channels.* | ||
import kotlin.test.* | ||
|
||
class FlowCancellationTest : TestBase() { | ||
|
||
@Test | ||
fun testEmitIsCooperative() = runTest { | ||
val latch = Channel<Unit>(1) | ||
val job = flow { | ||
expect(1) | ||
latch.send(Unit) | ||
while (true) { | ||
emit(42) | ||
} | ||
}.launchIn(this + Dispatchers.Default) | ||
|
||
latch.receive() | ||
expect(2) | ||
job.cancelAndJoin() | ||
finish(3) | ||
} | ||
|
||
@Test | ||
fun testIsActiveOnCurrentContext() = runTest { | ||
val latch = Channel<Unit>(1) | ||
val job = flow<Unit> { | ||
expect(1) | ||
latch.send(Unit) | ||
while (currentContext().isActive) { | ||
// Do nothing | ||
} | ||
}.launchIn(this + Dispatchers.Default) | ||
|
||
latch.receive() | ||
expect(2) | ||
job.cancelAndJoin() | ||
finish(3) | ||
} | ||
} |
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