Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Bind BlockHoundMode setting to coroutines, lifting restrictions * Refactor concurrency * Avoid the use of `runBlocking` inside coroutines. Schedule all coroutines below the test engine layer via regular dispatchers, bringing the test environment more in line with actual production environments. * Remove direct use of Java's `Executors`, delegating to kotlinx.coroutines-provided dispatchers instead. * Preserve coroutine context (which includes lots of Kotest configuration) in multithreaded test executions. * Remove hard-coded 1-day time limit for multithreaded test executions. * Change tests relying on single-threading from a coroutine model to a thread model, removing coroutine invocations (`delay`), replacing those with thread invocations (`Thread.sleep`). * Avoid leaking threads by closing dispatchers after use. * Keep the `assertionCounter` synchronized with thread-switching coroutines. * Remove `fun <K, V> concurrentHashMap(): MutableMap<K, V>`, which was not thread-safe. * Make `FixedThreadCoroutineDispatcherFactory.dispatcherAffinity` thread-safe. * Revert a change in `ConcurrentTestSuiteScheduler.schedule` by commit c316bbd, which replaced `launch` with `async` plus `joinAll`. This was functionally equivalent, but `async` was not needed, as there were no values returned, and `joinAll` was already performed by the enclosing `coroutineScope`. * Allow 1 thread configured for test case, let invocations default to 1 * Update API * Simplify replay * Fix multithreading test, provoke thread switching more reliably * Fix System.out/err handling: flush streams when switching or capturing This avoids missing captured output. * Stabilize multithreading tests, provoke thread switching consistently * Make CollectingTestEngineListener thread-safe internally * CollectingTestEngineListener: bring back `result` methods * Stabilize provoked thread switching with Dispatchers.Unconfined Increase the minimum delay to 50 milliseconds based on observed behavior. * Update API dump * Make provokeThreadSwitch internal and non-suspending --------- Co-authored-by: Sam <sam@sksamuel.com>
- Loading branch information
Showing
45 changed files
with
399 additions
and
305 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
.../src/jvmTest/kotlin/com/sksamuel/kotest/assertions/AssertionCounterMultithreadingTests.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,25 @@ | ||
package com.sksamuel.kotest.assertions | ||
|
||
import io.kotest.assertions.assertionCounter | ||
import io.kotest.assertions.assertionCounterContextElement | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.collections.shouldHaveSize | ||
import io.kotest.matchers.shouldBe | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.withContext | ||
|
||
class AssertionCounterMultithreadingTests : FunSpec({ | ||
test("assertionCounter should work across coroutine thread switch") { | ||
withContext(Dispatchers.Unconfined + assertionCounterContextElement) { | ||
val threadIds = mutableSetOf<Long>() | ||
assertionCounter.inc() | ||
threadIds.add(Thread.currentThread().id) | ||
delay(50) | ||
assertionCounter.inc() | ||
threadIds.add(Thread.currentThread().id) | ||
assertionCounter.get() shouldBe 2 | ||
threadIds shouldHaveSize 2 | ||
} | ||
} | ||
}) |
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
35 changes: 28 additions & 7 deletions
35
...st-assertions/kotest-assertions-shared/src/jvmMain/kotlin/io/kotest/assertions/counter.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 |
---|---|---|
@@ -1,15 +1,36 @@ | ||
@file:JvmName("jvmcounter") | ||
|
||
package io.kotest.assertions | ||
|
||
actual val assertionCounter: AssertionCounter = ThreadLocalAssertionCounter | ||
import kotlinx.coroutines.asContextElement | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
actual val assertionCounter: AssertionCounter get() = threadLocalAssertionCounter.get() | ||
|
||
/** | ||
* A [CoroutineContext.Element] which keeps the [assertionCounter] synchronized with thread-switching coroutines. | ||
* | ||
* When using [assertionCounter] without the Kotest framework, this context element should be added to a | ||
* coroutine context, e.g. via | ||
* - `runBlocking(assertionCounterContextElement) { ... }` | ||
* - `runTest(Dispatchers.IO + assertionCounterContextElement) { ... }` | ||
*/ | ||
val assertionCounterContextElement: CoroutineContext.Element | ||
get() = threadLocalAssertionCounter.asContextElement() | ||
|
||
object ThreadLocalAssertionCounter : AssertionCounter { | ||
private val threadLocalAssertionCounter: ThreadLocal<CoroutineLocalAssertionCounter> = | ||
ThreadLocal.withInitial { CoroutineLocalAssertionCounter() } | ||
|
||
private val context = object : ThreadLocal<Int>() { | ||
override fun initialValue(): Int = 0 | ||
private class CoroutineLocalAssertionCounter : AssertionCounter { | ||
private var value = 0 | ||
|
||
override fun get(): Int = value | ||
|
||
override fun reset() { | ||
value = 0 | ||
} | ||
|
||
override fun get(): Int = context.get() | ||
override fun reset() = context.set(0) | ||
override fun inc() = context.set(context.get() + 1) | ||
override fun inc() { | ||
value++ | ||
} | ||
} |
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
3 changes: 0 additions & 3 deletions
3
kotest-common/src/commonMain/kotlin/io/kotest/common/collections.kt
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
kotest-common/src/desktopMain/kotlin/io/kotest/common/collections.kt
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
kotest-common/src/jsMain/kotlin/io/kotest/common/concurrentHashMap.kt
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
kotest-common/src/jvmMain/kotlin/io/kotest/common/concurrentHashMap.kt
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.