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
Fix assertions mode when using coroutines in another thread #3604
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
28 changes: 28 additions & 0 deletions
28
...ertions-core/src/jvmTest/kotlin/com/sksamuel/kotest/assertions/AssertionModeThreadTest.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,28 @@ | ||
package com.sksamuel.kotest.assertions | ||
|
||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.core.test.AssertionMode | ||
import io.kotest.matchers.shouldBe | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
class AssertionModeThreadTest : FunSpec() { | ||
|
||
override fun assertionMode() = AssertionMode.Error | ||
|
||
init { | ||
|
||
test("assertions from another thread should be counted") { | ||
withContext(Dispatchers.Default.limitedParallelism(1)) { | ||
launch { | ||
1 shouldBe 1 | ||
1 shouldBe 1 | ||
1 shouldBe 1 | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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
60 changes: 60 additions & 0 deletions
60
...n/kotlin/io/kotest/engine/test/interceptors/AssertionModeThreadLocalContextInterceptor.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,60 @@ | ||
package io.kotest.engine.test.interceptors | ||
|
||
import io.kotest.assertions.ThreadLocalAssertionCounter | ||
import io.kotest.common.JVMOnly | ||
import io.kotest.common.TestNameContextElement | ||
import io.kotest.core.test.TestCase | ||
import io.kotest.core.test.TestResult | ||
import io.kotest.core.test.TestScope | ||
import io.kotest.engine.test.scopes.withCoroutineContext | ||
import kotlinx.coroutines.ThreadContextElement | ||
import kotlinx.coroutines.withContext | ||
import java.util.concurrent.ConcurrentHashMap | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlin.coroutines.coroutineContext | ||
|
||
@JVMOnly | ||
internal actual fun assertionModeThreadLocalContextInterceptor(): TestExecutionInterceptor = | ||
AssertionModeThreadLocalContextInterceptor | ||
|
||
/** | ||
* Installs the [AssertionCounterThreadContextElement]s into the running coroutine context. | ||
*/ | ||
object AssertionModeThreadLocalContextInterceptor : TestExecutionInterceptor { | ||
override suspend fun intercept( | ||
testCase: TestCase, | ||
scope: TestScope, | ||
test: suspend (TestCase, TestScope) -> TestResult | ||
): TestResult { | ||
val testNameContextElement = coroutineContext[TestNameContextElement] ?: error("Requires TestNameContextElement") | ||
return withContext(AssertionCounterThreadContextElement(testNameContextElement.testName)) { | ||
test(testCase, scope.withCoroutineContext(this.coroutineContext)) | ||
} | ||
} | ||
} | ||
|
||
private val testNameCounters = ConcurrentHashMap<String, Int>() | ||
|
||
class AssertionCounterThreadContextElement(private val testName: String) : ThreadContextElement<Int> { | ||
|
||
companion object Key : CoroutineContext.Key<AssertionCounterThreadContextElement> | ||
|
||
override val key: CoroutineContext.Key<AssertionCounterThreadContextElement> | ||
get() = Key | ||
|
||
// this is invoked before coroutine is resumed on current thread | ||
override fun updateThreadContext(context: CoroutineContext): Int { | ||
// need to use our backing map's value and install that in the thread local copy | ||
val counter = testNameCounters.getOrPut(testName) { 0 } | ||
ThreadLocalAssertionCounter.values.set(counter) | ||
// we track the state in our backing map so this can be ignored | ||
return -1 | ||
} | ||
|
||
// this is invoked after coroutine has suspended on current thread | ||
override fun restoreThreadContext(context: CoroutineContext, oldState: Int) { | ||
// need to put the current thread-local value into our backing map before the coroutine is switched out | ||
testNameCounters[testName] = ThreadLocalAssertionCounter.values.get() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if a test launches multiple coroutines and makes assertions in both? What happens if multiple coroutines launch at the same time but the last one to finish doesn't make any assertions? Won't we end up with a count of 0 assertions for the test? |
||
ThreadLocalAssertionCounter.values.set(oldState) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this safe if multiple coroutines are running for the same test simultaneously?
If all we need to track is "an assertion did / did not occur", perhaps an atomic boolean would be easier to reason about?