diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2021-04-21 16:35:52 +0200 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2021-04-21 16:37:18 +0200 |
| commit | 62678b2890a7f3640836b99ca2fec9efd7485929 (patch) | |
| tree | dc6d6e8bb495c019990513511d7ff042afde0f05 /simulator/opendc-simulator/opendc-simulator-resources/src | |
| parent | 1c0568c31d60d4e690b4b9aec2e14f660b72a5c8 (diff) | |
simulator: Migrate to SimulationCoroutineDispatcher
This change migrates the remainder of the codebase to the
SimulationCoroutineDispatcher implementation.
Diffstat (limited to 'simulator/opendc-simulator/opendc-simulator-resources/src')
8 files changed, 81 insertions, 135 deletions
diff --git a/simulator/opendc-simulator/opendc-simulator-resources/src/jmh/kotlin/org/opendc/simulator/resources/SimResourceBenchmarks.kt b/simulator/opendc-simulator/opendc-simulator-resources/src/jmh/kotlin/org/opendc/simulator/resources/SimResourceBenchmarks.kt index ab0abe23..beda3eaa 100644 --- a/simulator/opendc-simulator/opendc-simulator-resources/src/jmh/kotlin/org/opendc/simulator/resources/SimResourceBenchmarks.kt +++ b/simulator/opendc-simulator/opendc-simulator-resources/src/jmh/kotlin/org/opendc/simulator/resources/SimResourceBenchmarks.kt @@ -24,12 +24,10 @@ package org.opendc.simulator.resources import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.launch -import kotlinx.coroutines.test.TestCoroutineScope -import kotlinx.coroutines.test.runBlockingTest -import org.opendc.simulator.core.DelayControllerClockAdapter +import org.opendc.simulator.core.SimulationCoroutineScope +import org.opendc.simulator.core.runBlockingSimulation import org.opendc.utils.TimerScheduler import org.openjdk.jmh.annotations.* -import java.time.Clock import java.util.concurrent.TimeUnit @State(Scope.Thread) @@ -38,15 +36,13 @@ import java.util.concurrent.TimeUnit @Measurement(iterations = 5, time = 3, timeUnit = TimeUnit.SECONDS) @OptIn(ExperimentalCoroutinesApi::class) class SimResourceBenchmarks { - private lateinit var scope: TestCoroutineScope - private lateinit var clock: Clock + private lateinit var scope: SimulationCoroutineScope private lateinit var scheduler: TimerScheduler<Any> @Setup fun setUp() { - scope = TestCoroutineScope() - clock = DelayControllerClockAdapter(scope) - scheduler = TimerScheduler(scope.coroutineContext, clock) + scope = SimulationCoroutineScope() + scheduler = TimerScheduler(scope.coroutineContext, scope.clock) } @State(Scope.Thread) @@ -61,38 +57,38 @@ class SimResourceBenchmarks { @Benchmark fun benchmarkSource(state: Workload) { - return scope.runBlockingTest { + return scope.runBlockingSimulation { val provider = SimResourceSource(4200.0, clock, scheduler) - return@runBlockingTest provider.consume(state.consumers[0]) + return@runBlockingSimulation provider.consume(state.consumers[0]) } } @Benchmark fun benchmarkForwardOverhead(state: Workload) { - return scope.runBlockingTest { + return scope.runBlockingSimulation { val provider = SimResourceSource(4200.0, clock, scheduler) val forwarder = SimResourceForwarder() provider.startConsumer(forwarder) - return@runBlockingTest forwarder.consume(state.consumers[0]) + return@runBlockingSimulation forwarder.consume(state.consumers[0]) } } @Benchmark fun benchmarkSwitchMaxMinSingleConsumer(state: Workload) { - return scope.runBlockingTest { + return scope.runBlockingSimulation { val switch = SimResourceSwitchMaxMin(clock) switch.addInput(SimResourceSource(3000.0, clock, scheduler)) switch.addInput(SimResourceSource(3000.0, clock, scheduler)) val provider = switch.addOutput(3500.0) - return@runBlockingTest provider.consume(state.consumers[0]) + return@runBlockingSimulation provider.consume(state.consumers[0]) } } @Benchmark fun benchmarkSwitchMaxMinTripleConsumer(state: Workload) { - return scope.runBlockingTest { + return scope.runBlockingSimulation { val switch = SimResourceSwitchMaxMin(clock) switch.addInput(SimResourceSource(3000.0, clock, scheduler)) @@ -109,20 +105,20 @@ class SimResourceBenchmarks { @Benchmark fun benchmarkSwitchExclusiveSingleConsumer(state: Workload) { - return scope.runBlockingTest { + return scope.runBlockingSimulation { val switch = SimResourceSwitchExclusive() switch.addInput(SimResourceSource(3000.0, clock, scheduler)) switch.addInput(SimResourceSource(3000.0, clock, scheduler)) val provider = switch.addOutput(3500.0) - return@runBlockingTest provider.consume(state.consumers[0]) + return@runBlockingSimulation provider.consume(state.consumers[0]) } } @Benchmark fun benchmarkSwitchExclusiveTripleConsumer(state: Workload) { - return scope.runBlockingTest { + return scope.runBlockingSimulation { val switch = SimResourceSwitchExclusive() switch.addInput(SimResourceSource(3000.0, clock, scheduler)) diff --git a/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceAggregatorMaxMinTest.kt b/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceAggregatorMaxMinTest.kt index cc85d5d9..e272abb8 100644 --- a/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceAggregatorMaxMinTest.kt +++ b/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceAggregatorMaxMinTest.kt @@ -26,12 +26,11 @@ import io.mockk.every import io.mockk.mockk import io.mockk.verify import kotlinx.coroutines.* -import kotlinx.coroutines.test.runBlockingTest import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertAll import org.junit.jupiter.api.assertThrows -import org.opendc.simulator.core.DelayControllerClockAdapter +import org.opendc.simulator.core.runBlockingSimulation import org.opendc.simulator.resources.consumer.SimSpeedConsumerAdapter import org.opendc.simulator.resources.consumer.SimWorkConsumer import org.opendc.utils.TimerScheduler @@ -42,8 +41,7 @@ import org.opendc.utils.TimerScheduler @OptIn(ExperimentalCoroutinesApi::class) internal class SimResourceAggregatorMaxMinTest { @Test - fun testSingleCapacity() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testSingleCapacity() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val aggregator = SimResourceAggregatorMaxMin(clock) @@ -65,7 +63,7 @@ internal class SimResourceAggregatorMaxMinTest { yield() assertAll( - { assertEquals(1000, currentTime) }, + { assertEquals(1000, clock.millis()) }, { assertEquals(listOf(0.0, 0.5, 0.0), usage) } ) } finally { @@ -74,8 +72,7 @@ internal class SimResourceAggregatorMaxMinTest { } @Test - fun testDoubleCapacity() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testDoubleCapacity() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val aggregator = SimResourceAggregatorMaxMin(clock) @@ -93,7 +90,7 @@ internal class SimResourceAggregatorMaxMinTest { aggregator.output.consume(adapter) yield() assertAll( - { assertEquals(1000, currentTime) }, + { assertEquals(1000, clock.millis()) }, { assertEquals(listOf(0.0, 2.0, 0.0), usage) } ) } finally { @@ -102,8 +99,7 @@ internal class SimResourceAggregatorMaxMinTest { } @Test - fun testOvercommit() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testOvercommit() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val aggregator = SimResourceAggregatorMaxMin(clock) @@ -121,7 +117,7 @@ internal class SimResourceAggregatorMaxMinTest { try { aggregator.output.consume(consumer) yield() - assertEquals(1000, currentTime) + assertEquals(1000, clock.millis()) verify(exactly = 2) { consumer.onNext(any()) } } finally { @@ -130,8 +126,7 @@ internal class SimResourceAggregatorMaxMinTest { } @Test - fun testException() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testException() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val aggregator = SimResourceAggregatorMaxMin(clock) @@ -156,8 +151,7 @@ internal class SimResourceAggregatorMaxMinTest { } @Test - fun testAdjustCapacity() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testAdjustCapacity() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val aggregator = SimResourceAggregatorMaxMin(clock) @@ -175,15 +169,14 @@ internal class SimResourceAggregatorMaxMinTest { sources[0].capacity = 0.5 } yield() - assertEquals(2334, currentTime) + assertEquals(2334, clock.millis()) } finally { aggregator.output.close() } } @Test - fun testFailOverCapacity() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testFailOverCapacity() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val aggregator = SimResourceAggregatorMaxMin(clock) @@ -201,7 +194,7 @@ internal class SimResourceAggregatorMaxMinTest { sources[0].capacity = 0.5 } yield() - assertEquals(1000, currentTime) + assertEquals(1000, clock.millis()) } finally { aggregator.output.close() } diff --git a/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceContextTest.kt b/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceContextTest.kt index c8c1a9d7..be909556 100644 --- a/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceContextTest.kt +++ b/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceContextTest.kt @@ -24,9 +24,8 @@ package org.opendc.simulator.resources import io.mockk.* import kotlinx.coroutines.* -import kotlinx.coroutines.test.runBlockingTest import org.junit.jupiter.api.* -import org.opendc.simulator.core.DelayControllerClockAdapter +import org.opendc.simulator.core.runBlockingSimulation /** * A test suite for the [SimAbstractResourceContext] class. @@ -34,9 +33,7 @@ import org.opendc.simulator.core.DelayControllerClockAdapter @OptIn(ExperimentalCoroutinesApi::class) class SimResourceContextTest { @Test - fun testFlushWithoutCommand() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) - + fun testFlushWithoutCommand() = runBlockingSimulation { val consumer = mockk<SimResourceConsumer>(relaxUnitFun = true) every { consumer.onNext(any()) } returns SimResourceCommand.Consume(10.0, 1.0) andThen SimResourceCommand.Exit @@ -50,9 +47,7 @@ class SimResourceContextTest { } @Test - fun testIntermediateFlush() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) - + fun testIntermediateFlush() = runBlockingSimulation { val consumer = mockk<SimResourceConsumer>(relaxUnitFun = true) every { consumer.onNext(any()) } returns SimResourceCommand.Consume(10.0, 1.0) andThen SimResourceCommand.Exit @@ -70,9 +65,7 @@ class SimResourceContextTest { } @Test - fun testIntermediateFlushIdle() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) - + fun testIntermediateFlushIdle() = runBlockingSimulation { val consumer = mockk<SimResourceConsumer>(relaxUnitFun = true) every { consumer.onNext(any()) } returns SimResourceCommand.Idle(10) andThen SimResourceCommand.Exit @@ -95,9 +88,7 @@ class SimResourceContextTest { } @Test - fun testDoubleStart() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) - + fun testDoubleStart() = runBlockingSimulation { val consumer = mockk<SimResourceConsumer>(relaxUnitFun = true) every { consumer.onNext(any()) } returns SimResourceCommand.Idle(10) andThen SimResourceCommand.Exit diff --git a/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceSourceTest.kt b/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceSourceTest.kt index 06b1b87b..39f74481 100644 --- a/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceSourceTest.kt +++ b/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceSourceTest.kt @@ -27,10 +27,9 @@ import io.mockk.mockk import io.mockk.spyk import io.mockk.verify import kotlinx.coroutines.* -import kotlinx.coroutines.test.runBlockingTest import org.junit.jupiter.api.* import org.junit.jupiter.api.Assertions.assertEquals -import org.opendc.simulator.core.DelayControllerClockAdapter +import org.opendc.simulator.core.runBlockingSimulation import org.opendc.simulator.resources.consumer.SimSpeedConsumerAdapter import org.opendc.simulator.resources.consumer.SimWorkConsumer import org.opendc.utils.TimerScheduler @@ -41,8 +40,7 @@ import org.opendc.utils.TimerScheduler @OptIn(ExperimentalCoroutinesApi::class) class SimResourceSourceTest { @Test - fun testSpeed() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testSpeed() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val capacity = 4200.0 val provider = SimResourceSource(capacity, clock, scheduler) @@ -66,8 +64,7 @@ class SimResourceSourceTest { } @Test - fun testAdjustCapacity() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testAdjustCapacity() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val provider = SimResourceSource(1.0, clock, scheduler) @@ -79,7 +76,7 @@ class SimResourceSourceTest { delay(1000) provider.capacity = 0.5 } - assertEquals(3000, currentTime) + assertEquals(3000, clock.millis()) verify(exactly = 1) { consumer.onCapacityChanged(any(), true) } } finally { scheduler.close() @@ -88,8 +85,7 @@ class SimResourceSourceTest { } @Test - fun testSpeedLimit() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testSpeedLimit() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val capacity = 4200.0 val provider = SimResourceSource(capacity, clock, scheduler) @@ -117,8 +113,7 @@ class SimResourceSourceTest { * [SimResourceConsumer.onNext]. */ @Test - fun testIntermediateInterrupt() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testIntermediateInterrupt() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val capacity = 4200.0 val provider = SimResourceSource(capacity, clock, scheduler) @@ -142,8 +137,7 @@ class SimResourceSourceTest { } @Test - fun testInterrupt() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testInterrupt() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val capacity = 4200.0 val provider = SimResourceSource(capacity, clock, scheduler) @@ -173,7 +167,7 @@ class SimResourceSourceTest { } provider.consume(consumer) - assertEquals(0, currentTime) + assertEquals(0, clock.millis()) } finally { scheduler.close() provider.close() @@ -181,8 +175,7 @@ class SimResourceSourceTest { } @Test - fun testFailure() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testFailure() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val capacity = 4200.0 val provider = SimResourceSource(capacity, clock, scheduler) @@ -202,8 +195,7 @@ class SimResourceSourceTest { } @Test - fun testExceptionPropagationOnNext() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testExceptionPropagationOnNext() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val capacity = 4200.0 val provider = SimResourceSource(capacity, clock, scheduler) @@ -224,8 +216,7 @@ class SimResourceSourceTest { } @Test - fun testConcurrentConsumption() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testConcurrentConsumption() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val capacity = 4200.0 val provider = SimResourceSource(capacity, clock, scheduler) @@ -249,8 +240,7 @@ class SimResourceSourceTest { } @Test - fun testClosedConsumption() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testClosedConsumption() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val capacity = 4200.0 val provider = SimResourceSource(capacity, clock, scheduler) @@ -272,8 +262,7 @@ class SimResourceSourceTest { } @Test - fun testCloseDuringConsumption() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testCloseDuringConsumption() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val capacity = 4200.0 val provider = SimResourceSource(capacity, clock, scheduler) @@ -288,7 +277,7 @@ class SimResourceSourceTest { delay(500) provider.close() - assertEquals(500, currentTime) + assertEquals(500, clock.millis()) } finally { scheduler.close() provider.close() @@ -296,8 +285,7 @@ class SimResourceSourceTest { } @Test - fun testIdle() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testIdle() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val capacity = 4200.0 val provider = SimResourceSource(capacity, clock, scheduler) @@ -310,7 +298,7 @@ class SimResourceSourceTest { try { provider.consume(consumer) - assertEquals(500, currentTime) + assertEquals(500, clock.millis()) } finally { scheduler.close() provider.close() @@ -320,8 +308,7 @@ class SimResourceSourceTest { @Test fun testInfiniteSleep() { assertThrows<IllegalStateException> { - runBlockingTest { - val clock = DelayControllerClockAdapter(this) + runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val capacity = 4200.0 val provider = SimResourceSource(capacity, clock, scheduler) @@ -342,8 +329,7 @@ class SimResourceSourceTest { } @Test - fun testIncorrectDeadline() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testIncorrectDeadline() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val capacity = 4200.0 val provider = SimResourceSource(capacity, clock, scheduler) diff --git a/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceSwitchExclusiveTest.kt b/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceSwitchExclusiveTest.kt index 3acad3f7..f7d17867 100644 --- a/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceSwitchExclusiveTest.kt +++ b/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceSwitchExclusiveTest.kt @@ -25,13 +25,12 @@ package org.opendc.simulator.resources import io.mockk.every import io.mockk.mockk import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.test.runBlockingTest import kotlinx.coroutines.yield import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertAll import org.junit.jupiter.api.assertThrows -import org.opendc.simulator.core.DelayControllerClockAdapter +import org.opendc.simulator.core.runBlockingSimulation import org.opendc.simulator.resources.consumer.SimSpeedConsumerAdapter import org.opendc.simulator.resources.consumer.SimTraceConsumer import org.opendc.utils.TimerScheduler @@ -45,8 +44,7 @@ internal class SimResourceSwitchExclusiveTest { * Test a trace workload. */ @Test - fun testTrace() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testTrace() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val speed = mutableListOf<Double>() @@ -80,7 +78,7 @@ internal class SimResourceSwitchExclusiveTest { assertAll( { assertEquals(listOf(0.0, 28.0, 3200.0, 0.0, 183.0, 0.0), speed) { "Correct speed" } }, - { assertEquals(5 * 60L * 4000, currentTime) { "Took enough time" } } + { assertEquals(5 * 60L * 4000, clock.millis()) { "Took enough time" } } ) } @@ -88,8 +86,7 @@ internal class SimResourceSwitchExclusiveTest { * Test runtime workload on hypervisor. */ @Test - fun testRuntimeWorkload() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testRuntimeWorkload() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val duration = 5 * 60L * 1000 @@ -109,15 +106,14 @@ internal class SimResourceSwitchExclusiveTest { } finally { provider.close() } - assertEquals(duration, currentTime) { "Took enough time" } + assertEquals(duration, clock.millis()) { "Took enough time" } } /** * Test two workloads running sequentially. */ @Test - fun testTwoWorkloads() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testTwoWorkloads() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val duration = 5 * 60L * 1000 @@ -152,15 +148,14 @@ internal class SimResourceSwitchExclusiveTest { } finally { provider.close() } - assertEquals(duration * 2, currentTime) { "Took enough time" } + assertEquals(duration * 2, clock.millis()) { "Took enough time" } } /** * Test concurrent workloads on the machine. */ @Test - fun testConcurrentWorkloadFails() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testConcurrentWorkloadFails() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val duration = 5 * 60L * 1000 diff --git a/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceSwitchMaxMinTest.kt b/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceSwitchMaxMinTest.kt index 59e80e56..7416f277 100644 --- a/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceSwitchMaxMinTest.kt +++ b/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceSwitchMaxMinTest.kt @@ -27,11 +27,10 @@ import io.mockk.mockk import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.launch -import kotlinx.coroutines.test.runBlockingTest import kotlinx.coroutines.yield import org.junit.jupiter.api.* import org.junit.jupiter.api.Assertions.assertEquals -import org.opendc.simulator.core.DelayControllerClockAdapter +import org.opendc.simulator.core.runBlockingSimulation import org.opendc.simulator.resources.consumer.SimTraceConsumer import org.opendc.utils.TimerScheduler @@ -41,8 +40,7 @@ import org.opendc.utils.TimerScheduler @OptIn(ExperimentalCoroutinesApi::class) internal class SimResourceSwitchMaxMinTest { @Test - fun testSmoke() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testSmoke() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val switch = SimResourceSwitchMaxMin(clock) @@ -67,8 +65,7 @@ internal class SimResourceSwitchMaxMinTest { * Test overcommitting of resources via the hypervisor with a single VM. */ @Test - fun testOvercommittedSingle() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testOvercommittedSingle() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val listener = object : SimResourceSwitchMaxMin.Listener { @@ -118,7 +115,7 @@ internal class SimResourceSwitchMaxMinTest { { assertEquals(1113300, listener.totalRequestedWork, "Requested Burst does not match") }, { assertEquals(1023300, listener.totalGrantedWork, "Granted Burst does not match") }, { assertEquals(90000, listener.totalOvercommittedWork, "Overcommissioned Burst does not match") }, - { assertEquals(1200000, currentTime) } + { assertEquals(1200000, clock.millis()) } ) } @@ -126,8 +123,7 @@ internal class SimResourceSwitchMaxMinTest { * Test overcommitting of resources via the hypervisor with two VMs. */ @Test - fun testOvercommittedDual() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testOvercommittedDual() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val listener = object : SimResourceSwitchMaxMin.Listener { @@ -191,7 +187,7 @@ internal class SimResourceSwitchMaxMinTest { { assertEquals(2082000, listener.totalRequestedWork, "Requested Burst does not match") }, { assertEquals(1062000, listener.totalGrantedWork, "Granted Burst does not match") }, { assertEquals(1020000, listener.totalOvercommittedWork, "Overcommissioned Burst does not match") }, - { assertEquals(1200000, currentTime) } + { assertEquals(1200000, clock.millis()) } ) } } diff --git a/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceTransformerTest.kt b/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceTransformerTest.kt index aeaab6af..d2ad73bc 100644 --- a/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceTransformerTest.kt +++ b/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceTransformerTest.kt @@ -27,11 +27,10 @@ import io.mockk.mockk import io.mockk.spyk import io.mockk.verify import kotlinx.coroutines.* -import kotlinx.coroutines.test.runBlockingTest import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows -import org.opendc.simulator.core.DelayControllerClockAdapter +import org.opendc.simulator.core.runBlockingSimulation import org.opendc.simulator.resources.consumer.SimWorkConsumer import org.opendc.utils.TimerScheduler @@ -41,9 +40,8 @@ import org.opendc.utils.TimerScheduler @OptIn(ExperimentalCoroutinesApi::class) internal class SimResourceTransformerTest { @Test - fun testExitImmediately() = runBlockingTest { + fun testExitImmediately() = runBlockingSimulation { val forwarder = SimResourceForwarder() - val clock = DelayControllerClockAdapter(this) val scheduler = TimerScheduler<Any>(coroutineContext, clock) val source = SimResourceSource(2000.0, clock, scheduler) @@ -63,9 +61,8 @@ internal class SimResourceTransformerTest { } @Test - fun testExit() = runBlockingTest { + fun testExit() = runBlockingSimulation { val forwarder = SimResourceForwarder() - val clock = DelayControllerClockAdapter(this) val scheduler = TimerScheduler<Any>(coroutineContext, clock) val source = SimResourceSource(2000.0, clock, scheduler) @@ -91,7 +88,7 @@ internal class SimResourceTransformerTest { } @Test - fun testState() = runBlockingTest { + fun testState() = runBlockingSimulation { val forwarder = SimResourceForwarder() val consumer = object : SimResourceConsumer { override fun onNext(ctx: SimResourceContext): SimResourceCommand = SimResourceCommand.Exit @@ -112,7 +109,7 @@ internal class SimResourceTransformerTest { } @Test - fun testCancelPendingDelegate() = runBlockingTest { + fun testCancelPendingDelegate() = runBlockingSimulation { val forwarder = SimResourceForwarder() val consumer = mockk<SimResourceConsumer>(relaxUnitFun = true) @@ -125,9 +122,8 @@ internal class SimResourceTransformerTest { } @Test - fun testCancelStartedDelegate() = runBlockingTest { + fun testCancelStartedDelegate() = runBlockingSimulation { val forwarder = SimResourceForwarder() - val clock = DelayControllerClockAdapter(this) val scheduler = TimerScheduler<Any>(coroutineContext, clock) val source = SimResourceSource(2000.0, clock, scheduler) @@ -145,9 +141,8 @@ internal class SimResourceTransformerTest { } @Test - fun testCancelPropagation() = runBlockingTest { + fun testCancelPropagation() = runBlockingSimulation { val forwarder = SimResourceForwarder() - val clock = DelayControllerClockAdapter(this) val scheduler = TimerScheduler<Any>(coroutineContext, clock) val source = SimResourceSource(2000.0, clock, scheduler) @@ -165,9 +160,8 @@ internal class SimResourceTransformerTest { } @Test - fun testExitPropagation() = runBlockingTest { + fun testExitPropagation() = runBlockingSimulation { val forwarder = SimResourceForwarder(isCoupled = true) - val clock = DelayControllerClockAdapter(this) val scheduler = TimerScheduler<Any>(coroutineContext, clock) val source = SimResourceSource(2000.0, clock, scheduler) @@ -182,9 +176,8 @@ internal class SimResourceTransformerTest { } @Test - fun testAdjustCapacity() = runBlockingTest { + fun testAdjustCapacity() = runBlockingSimulation { val forwarder = SimResourceForwarder() - val clock = DelayControllerClockAdapter(this) val scheduler = TimerScheduler<Any>(coroutineContext, clock) val source = SimResourceSource(1.0, clock, scheduler) @@ -197,14 +190,13 @@ internal class SimResourceTransformerTest { source.capacity = 0.5 } - assertEquals(3000, currentTime) + assertEquals(3000, clock.millis()) verify(exactly = 1) { consumer.onCapacityChanged(any(), true) } } @Test - fun testTransformExit() = runBlockingTest { + fun testTransformExit() = runBlockingSimulation { val forwarder = SimResourceTransformer { _, _ -> SimResourceCommand.Exit } - val clock = DelayControllerClockAdapter(this) val scheduler = TimerScheduler<Any>(coroutineContext, clock) val source = SimResourceSource(1.0, clock, scheduler) @@ -212,7 +204,7 @@ internal class SimResourceTransformerTest { source.startConsumer(forwarder) forwarder.consume(consumer) - assertEquals(0, currentTime) + assertEquals(0, clock.millis()) verify(exactly = 1) { consumer.onNext(any()) } } } diff --git a/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimWorkConsumerTest.kt b/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimWorkConsumerTest.kt index 38dc0d48..bf58b1b6 100644 --- a/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimWorkConsumerTest.kt +++ b/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimWorkConsumerTest.kt @@ -23,10 +23,9 @@ package org.opendc.simulator.resources import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.test.runBlockingTest import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test -import org.opendc.simulator.core.DelayControllerClockAdapter +import org.opendc.simulator.core.runBlockingSimulation import org.opendc.simulator.resources.consumer.SimWorkConsumer import org.opendc.utils.TimerScheduler @@ -36,8 +35,7 @@ import org.opendc.utils.TimerScheduler @OptIn(ExperimentalCoroutinesApi::class) internal class SimWorkConsumerTest { @Test - fun testSmoke() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testSmoke() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val provider = SimResourceSource(1.0, clock, scheduler) @@ -45,15 +43,14 @@ internal class SimWorkConsumerTest { try { provider.consume(consumer) - assertEquals(1000, currentTime) + assertEquals(1000, clock.millis()) } finally { provider.close() } } @Test - fun testUtilization() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testUtilization() = runBlockingSimulation { val scheduler = TimerScheduler<Any>(coroutineContext, clock) val provider = SimResourceSource(1.0, clock, scheduler) @@ -61,7 +58,7 @@ internal class SimWorkConsumerTest { try { provider.consume(consumer) - assertEquals(2000, currentTime) + assertEquals(2000, clock.millis()) } finally { provider.close() } |
