diff options
Diffstat (limited to 'opendc-simulator/opendc-simulator-core/src')
4 files changed, 41 insertions, 37 deletions
diff --git a/opendc-simulator/opendc-simulator-core/src/main/kotlin/org/opendc/simulator/kotlin/SimulationBuilders.kt b/opendc-simulator/opendc-simulator-core/src/main/kotlin/org/opendc/simulator/kotlin/SimulationBuilders.kt index 271b89e0..bc232ce0 100644 --- a/opendc-simulator/opendc-simulator-core/src/main/kotlin/org/opendc/simulator/kotlin/SimulationBuilders.kt +++ b/opendc-simulator/opendc-simulator-core/src/main/kotlin/org/opendc/simulator/kotlin/SimulationBuilders.kt @@ -66,14 +66,15 @@ import kotlin.coroutines.EmptyCoroutineContext public fun runSimulation( context: CoroutineContext = EmptyCoroutineContext, scheduler: SimulationDispatcher = SimulationDispatcher(), - body: suspend SimulationCoroutineScope.() -> Unit + body: suspend SimulationCoroutineScope.() -> Unit, ) { val (safeContext, job, dispatcher) = context.checkArguments(scheduler) val startingJobs = job.activeJobs() val scope = SimulationCoroutineScope(safeContext) - val deferred = scope.async { - body(scope) - } + val deferred = + scope.async { + body(scope) + } dispatcher.advanceUntilIdle() deferred.getCompletionExceptionOrNull()?.let { throw it @@ -105,9 +106,10 @@ private fun Job.activeJobs(): Set<Job> { * Convert a [ContinuationInterceptor] into a [SimulationDispatcher] if possible. */ internal fun ContinuationInterceptor.asSimulationDispatcher(): SimulationDispatcher { - val provider = this as? DispatcherProvider ?: throw IllegalArgumentException( - "DispatcherProvider such as SimulatorCoroutineDispatcher as the ContinuationInterceptor(Dispatcher) is required" - ) + val provider = + this as? DispatcherProvider ?: throw IllegalArgumentException( + "DispatcherProvider such as SimulatorCoroutineDispatcher as the ContinuationInterceptor(Dispatcher) is required", + ) return provider.dispatcher as? SimulationDispatcher ?: throw IllegalArgumentException("Active dispatcher is not a SimulationDispatcher") } diff --git a/opendc-simulator/opendc-simulator-core/src/main/kotlin/org/opendc/simulator/kotlin/SimulationCoroutineScope.kt b/opendc-simulator/opendc-simulator-core/src/main/kotlin/org/opendc/simulator/kotlin/SimulationCoroutineScope.kt index ca49fc53..a29e9404 100644 --- a/opendc-simulator/opendc-simulator-core/src/main/kotlin/org/opendc/simulator/kotlin/SimulationCoroutineScope.kt +++ b/opendc-simulator/opendc-simulator-core/src/main/kotlin/org/opendc/simulator/kotlin/SimulationCoroutineScope.kt @@ -43,7 +43,7 @@ public interface SimulationCoroutineScope : CoroutineScope, SimulationController */ public fun SimulationCoroutineScope( context: CoroutineContext = EmptyCoroutineContext, - scheduler: SimulationDispatcher = SimulationDispatcher() + scheduler: SimulationDispatcher = SimulationDispatcher(), ): SimulationCoroutineScope { var safeContext = context val simulationDispatcher: SimulationDispatcher diff --git a/opendc-simulator/opendc-simulator-core/src/test/kotlin/org/opendc/simulator/TaskQueueTest.kt b/opendc-simulator/opendc-simulator-core/src/test/kotlin/org/opendc/simulator/TaskQueueTest.kt index 56dd83aa..eaafedfd 100644 --- a/opendc-simulator/opendc-simulator-core/src/test/kotlin/org/opendc/simulator/TaskQueueTest.kt +++ b/opendc-simulator/opendc-simulator-core/src/test/kotlin/org/opendc/simulator/TaskQueueTest.kt @@ -47,7 +47,7 @@ class TaskQueueTest { fun testPollEmpty() { assertAll( { assertEquals(Long.MAX_VALUE, queue.peekDeadline()) }, - { assertNull(queue.poll()) } + { assertNull(queue.poll()) }, ) } @@ -63,7 +63,7 @@ class TaskQueueTest { assertAll( { assertEquals(100, queue.peekDeadline()) }, { assertEquals(entry, queue.poll()) }, - { assertNull(queue.poll()) } + { assertNull(queue.poll()) }, ) } @@ -86,7 +86,7 @@ class TaskQueueTest { { assertEquals(entryB, queue.poll()) }, { assertEquals(entryC, queue.poll()) }, { assertEquals(entryA, queue.poll()) }, - { assertNull(queue.poll()) } + { assertNull(queue.poll()) }, ) } @@ -109,7 +109,7 @@ class TaskQueueTest { { assertEquals(entryA, queue.poll()) }, { assertEquals(entryB, queue.poll()) }, { assertEquals(entryC, queue.poll()) }, - { assertNull(queue.poll()) } + { assertNull(queue.poll()) }, ) } @@ -136,7 +136,7 @@ class TaskQueueTest { { assertEquals(entryD, queue.poll()) }, { assertEquals(entryC, queue.poll()) }, { assertEquals(entryA, queue.poll()) }, - { assertNull(queue.poll()) } + { assertNull(queue.poll()) }, ) } @@ -160,7 +160,7 @@ class TaskQueueTest { { assertEquals(20, queue.peekDeadline()) }, { assertEquals(entryB, queue.poll()) }, { assertEquals(entryC, queue.poll()) }, - { assertNull(queue.poll()) } + { assertNull(queue.poll()) }, ) } @@ -184,7 +184,7 @@ class TaskQueueTest { { assertEquals(58, queue.peekDeadline()) }, { assertEquals(entryC, queue.poll()) }, { assertEquals(entryA, queue.poll()) }, - { assertNull(queue.poll()) } + { assertNull(queue.poll()) }, ) } @@ -208,7 +208,7 @@ class TaskQueueTest { { assertEquals(20, queue.peekDeadline()) }, { assertEquals(entryB, queue.poll()) }, { assertEquals(entryA, queue.poll()) }, - { assertNull(queue.poll()) } + { assertNull(queue.poll()) }, ) } @@ -228,7 +228,7 @@ class TaskQueueTest { assertAll( { assertFalse(queue.remove(10, 1)) }, - { assertFalse(queue.remove(58, 2)) } + { assertFalse(queue.remove(58, 2)) }, ) } } diff --git a/opendc-simulator/opendc-simulator-core/src/test/kotlin/org/opendc/simulator/kotlin/SimulationBuildersTest.kt b/opendc-simulator/opendc-simulator-core/src/test/kotlin/org/opendc/simulator/kotlin/SimulationBuildersTest.kt index 26419a50..b25025ef 100644 --- a/opendc-simulator/opendc-simulator-core/src/test/kotlin/org/opendc/simulator/kotlin/SimulationBuildersTest.kt +++ b/opendc-simulator/opendc-simulator-core/src/test/kotlin/org/opendc/simulator/kotlin/SimulationBuildersTest.kt @@ -38,31 +38,33 @@ import org.junit.jupiter.api.assertThrows */ class SimulationBuildersTest { @Test - fun testDelay() = runSimulation { - assertEquals(0, currentTime) - delay(100) - assertEquals(100, currentTime) - } + fun testDelay() = + runSimulation { + assertEquals(0, currentTime) + delay(100) + assertEquals(100, currentTime) + } @Test - fun testController() = runSimulation { - var completed = false + fun testController() = + runSimulation { + var completed = false - launch { - delay(20) - completed = true - } + launch { + delay(20) + completed = true + } - advanceBy(10) - assertFalse(completed) - advanceBy(11) - assertTrue(completed) + advanceBy(10) + assertFalse(completed) + advanceBy(11) + assertTrue(completed) - completed = false - launch { completed = true } - runCurrent() - assertTrue(completed) - } + completed = false + launch { completed = true } + runCurrent() + assertTrue(completed) + } @Test fun testFailOnActiveJobs() { |
