Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ru.fix.corounit.engine
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.supervisorScope
import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.withContext
import mu.KotlinLogging
import org.junit.jupiter.api.*
Expand Down Expand Up @@ -49,7 +50,7 @@ class ClassRunner(
.firstOrNull { it.parameters.size == 1 }
}

suspend fun executeClass() {
suspend fun executeClass(methodsRunsSemaphore: Semaphore?) {
val classContext = TestClassContextElement(classDesc.clazz)
val pluginsClassContext = context.pluginDispatcher.beforeTestClass(classContext)

Expand All @@ -62,7 +63,9 @@ class ClassRunner(

supervisorScope {
for (methodDesc in classDesc.methodDescriptors) {
executeMethod(classContext, methodDesc, testInstance)
methodsRunsSemaphore.withPermitIfNotNull {
executeMethod(classContext, methodDesc, testInstance)
}
}
}

Expand All @@ -76,7 +79,9 @@ class ClassRunner(
supervisorScope {
for (methodDesc in classDesc.methodDescriptors) {
val testInstance = context.pluginDispatcher.createTestClassInstance(classDesc.clazz)
executeMethod(classContext, methodDesc, testInstance)
methodsRunsSemaphore.withPermitIfNotNull {
executeMethod(classContext, methodDesc, testInstance)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import org.junit.platform.engine.ConfigurationParameters
import java.util.concurrent.ForkJoinPool

class Configuration(configurationParameters: ConfigurationParameters) {

val concurrentTestClassesLimit = configurationParameters.get("corounit.execution.concurrent.test.classes.limit")
.map { it?.toInt() }
.orElse(null)

val concurrentTestMethodsLimit = configurationParameters.get("corounit.execution.concurrent.test.methods.limit")
.map { it?.toInt() }
.orElse(null)

val parallelism = configurationParameters.get("corounit.execution.parallelism")
.map { it?.toInt() }
.orElse(Math.max(ForkJoinPool.getCommonPoolParallelism(), 2))!!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
package ru.fix.corounit.engine

import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.sync.withPermit

class ExecutionRunner(private val context: ExecutionContext) {

private val classesRunsSemaphore = context.configuration.concurrentTestClassesLimit?.let { Semaphore(it) }
private val methodsRunsSemaphore = context.configuration.concurrentTestMethodsLimit?.let { Semaphore(it) }

suspend fun executeExecution(execDesc: CorounitExecutionDescriptor) {
context.notifyListenerAndRunInSupervisorScope(execDesc) {
for (classDesc in execDesc.children.mapNotNull { it as? CorounitClassDescriptior }) {
launch {
ClassRunner(context, classDesc).executeClass()
classesRunsSemaphore.withPermitIfNotNull {
ClassRunner(context, classDesc).executeClass(methodsRunsSemaphore)
}
}
}
}
}
}

suspend fun Semaphore?.withPermitIfNotNull(block: suspend () -> Unit) = if(this != null) {
withPermit { block() }
} else {
block()
Copy link
Author

@KazankovMarch KazankovMarch Aug 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code is needed to avoid any overhead for those who don't want to set up the limits.

But MAYBE we should add some limits by default

}
2 changes: 2 additions & 0 deletions corounit-engine/src/test/resources/junit-platform.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
corounit.execution.concurrent.test.classes.limit=5
corounit.execution.concurrent.test.methods.limit=10
corounit.execution.parallelism=2
# Corounit engine tests share single CorounitConfig class to assert interaction with engine plugins
# In order to ensure that one test do not affect another we are running them sequentially
Expand Down
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ You can use default `junit-platform.properties` file in test resources to specif

```properties
# junit-platform.properties
corounit.execution.concurrent.test.classes.limit=5
corounit.execution.concurrent.test.methods.limit=10
corounit.execution.parallelism=4
corounit.testinstance.lifecycle.default=per_class
```
Expand Down Expand Up @@ -376,6 +378,10 @@ class TestClass{
## Corounit Properties
You can add corounit properties within default JUnit property file at `src/test/resources/junit-platform.properties`:

* `corounit.execution.concurrent.test.classes.limit=null`
Max number of running concurrently test classes
* `corounit.execution.concurrent.test.methods.limit=null`
Max number of running concurrently test methods
* `corounit.execution.parallelism=4`
How many threads corounite engine will use to execute tests.
* `corounit.testinstance.lifecycle.default=per_class`
Expand Down