Skip to content
Snippets Groups Projects
Commit 18e3d6f9 authored by Benedikt Wetzel's avatar Benedikt Wetzel
Browse files

Restructure the comparator class

parent 5e886842
No related branches found
No related tags found
1 merge request!168Enhance Code Quality
...@@ -8,7 +8,7 @@ import theodolite.benchmark.KubernetesBenchmark ...@@ -8,7 +8,7 @@ import theodolite.benchmark.KubernetesBenchmark
import theodolite.execution.TheodoliteExecutor import theodolite.execution.TheodoliteExecutor
import theodolite.model.crd.* import theodolite.model.crd.*
import theodolite.patcher.ConfigOverrideModifier import theodolite.patcher.ConfigOverrideModifier
import theodolite.util.ExecutionComparator import theodolite.util.ExecutionStateComparator
import java.lang.Thread.sleep import java.lang.Thread.sleep
private val logger = KotlinLogging.logger {} private val logger = KotlinLogging.logger {}
...@@ -133,7 +133,7 @@ class TheodoliteController( ...@@ -133,7 +133,7 @@ class TheodoliteController(
* @return the next execution or null * @return the next execution or null
*/ */
private fun getNextExecution(): BenchmarkExecution? { private fun getNextExecution(): BenchmarkExecution? {
val comparator = ExecutionComparator().compareByState(States.RESTART) val comparator = ExecutionStateComparator(States.RESTART)
val availableBenchmarkNames = getBenchmarks() val availableBenchmarkNames = getBenchmarks()
.map { it.name } .map { it.name }
......
...@@ -3,16 +3,16 @@ package theodolite.util ...@@ -3,16 +3,16 @@ package theodolite.util
import theodolite.model.crd.ExecutionCRD import theodolite.model.crd.ExecutionCRD
import theodolite.model.crd.States import theodolite.model.crd.States
class ExecutionComparator { class ExecutionStateComparator(private val preferredState: States): Comparator<ExecutionCRD> {
/** /**
* Simple comparator which can be used to order a list of [ExecutionCRD] such that executions with * Simple comparator which can be used to order a list of [ExecutionCRD] such that executions with
* status [States.RESTART] are before all other executions. * status [States.RESTART] are before all other executions.
*/ */
fun compareByState(preferredState: States) = Comparator<ExecutionCRD> { a, b -> override fun compare(p0: ExecutionCRD, p1: ExecutionCRD): Int {
when { return when {
(a == null && b == null) -> 0 (p0 == null && p1 == null) -> 0
(a.status.executionState == preferredState.value) -> -1 (p0.status.executionState == preferredState.value) -> -1
else -> 1 else -> 1
} }
} }
......
...@@ -15,6 +15,10 @@ class ExecutionCRDummy(name: String, benchmark: String) { ...@@ -15,6 +15,10 @@ class ExecutionCRDummy(name: String, benchmark: String) {
return this.executionCR return this.executionCR
} }
fun getStatus() : ExecutionStatus {
return this.executionState
}
init { init {
// configure metadata // configure metadata
executionCR.spec = execution executionCR.spec = execution
......
package theodolite.util
import io.quarkus.test.junit.QuarkusTest
import org.junit.Rule
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import theodolite.execution.operator.ExecutionCRDummy
import theodolite.model.crd.ExecutionCRD
import theodolite.model.crd.States
@QuarkusTest
class ExecutionStateComparatorTest {
@Test
fun testCompare() {
val comparator = ExecutionStateComparator(States.RESTART)
val execution1 = ExecutionCRDummy("dummy1", "default-benchmark")
val execution2 = ExecutionCRDummy("dummy2", "default-benchmark")
execution1.getStatus().executionState = States.RESTART.value
execution2.getStatus().executionState = States.PENDING.value
val list = listOf(execution2.getCR(), execution1.getCR())
assertEquals(
list.reversed(),
list.sortedWith(comparator)
)
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment