How to use DefaultCoroutineDispatcherFactory class of io.kotest.engine.concurrency package

Best Kotest code snippet using io.kotest.engine.concurrency.DefaultCoroutineDispatcherFactory

SpecExecutor.kt

Source:SpecExecutor.kt Github

copy

Full Screen

1package io.kotest.engine.spec2import io.kotest.common.ExperimentalKotest3import io.kotest.common.Platform4import io.kotest.common.flatMap5import io.kotest.common.platform6import io.kotest.core.concurrency.CoroutineDispatcherFactory7import io.kotest.core.config.ProjectConfiguration8import io.kotest.core.spec.DslDrivenSpec9import io.kotest.core.spec.Spec10import io.kotest.core.spec.SpecRef11import io.kotest.core.test.TestCase12import io.kotest.core.test.TestResult13import io.kotest.engine.interceptors.EngineContext14import io.kotest.engine.interceptors.toProjectContext15import io.kotest.engine.listener.TestEngineListener16import io.kotest.engine.spec.interceptor.ApplyExtensionsInterceptor17import io.kotest.engine.spec.interceptor.ConfigurationInContextInterceptor18import io.kotest.engine.spec.interceptor.EnabledIfSpecInterceptor19import io.kotest.engine.spec.interceptor.FinalizeSpecInterceptor20import io.kotest.engine.spec.interceptor.IgnoreNestedSpecStylesInterceptor21import io.kotest.engine.spec.interceptor.IgnoredSpecInterceptor22import io.kotest.engine.spec.interceptor.PrepareSpecInterceptor23import io.kotest.engine.spec.interceptor.ProjectContextInterceptor24import io.kotest.engine.spec.interceptor.RequiresTagSpecInterceptor25import io.kotest.engine.spec.interceptor.SpecExtensionInterceptor26import io.kotest.engine.spec.interceptor.SpecFilterInterceptor27import io.kotest.engine.spec.interceptor.SpecFinishedInterceptor28import io.kotest.engine.spec.interceptor.SpecRefExtensionInterceptor29import io.kotest.engine.spec.interceptor.SpecRefInterceptor30import io.kotest.engine.spec.interceptor.SpecStartedInterceptor31import io.kotest.engine.spec.interceptor.SystemPropertySpecFilterInterceptor32import io.kotest.engine.spec.interceptor.TagsExcludedSpecInterceptor33import io.kotest.mpp.Logger34import io.kotest.mpp.bestName35import kotlin.reflect.KClass36/**37 * Executes a single [SpecRef].38 *39 * Uses a [TestEngineListener] to notify of events in the spec lifecycle.40 *41 * The spec executor has two levels of interceptors:42 * [io.kotest.engine.spec.interceptor.SpecRefInterceptor] are executed before the spec is created.43 * [io.kotest.engine.spec.interceptor.SpecInterceptor] are executed after the spec is created.44 *45 */46@ExperimentalKotest47class SpecExecutor(48 private val defaultCoroutineDispatcherFactory: CoroutineDispatcherFactory,49 private val context: EngineContext,50) {51 private val logger = Logger(SpecExecutorDelegate::class)52 private val extensions = SpecExtensions(context.configuration.registry)53 private val listener = context.listener54 suspend fun execute(ref: SpecRef) {55 logger.log { Pair(ref.kclass.bestName(), "Received $ref") }56 referenceInterceptors(ref)57 }58 suspend fun execute(kclass: KClass<out Spec>) {59 execute(SpecRef.Reference(kclass))60 }61 private suspend fun referenceInterceptors(ref: SpecRef) {62 val interceptors = listOfNotNull(63 if (platform == Platform.JVM) EnabledIfSpecInterceptor(listener, context.configuration.registry) else null,64 IgnoredSpecInterceptor(listener, context.configuration.registry),65 SpecFilterInterceptor(listener, context.configuration.registry),66 SystemPropertySpecFilterInterceptor(listener, context.configuration.registry),67 TagsExcludedSpecInterceptor(listener, context.configuration),68 if (platform == Platform.JVM) RequiresTagSpecInterceptor(listener, context.configuration, context.configuration.registry) else null,69 SpecRefExtensionInterceptor(context.configuration.registry),70 SpecStartedInterceptor(listener),71 SpecFinishedInterceptor(listener),72 if (platform == Platform.JVM) ApplyExtensionsInterceptor(context.configuration.registry) else null,73 PrepareSpecInterceptor(context.configuration.registry),74 FinalizeSpecInterceptor(context.configuration.registry),75 )76 val innerExecute: suspend (SpecRef) -> Result<Map<TestCase, TestResult>> = {77 createInstance(ref).flatMap { specInterceptors(it) }78 }79 logger.log { Pair(ref.kclass.bestName(), "Executing ${interceptors.size} reference interceptors") }80 interceptors.foldRight(innerExecute) { ext: SpecRefInterceptor, fn: suspend (SpecRef) -> Result<Map<TestCase, TestResult>> ->81 { ref -> ext.intercept(ref, fn) }82 }.invoke(ref)83 }84 private suspend fun specInterceptors(spec: Spec): Result<Map<TestCase, TestResult>> {85 val interceptors = listOfNotNull(86 if (platform == Platform.JS) IgnoreNestedSpecStylesInterceptor(listener, context.configuration.registry) else null,87 ProjectContextInterceptor(context.toProjectContext()),88 SpecExtensionInterceptor(context.configuration.registry),89 ConfigurationInContextInterceptor(context.configuration),90 )91 val initial: suspend (Spec) -> Result<Map<TestCase, TestResult>> = {92 try {93 val delegate = createSpecExecutorDelegate(listener, defaultCoroutineDispatcherFactory, context.configuration)94 logger.log { Pair(spec::class.bestName(), "delegate=$delegate") }95 Result.success(delegate.execute(spec))96 } catch (t: Throwable) {97 logger.log { Pair(spec::class.bestName(), "Error executing spec $t") }98 Result.failure(t)99 }100 }101 logger.log { Pair(spec::class.bestName(), "Executing ${interceptors.size} spec interceptors") }102 return interceptors.foldRight(initial) { ext, fn ->103 { spec -> ext.intercept(spec, fn) }104 }.invoke(spec)105 }106 /**107 * Creates an instance of the given [SpecRef], notifies users of the instantiation event108 * or instantiation failure, and returns a Result with the error or spec.109 *110 * After this method is called the spec is sealed.111 */112 private suspend fun createInstance(ref: SpecRef): Result<Spec> =113 ref.instance(context.configuration.registry)114 .onFailure { extensions.specInstantiationError(ref.kclass, it) }115 .flatMap { spec -> extensions.specInstantiated(spec).map { spec } }116 .onSuccess { if (it is DslDrivenSpec) it.seal() }117}118interface SpecExecutorDelegate {119 suspend fun execute(spec: Spec): Map<TestCase, TestResult>120}121@ExperimentalKotest122internal expect fun createSpecExecutorDelegate(123 listener: TestEngineListener,124 defaultCoroutineDispatcherFactory: CoroutineDispatcherFactory,125 configuration: ProjectConfiguration,126): SpecExecutorDelegate...

Full Screen

Full Screen

SingleInstanceSpecRunner.kt

Source:SingleInstanceSpecRunner.kt Github

copy

Full Screen

1package io.kotest.engine.spec.runners2import io.kotest.common.ExperimentalKotest3import io.kotest.common.flatMap4import io.kotest.core.concurrency.CoroutineDispatcherFactory5import io.kotest.core.config.ProjectConfiguration6import io.kotest.core.spec.Spec7import io.kotest.core.test.NestedTest8import io.kotest.core.test.TestCase9import io.kotest.core.test.TestResult10import io.kotest.core.test.TestScope11import io.kotest.engine.listener.TestEngineListener12import io.kotest.engine.spec.Materializer13import io.kotest.engine.spec.SpecExtensions14import io.kotest.engine.spec.SpecRunner15import io.kotest.engine.test.TestCaseExecutor16import io.kotest.engine.test.listener.TestCaseExecutionListenerToTestEngineListenerAdapter17import io.kotest.engine.test.scheduler.TestScheduler18import io.kotest.engine.test.scopes.DuplicateNameHandlingTestScope19import io.kotest.mpp.Logger20import io.kotest.mpp.bestName21import kotlinx.coroutines.coroutineScope22import java.util.concurrent.ConcurrentHashMap23import kotlin.coroutines.CoroutineContext24/**25 * Implementation of [SpecRunner] that executes all tests against the26 * same [Spec] instance. In other words, only a single instance of the spec class27 * is instantiated for all the test cases.28 */29@ExperimentalKotest30internal class SingleInstanceSpecRunner(31 listener: TestEngineListener,32 scheduler: TestScheduler,33 private val defaultCoroutineDispatcherFactory: CoroutineDispatcherFactory,34 private val configuration: ProjectConfiguration,35) : SpecRunner(listener, scheduler, configuration) {36 private val results = ConcurrentHashMap<TestCase, TestResult>()37 private val extensions = SpecExtensions(configuration.registry)38 private val logger = Logger(SingleInstanceSpecRunner::class)39 override suspend fun execute(spec: Spec): Result<Map<TestCase, TestResult>> {40 logger.log { Pair(spec::class.bestName(), "executing spec $spec") }41 suspend fun interceptAndRun(context: CoroutineContext) = runCatching {42 val rootTests = materializer.materialize(spec)43 logger.log { Pair(spec::class.bestName(), "Materialized root tests: ${rootTests.size}") }44 launch(spec) {45 logger.log { Pair(it.name.testName, "Executing test $it") }46 runTest(it, context, null)47 }48 }49 try {50 return coroutineScope {51 extensions.beforeSpec(spec)52 .flatMap { interceptAndRun(coroutineContext) }53 .flatMap { SpecExtensions(configuration.registry).afterSpec(spec) }54 .map { results }55 }56 } catch (e: Exception) {57 e.printStackTrace()58 throw e59 }60 }61 /**62 * A [TestScope] that runs discovered tests as soon as they are registered in the same spec instance.63 *64 * This implementation tracks fail fast if configured via TestCase config or globally.65 */66 inner class SingleInstanceTestScope(67 override val testCase: TestCase,68 override val coroutineContext: CoroutineContext,69 private val parentScope: SingleInstanceTestScope?,70 ) : TestScope {71 // set to true if we failed fast and should ignore further tests72 private var skipRemaining = false73 // in the single instance runner we execute each nested test as soon as they are registered74 override suspend fun registerTestCase(nested: NestedTest) {75 logger.log { Pair(testCase.name.testName, "Nested test case discovered '${nested}") }76 val nestedTestCase = Materializer(configuration).materialize(nested, testCase)77 if (skipRemaining) {78 logger.log { Pair(testCase.name.testName, "Skipping test due to fail fast") }79 listener.testIgnored(nestedTestCase, "Skipping test due to fail fast")80 } else {81 // if running this nested test results in an error, we won't launch anymore nested tests82 val result = runTest(nestedTestCase, coroutineContext, this@SingleInstanceTestScope)83 if (result.isErrorOrFailure) {84 if (testCase.config.failfast || configuration.projectWideFailFast) {85 logger.log { Pair(testCase.name.testName, "Test failed - setting skipRemaining = true") }86 skipRemaining = true87 parentScope?.skipRemaining = true88 }89 }90 }91 }92 }93 private suspend fun runTest(94 testCase: TestCase,95 coroutineContext: CoroutineContext,96 parentScope: SingleInstanceTestScope?,97 ): TestResult {98 val testExecutor = TestCaseExecutor(99 TestCaseExecutionListenerToTestEngineListenerAdapter(listener),100 defaultCoroutineDispatcherFactory,101 configuration,102 )103 val scope = DuplicateNameHandlingTestScope(104 configuration.duplicateTestNameMode,105 SingleInstanceTestScope(testCase, coroutineContext, parentScope)106 )107 val result = testExecutor.execute(testCase, scope)108 results[testCase] = result109 return result110 }111}...

Full Screen

Full Screen

TestCaseExecutor.kt

Source:TestCaseExecutor.kt Github

copy

Full Screen

1@file:OptIn(ExperimentalStdlibApi::class)2package io.kotest.engine.test3import io.kotest.common.ExperimentalKotest4import io.kotest.common.Platform5import io.kotest.common.platform6import io.kotest.core.concurrency.CoroutineDispatcherFactory7import io.kotest.core.config.ProjectConfiguration8import io.kotest.core.test.TestCase9import io.kotest.core.test.TestResult10import io.kotest.core.test.TestScope11import io.kotest.engine.concurrency.NoopCoroutineDispatcherFactory12import io.kotest.engine.test.interceptors.AssertionModeInterceptor13import io.kotest.engine.test.interceptors.CoroutineDebugProbeInterceptor14import io.kotest.engine.test.interceptors.CoroutineLoggingInterceptor15import io.kotest.engine.test.interceptors.EnabledCheckInterceptor16import io.kotest.engine.test.interceptors.InvocationCountCheckInterceptor17import io.kotest.engine.test.interceptors.InvocationTimeoutInterceptor18import io.kotest.engine.test.interceptors.LifecycleInterceptor19import io.kotest.engine.test.interceptors.SoftAssertInterceptor20import io.kotest.engine.test.interceptors.SupervisorScopeInterceptor21import io.kotest.engine.test.interceptors.TestCaseExtensionInterceptor22import io.kotest.engine.test.interceptors.TestCoroutineInterceptor23import io.kotest.engine.test.interceptors.TestDispatcherInterceptor24import 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,68 )69 val innerExecute: suspend (TestCase, TestScope) -> TestResult = { tc, scope ->70 logger.log { Pair(testCase.name.testName, "Executing test") }71 tc.test(scope)72 try {73 TestResult.Success(timeMark.elapsedNow())74 } catch (e: Throwable) {75 TestResult.Success(Duration.ZERO) // workaround for kotlin 1.576 }77 }78 return interceptors.foldRight(innerExecute) { ext, fn ->79 { tc, sc -> ext.intercept(tc, sc, fn) }80 }.invoke(testCase, testScope)81 }82}...

Full Screen

Full Screen

createSpecExecutorDelegate.kt

Source:createSpecExecutorDelegate.kt Github

copy

Full Screen

1package io.kotest.engine.spec2import io.kotest.common.ExperimentalKotest3import io.kotest.core.concurrency.CoroutineDispatcherFactory4import io.kotest.core.config.ProjectConfiguration5import io.kotest.core.spec.IsolationMode6import io.kotest.core.spec.Spec7import io.kotest.core.test.TestCase8import io.kotest.core.test.TestResult9import io.kotest.engine.concurrency.isIsolate10import io.kotest.engine.listener.TestEngineListener11import io.kotest.engine.spec.runners.InstancePerLeafSpecRunner12import io.kotest.engine.spec.runners.InstancePerTestSpecRunner13import io.kotest.engine.spec.runners.SingleInstanceSpecRunner14import io.kotest.engine.test.scheduler.ConcurrentTestScheduler15import io.kotest.engine.test.scheduler.SequentialTestScheduler16import io.kotest.mpp.Logger17import io.kotest.mpp.bestName18import kotlin.math.max19@ExperimentalKotest20internal actual fun createSpecExecutorDelegate(21 listener: TestEngineListener,22 defaultCoroutineDispatcherFactory: CoroutineDispatcherFactory,23 configuration: ProjectConfiguration,24): SpecExecutorDelegate = JvmSpecExecutorDelegate(listener, defaultCoroutineDispatcherFactory, configuration)25@ExperimentalKotest26class JvmSpecExecutorDelegate(27 private val listener: TestEngineListener,28 private val dispatcherFactory: CoroutineDispatcherFactory,29 private val configuration: ProjectConfiguration,30) : SpecExecutorDelegate {31 private val logger = Logger(JvmSpecExecutorDelegate::class)32 private fun Spec.resolvedIsolationMode() =33 this.isolationMode() ?: this.isolationMode ?: configuration.isolationMode34 override suspend fun execute(spec: Spec): Map<TestCase, TestResult> {35 val scheduler = when (val concurrentTests = spec.resolvedConcurrentTests(configuration.concurrentTests)) {36 ProjectConfiguration.Sequential -> SequentialTestScheduler37 else -> ConcurrentTestScheduler(max(1, concurrentTests))38 }39 val isolation = spec.resolvedIsolationMode()40 logger.log { Pair(spec::class.bestName(), "isolation=$isolation") }41 val runner = when (isolation) {42 IsolationMode.SingleInstance -> SingleInstanceSpecRunner(43 listener,44 scheduler,45 dispatcherFactory,46 configuration47 )48 IsolationMode.InstancePerTest -> InstancePerTestSpecRunner(49 listener,50 scheduler,51 dispatcherFactory,52 configuration53 )54 IsolationMode.InstancePerLeaf -> InstancePerLeafSpecRunner(55 listener,56 scheduler,57 dispatcherFactory,58 configuration59 )60 }61 return runner.execute(spec).getOrThrow()62 }63}64/**65 * Returns the concurrent tests count to use for tests in this spec.66 *67 * If threads is specified on the spec, then that will implicitly raise the concurrentTests68 * count to the same value if concurrentTests is not specified.69 *70 * Note that if this spec is annotated with @Isolate then the value71 * will be 1 regardless of the config setting.72 *73 * spec.concurrency ?: configuration.concurrentTests74 */75@OptIn(ExperimentalKotest::class)76internal fun Spec.resolvedConcurrentTests(defaultConcurrentTests: Int): Int {77 val fromSpecConcurrency = this.concurrency ?: this.concurrency()78 return when {79 this::class.isIsolate() -> ProjectConfiguration.Sequential80 fromSpecConcurrency != null -> max(1, fromSpecConcurrency)81 else -> defaultConcurrentTests82 }83}...

Full Screen

Full Screen

ConcurrentTestSuiteScheduler.kt

Source:ConcurrentTestSuiteScheduler.kt Github

copy

Full Screen

1package io.kotest.engine2import io.kotest.common.ExperimentalKotest3import io.kotest.core.annotation.DoNotParallelize4import io.kotest.core.annotation.Isolate5import io.kotest.core.project.TestSuite6import io.kotest.core.spec.SpecRef7import io.kotest.engine.concurrency.defaultCoroutineDispatcherFactory8import io.kotest.engine.concurrency.isIsolate9import io.kotest.engine.interceptors.EngineContext10import io.kotest.engine.listener.CollectingTestEngineListener11import io.kotest.engine.spec.SpecExecutor12import io.kotest.mpp.Logger13import io.kotest.mpp.bestName14import kotlinx.coroutines.coroutineScope15import kotlinx.coroutines.launch16import kotlinx.coroutines.sync.Semaphore17import kotlinx.coroutines.sync.withPermit18/**19 * A [TestSuiteScheduler] that schedules specs concurrently, up to a provided [maxConcurrent] value.20 * If the value is 1 then this scheduler will execute specs strictly sequentially.21 *22 * Additionally, on JVM targets, it will recognize the [Isolate] and [DoNotParallelize]23 * annotations to ensure those specs are never scheduled concurrently.24 *25 * @param maxConcurrent The maximum number of concurrent coroutines.26 */27@ExperimentalKotest28internal class ConcurrentTestSuiteScheduler(29 private val maxConcurrent: Int,30 private val context: EngineContext,31) : TestSuiteScheduler {32 private val logger = Logger(ConcurrentTestSuiteScheduler::class)33 override suspend fun schedule(suite: TestSuite): EngineResult {34 logger.log { Pair(null, "Launching ${suite.specs.size} specs") }35 val (sequential, concurrent) = suite.specs.partition { it.kclass.isIsolate() }36 logger.log { Pair(null, "Split on isIsolate: ${sequential.size} sequential ${concurrent.size} concurrent") }37 schedule(concurrent, maxConcurrent)38 logger.log { Pair(null, "Concurrent specs have completed") }39 schedule(sequential, 1)40 logger.log { Pair(null, "Sequential specs have completed") }41 return EngineResult(emptyList())42 }43 private suspend fun schedule(44 specs: List<SpecRef>,45 concurrency: Int,46 ) = coroutineScope { // we don't want this function to return until all specs are completed47 val coroutineDispatcherFactory = defaultCoroutineDispatcherFactory(context.configuration)48 val semaphore = Semaphore(concurrency)49 val collector = CollectingTestEngineListener()50 specs.forEach { ref ->51 logger.log { Pair(ref.kclass.bestName(), "Scheduling coroutine") }52 launch {53 semaphore.withPermit {54 logger.log { Pair(ref.kclass.bestName(), "Acquired permit") }55 if (context.configuration.projectWideFailFast && collector.errors) {56 context.listener.specIgnored(ref.kclass, null)57 } else {58 try {59 val executor = SpecExecutor(coroutineDispatcherFactory, context.mergeListener(collector))60 logger.log { Pair(ref.kclass.bestName(), "Executing ref") }61 executor.execute(ref)62 } catch (t: Throwable) {63 logger.log { Pair(ref.kclass.bestName(), "Unhandled error during spec execution $t") }64 throw t65 }66 }67 }68 logger.log { Pair(ref.kclass.bestName(), "Released permit") }69 }70 }71 }72}...

Full Screen

Full Screen

coroutineDispatcherFactoryInterceptor.kt

Source:coroutineDispatcherFactoryInterceptor.kt Github

copy

Full Screen

1package io.kotest.engine.test.interceptors2import io.kotest.core.concurrency.CoroutineDispatcherFactory3import io.kotest.core.test.TestCase4import io.kotest.core.test.TestResult5import io.kotest.core.test.TestScope6import io.kotest.engine.concurrency.FixedThreadCoroutineDispatcherFactory7import io.kotest.engine.test.scopes.withCoroutineContext8import io.kotest.mpp.Logger9import kotlinx.coroutines.CoroutineDispatcher10import 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,29 scope: TestScope,30 test: suspend (TestCase, TestScope) -> TestResult31 ): TestResult {32 val currentDispatcher = coroutineContext[CoroutineDispatcher]33 // we don't override if we've set a test dispatcher on this already34 return if (currentDispatcher is TestDispatcher) {35 test(testCase, scope)36 } else {37 val userFactory = testCase.spec.coroutineDispatcherFactory ?: testCase.spec.coroutineDispatcherFactory()38 val threads = testCase.spec.threads ?: testCase.spec.threads() ?: 139 logger.log { Pair(testCase.name.testName, "userFactory=$userFactory; threads=$threads") }40 val f = when {41 userFactory != null -> userFactory42 threads > 1 -> FixedThreadCoroutineDispatcherFactory(threads, false)43 else -> defaultCoroutineDispatcherFactory44 }45 logger.log { Pair(testCase.name.testName, "Switching dispatcher using factory $f") }46 f.withDispatcher(testCase) {47 test(testCase, scope.withCoroutineContext(coroutineContext))48 }49 }50 }51}...

Full Screen

Full Screen

interceptors.kt

Source:interceptors.kt Github

copy

Full Screen

1package io.kotest.engine.test.interceptors2import 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")...

Full Screen

Full Screen

DefaultCoroutineDispatcherFactory.kt

Source:DefaultCoroutineDispatcherFactory.kt Github

copy

Full Screen

1package io.kotest.engine.concurrency2import io.kotest.core.concurrency.CoroutineDispatcherFactory3import io.kotest.core.config.ProjectConfiguration4internal actual fun defaultCoroutineDispatcherFactory(configuration: ProjectConfiguration): CoroutineDispatcherFactory =5 NoopCoroutineDispatcherFactory...

Full Screen

Full Screen

DefaultCoroutineDispatcherFactory

Using AI Code Generation

copy

Full Screen

1val dispatcherFactory = DefaultCoroutineDispatcherFactory()2val dispatcherFactory = CustomCoroutineDispatcherFactory()3val dispatcherFactory = CustomCoroutineDispatcherFactory(Dispatchers.Default)4val dispatcherFactory = CustomCoroutineDispatcherFactory(Dispatchers.Default, 1000L)5val dispatcherFactory = CustomCoroutineDispatcherFactory(1000L)6val dispatcherFactory = CustomCoroutineDispatcherFactory(Dispatchers.Default, 1000L, "my-thread-name")7val dispatcherFactory = CustomCoroutineDispatcherFactory(Dispatchers.Default, "my-thread-name")8val dispatcherFactory = CustomCoroutineDispatcherFactory(1000L, "my-thread-name")9val dispatcherFactory = CustomCoroutineDispatcherFactory("my-thread-name")10val dispatcherFactory = CustomCoroutineDispatcherFactory(Dispatchers.Default, 1000L, "my-thread-name", myThreadFactory)11val dispatcherFactory = CustomCoroutineDispatcherFactory(Dispatchers.Default, "my-thread-name", myThreadFactory)12val dispatcherFactory = CustomCoroutineDispatcherFactory(1000L, "my-thread-name", myThreadFactory)

Full Screen

Full Screen

DefaultCoroutineDispatcherFactory

Using AI Code Generation

copy

Full Screen

1import io.kotest.core.config.AbstractProjectConfig2class ProjectConfig : AbstractProjectConfig() {3override fun coroutineDispatcherFactory() = DefaultCoroutineDispatcherFactory()4}5import io.kotest.core.config.AbstractProjectConfig6class ProjectConfig : AbstractProjectConfig() {7override fun coroutineDispatcherFactory() = MyCustomCoroutineDispatcherFactory()8}9import io.kotest.core.config.AbstractProjectConfig10class ProjectConfig : AbstractProjectConfig() {11override fun coroutineDispatcherFactory() = MyCustomCoroutineDispatcherFactory.instance12}13import io.kotest.core.config.AbstractProjectConfig14class ProjectConfig : AbstractProjectConfig() {15override fun coroutineDispatcherFactory() = MyCustomCoroutineDispatcherFactory.instance16}17import io.kotest.core.config.AbstractProjectConfig18class ProjectConfig : AbstractProjectConfig() {19override fun coroutineDispatcherFactory() = MyCustomCoroutineDispatcherFactory.instance20}21import io.kotest.core.config.AbstractProjectConfig22class ProjectConfig : AbstractProjectConfig() {23override fun coroutineDispatcherFactory() = MyCustomCoroutineDispatcherFactory.instance24}25import io.kotest.core.config.AbstractProjectConfig26class ProjectConfig : AbstractProjectConfig() {27override fun coroutineDispatcherFactory() = MyCustomCoroutineDispatcherFactory.instance28}29import io.kotest.core.config.AbstractProjectConfig30class ProjectConfig : AbstractProjectConfig() {31override fun coroutineDispatcherFactory() = MyCustomCoroutineDispatcherFactory.instance32}33import io.kotest.core.config.AbstractProjectConfig34class ProjectConfig : AbstractProjectConfig() {35override fun coroutineDispatcherFactory() = MyCustomCoroutineDispatcherFactory.instance36}37import io.kotest.core.config.AbstractProjectConfig38class ProjectConfig : AbstractProjectConfig() {39override fun coroutineDispatcherFactory() = MyCustomCoroutineDispatcherFactory.instance40}41import io.kotest.core.config.AbstractProjectConfig

Full Screen

Full Screen

DefaultCoroutineDispatcherFactory

Using AI Code Generation

copy

Full Screen

1val defaultCoroutineDispatcherFactory = DefaultCoroutineDispatcherFactory()2val defaultCoroutineDispatcherFactory = DefaultCoroutineDispatcherFactory()3val defaultCoroutineDispatcherFactory = DefaultCoroutineDispatcherFactory()4val defaultCoroutineDispatcherFactory = DefaultCoroutineDispatcherFactory()5val defaultCoroutineDispatcherFactory = DefaultCoroutineDispatcherFactory()6val defaultCoroutineDispatcherFactory = DefaultCoroutineDispatcherFactory()7val defaultCoroutineDispatcherFactory = DefaultCoroutineDispatcherFactory()8val defaultCoroutineDispatcherFactory = DefaultCoroutineDispatcherFactory()9val defaultCoroutineDispatcherFactory = DefaultCoroutineDispatcherFactory()10val defaultCoroutineDispatcherFactory = DefaultCoroutineDispatcherFactory()11val defaultCoroutineDispatcherFactory = DefaultCoroutineDispatcherFactory()12val defaultCoroutineDispatcherFactory = DefaultCoroutineDispatcherFactory()

Full Screen

Full Screen

DefaultCoroutineDispatcherFactory

Using AI Code Generation

copy

Full Screen

1val dispatcherFactory = DefaultCoroutineDispatcherFactory(4)2val listener = TestEngineListener(dispatcherFactory)3val engine = TestEngine(listOf(listener))4engine.execute(listOf(spec))5thread(start = true) {6engine.execute(listOf(spec))7}8thread.join()9assertEquals(4, listener.testCount)10}11}12val listener = TestEngineListener(dispatcherFactory)13val engine = TestEngine(listOf(listener))14engine.execute(listOf(spec))15thread(start = true) {16engine.execute(listOf(spec))17}18thread.join()19assertEquals(4, listener.testCount)20}21}22val listener = TestEngineListener(dispatcherFactory)23val engine = TestEngine(listOf(listener))24engine.execute(listOf(spec))25thread(start = true) {

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Kotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in DefaultCoroutineDispatcherFactory

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful