Best Kotest code snippet using io.kotest.engine.concurrency.concurrency.KClass.isIsolate
ConcurrentTestSuiteScheduler.kt
Source: ConcurrentTestSuiteScheduler.kt
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}...
concurrency.kt
Source: concurrency.kt
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...
KClass.isIsolate
Using AI Code Generation
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}
Check out the latest blogs from LambdaTest on this topic:
Agile software development stems from a philosophy that being agile means creating and responding to change swiftly. Agile means having the ability to adapt and respond to change without dissolving into chaos. Being Agile involves teamwork built on diverse capabilities, skills, and talents. Team members include both the business and software development sides working together to produce working software that meets or exceeds customer expectations continuously.
When working on web automation with Selenium, I encountered scenarios where I needed to refresh pages from time to time. When does this happen? One scenario is that I needed to refresh the page to check that the data I expected to see was still available even after refreshing. Another possibility is to clear form data without going through each input individually.
Building a website is all about keeping the user experience in mind. Ultimately, it’s about providing visitors with a mind-blowing experience so they’ll keep coming back. One way to ensure visitors have a great time on your site is to add some eye-catching text or image animations.
Automating testing is a crucial step in the development pipeline of a software product. In an agile development environment, where there is continuous development, deployment, and maintenance of software products, automation testing ensures that the end software products delivered are error-free.
As a developer, checking the cross browser compatibility of your CSS properties is of utmost importance when building your website. I have often found myself excited to use a CSS feature only to discover that it’s still not supported on all browsers. Even if it is supported, the feature might be experimental and not work consistently across all browsers. Ask any front-end developer about using a CSS feature whose support is still in the experimental phase in most prominent web browsers. ????
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!!