Best Kotest code snippet using io.kotest.engine.test.interceptors.coroutineDispatcherFactoryInterceptor.coroutineDispatcherFactoryInterceptor
TestCaseExecutor.kt
Source:TestCaseExecutor.kt
...24import io.kotest.engine.test.interceptors.TestFinishedInterceptor25import io.kotest.engine.test.interceptors.TestPathContextInterceptor26import io.kotest.engine.test.interceptors.TimeoutInterceptor27import io.kotest.engine.test.interceptors.blockedThreadTimeoutInterceptor28import io.kotest.engine.test.interceptors.coroutineDispatcherFactoryInterceptor29import io.kotest.engine.test.interceptors.coroutineErrorCollectorInterceptor30import io.kotest.mpp.Logger31import kotlin.time.Duration32import kotlin.time.TimeSource33/**34 * Executes a single [TestCase].35 *36 * Uses a [TestCaseExecutionListener] to notify callers of events in the test lifecycle.37 *38 */39@OptIn(ExperimentalKotest::class)40class TestCaseExecutor(41 private val listener: TestCaseExecutionListener,42 private val defaultCoroutineDispatcherFactory: CoroutineDispatcherFactory = NoopCoroutineDispatcherFactory,43 private val configuration: ProjectConfiguration,44) {45 private val logger = Logger(TestCaseExecutor::class)46 suspend fun execute(testCase: TestCase, testScope: TestScope): TestResult {47 val timeMark = TimeSource.Monotonic.markNow()48 val interceptors = listOfNotNull(49 TestPathContextInterceptor,50 TestFinishedInterceptor(listener),51 InvocationCountCheckInterceptor,52 SupervisorScopeInterceptor,53 if (platform == Platform.JVM) coroutineDispatcherFactoryInterceptor(defaultCoroutineDispatcherFactory) else null,54 if (platform == Platform.JVM) coroutineErrorCollectorInterceptor() else null,55 TestCaseExtensionInterceptor(configuration.registry),56 EnabledCheckInterceptor(configuration),57 LifecycleInterceptor(listener, timeMark, configuration.registry),58 AssertionModeInterceptor,59 SoftAssertInterceptor(),60 CoroutineLoggingInterceptor(configuration),61 if (platform == Platform.JVM) blockedThreadTimeoutInterceptor(configuration, timeMark) else null,62 TimeoutInterceptor(timeMark),63 TestInvocationInterceptor(configuration.registry, timeMark),64 InvocationTimeoutInterceptor,65 if (platform == Platform.JVM && testCase.config.testCoroutineDispatcher) TestDispatcherInterceptor() else null,66 if (platform == Platform.JVM && testCase.config.coroutineTestScope) TestCoroutineInterceptor() else null,67 CoroutineDebugProbeInterceptor,...
coroutineDispatcherFactoryInterceptor.kt
Source:coroutineDispatcherFactoryInterceptor.kt
...10import kotlinx.coroutines.test.TestCoroutineDispatcher11import kotlinx.coroutines.test.TestDispatcher12import kotlin.coroutines.coroutineContext13@ExperimentalStdlibApi14internal actual fun coroutineDispatcherFactoryInterceptor(15 defaultCoroutineDispatcherFactory: CoroutineDispatcherFactory16): TestExecutionInterceptor = CoroutineDispatcherFactoryInterceptor(defaultCoroutineDispatcherFactory)17/**18 * Switches execution onto a dispatcher provided by a [CoroutineDispatcherFactory].19 *20 * If the coroutine is an instance of [TestDispatcher] then the coroutine will not be changed.21 */22@ExperimentalStdlibApi23internal class CoroutineDispatcherFactoryInterceptor(24 private val defaultCoroutineDispatcherFactory: CoroutineDispatcherFactory25) : TestExecutionInterceptor {26 private val logger = Logger(CoroutineDispatcherFactoryInterceptor::class)27 override suspend fun intercept(28 testCase: TestCase,...
CoroutineDispatcherInterceptorTest.kt
Source:CoroutineDispatcherInterceptorTest.kt
1package com.sksamuel.kotest.engine.test.interceptors2import io.kotest.core.concurrency.CoroutineDispatcherFactory3import io.kotest.core.descriptors.append4import io.kotest.core.descriptors.toDescriptor5import io.kotest.core.names.TestName6import io.kotest.core.source.sourceRef7import io.kotest.core.spec.style.DescribeSpec8import io.kotest.core.test.TestCase9import io.kotest.core.test.TestResult10import io.kotest.core.test.TestType11import io.kotest.engine.test.interceptors.CoroutineDispatcherFactoryInterceptor12import io.kotest.engine.test.scopes.NoopTestScope13import io.kotest.matchers.string.shouldStartWith14import kotlinx.coroutines.asCoroutineDispatcher15import kotlinx.coroutines.withContext16import java.util.concurrent.Executors17import kotlin.time.Duration.Companion.milliseconds18@ExperimentalStdlibApi19class CoroutineDispatcherInterceptorTest : DescribeSpec() {20 init {21 describe("CoroutineDispatcherInterceptor") {22 it("should dispatch to coroutineDispatcher") {23 val tc = TestCase(24 InvocationCountCheckInterceptorTest::class.toDescriptor().append("foo"),25 TestName("foo"),26 InvocationCountCheckInterceptorTest(),27 {},28 sourceRef(),29 TestType.Container,30 )31 val controller = object : CoroutineDispatcherFactory {32 override suspend fun <T> withDispatcher(testCase: TestCase, f: suspend () -> T): T {33 val executor = Executors.newSingleThreadExecutor {34 val t = Thread(it)35 t.name = "foo"36 t37 }38 return withContext(executor.asCoroutineDispatcher()) {39 f()40 }41 }42 }43 CoroutineDispatcherFactoryInterceptor(controller).intercept(44 tc,45 NoopTestScope(tc, coroutineContext)46 ) { _, _ ->47 Thread.currentThread().name.shouldStartWith("foo")48 TestResult.Success(0.milliseconds)49 }50 }51 }52 }53}...
interceptors.kt
Source:interceptors.kt
2import io.kotest.common.platform3import io.kotest.core.concurrency.CoroutineDispatcherFactory4import io.kotest.core.config.ProjectConfiguration5import kotlin.time.TimeMark6internal actual fun coroutineDispatcherFactoryInterceptor(7 defaultCoroutineDispatcherFactory: CoroutineDispatcherFactory8): TestExecutionInterceptor = error("Unsupported on $platform")9internal actual fun blockedThreadTimeoutInterceptor(10 configuration: ProjectConfiguration,11 start: TimeMark,12): TestExecutionInterceptor = error("Unsupported on $platform")13internal actual fun coroutineErrorCollectorInterceptor(): TestExecutionInterceptor =14 error("Unsupported on $platform")...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!