forked from Kotlin/kotlinx.coroutines
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce EXACTLY_ONCE contracts to coroutineScope, supervisorScope, …
…withContext, runBlocking, withTimeout and select (Kotlin#2030)
- Loading branch information
Showing
9 changed files
with
138 additions
and
32 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
52 changes: 52 additions & 0 deletions
52
kotlinx-coroutines-core/common/test/BuilderContractsTest.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,52 @@ | ||
/* | ||
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines | ||
|
||
import kotlinx.coroutines.selects.* | ||
import kotlin.test.* | ||
|
||
class BuilderContractsTest : TestBase() { | ||
|
||
@Test | ||
fun testContracts() = runTest { | ||
// Coroutine scope | ||
val cs: Int | ||
coroutineScope { | ||
cs = 42 | ||
} | ||
consume(cs) | ||
|
||
// Supervisor scope | ||
val svs: Int | ||
supervisorScope { | ||
svs = 21 | ||
} | ||
consume(svs) | ||
|
||
// with context scope | ||
val wctx: Int | ||
withContext(Dispatchers.Unconfined) { | ||
wctx = 239 | ||
} | ||
consume(wctx) | ||
|
||
val wt: Int | ||
withTimeout(Long.MAX_VALUE) { | ||
wt = 123 | ||
} | ||
consume(wt) | ||
|
||
val s: Int | ||
select<Unit> { | ||
s = 42 | ||
Job().apply { complete() }.onJoin {} | ||
} | ||
consume(s) | ||
} | ||
|
||
private fun consume(a: Int) { | ||
a.hashCode() // BE codegen verification | ||
} | ||
} |
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