How to use KClass.isIsolate method of io.kotest.engine.concurrency.concurrency class

Best Kotest code snippet using io.kotest.engine.concurrency.concurrency.KClass.isIsolate

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

concurrency.kt

Source:concurrency.kt Github

copy

Full Screen

1package io.kotest.engine.concurrency2import io.kotest.core.annotation.DoNotParallelize3import io.kotest.core.annotation.Isolate4import io.kotest.mpp.annotation5import kotlin.reflect.KClass6/**7 * Returns true if this class is annotated with either of the annotations used to indicate8 * this spec should not run concurrently regardless of config.9 *10 * Those annotations are [DoNotParallelize] and [Isolate].11 */12internal fun KClass<*>.isIsolate(): Boolean = annotation<DoNotParallelize>() != null || annotation<Isolate>() != null...

Full Screen

Full Screen

KClass.isIsolate

Using AI Code Generation

copy

Full Screen

1 val concurrency = concurrency()2 val testThread = Thread.currentThread()3 return when (concurrency) {4 is Concurrency.Isolated -> {5 }6 is Concurrency.Shared -> {7 val newContext = context.withValue(testThread, value)8 }9 }10 }11class ContextTest : FunSpec() {12 init {13 test("context should be set") {14 val context = context()15 context.get("hello") shouldBe "world"16 }17 }18}

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 method in concurrency

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful