From 62678b2890a7f3640836b99ca2fec9efd7485929 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Wed, 21 Apr 2021 16:35:52 +0200 Subject: simulator: Migrate to SimulationCoroutineDispatcher This change migrates the remainder of the codebase to the SimulationCoroutineDispatcher implementation. --- .../opendc/compute/service/ComputeServiceTest.kt | 47 +++++++++------------- .../opendc/compute/service/InternalServerTest.kt | 26 ++++++------ .../org/opendc/compute/simulator/SimHostTest.kt | 8 ++-- 3 files changed, 36 insertions(+), 45 deletions(-) (limited to 'simulator/opendc-compute') diff --git a/simulator/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ComputeServiceTest.kt b/simulator/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ComputeServiceTest.kt index 059f9cd9..a6258845 100644 --- a/simulator/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ComputeServiceTest.kt +++ b/simulator/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ComputeServiceTest.kt @@ -26,9 +26,6 @@ import io.mockk.* import io.opentelemetry.api.metrics.MeterProvider import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.delay -import kotlinx.coroutines.test.TestCoroutineScope -import kotlinx.coroutines.test.runBlockingTest -import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertNull import org.junit.jupiter.api.BeforeEach @@ -43,7 +40,8 @@ import org.opendc.compute.service.scheduler.FilterScheduler import org.opendc.compute.service.scheduler.filters.ComputeCapabilitiesFilter import org.opendc.compute.service.scheduler.filters.ComputeFilter import org.opendc.compute.service.scheduler.weights.MemoryWeigher -import org.opendc.simulator.core.DelayControllerClockAdapter +import org.opendc.simulator.core.SimulationCoroutineScope +import org.opendc.simulator.core.runBlockingSimulation import java.util.* /** @@ -51,13 +49,13 @@ import java.util.* */ @OptIn(ExperimentalCoroutinesApi::class) internal class ComputeServiceTest { - lateinit var scope: TestCoroutineScope + lateinit var scope: SimulationCoroutineScope lateinit var service: ComputeService @BeforeEach fun setUp() { - scope = TestCoroutineScope() - val clock = DelayControllerClockAdapter(scope) + scope = SimulationCoroutineScope() + val clock = scope.clock val computeScheduler = FilterScheduler( filters = listOf(ComputeFilter(), ComputeCapabilitiesFilter()), weighers = listOf(MemoryWeigher() to -1.0) @@ -66,13 +64,8 @@ internal class ComputeServiceTest { service = ComputeService(scope.coroutineContext, clock, meter, computeScheduler) } - @AfterEach - fun tearDown() { - scope.cleanupTestCoroutines() - } - @Test - fun testClientClose() = scope.runBlockingTest { + fun testClientClose() = scope.runBlockingSimulation { val client = service.newClient() assertEquals(emptyList(), client.queryFlavors()) @@ -95,7 +88,7 @@ internal class ComputeServiceTest { } @Test - fun testClientCreate() = scope.runBlockingTest { + fun testClientCreate() = scope.runBlockingSimulation { val client = service.newClient() val flavor = client.newFlavor("test", 1, 1024) @@ -121,7 +114,7 @@ internal class ComputeServiceTest { } @Test - fun testClientOnClose() = scope.runBlockingTest { + fun testClientOnClose() = scope.runBlockingSimulation { service.close() assertThrows { service.newClient() @@ -129,7 +122,7 @@ internal class ComputeServiceTest { } @Test - fun testAddHost() = scope.runBlockingTest { + fun testAddHost() = scope.runBlockingSimulation { val host = mockk(relaxUnitFun = true) every { host.model } returns HostModel(4, 2048) @@ -151,7 +144,7 @@ internal class ComputeServiceTest { } @Test - fun testAddHostDouble() = scope.runBlockingTest { + fun testAddHostDouble() = scope.runBlockingSimulation { val host = mockk(relaxUnitFun = true) every { host.model } returns HostModel(4, 2048) @@ -167,7 +160,7 @@ internal class ComputeServiceTest { } @Test - fun testServerStartWithoutEnoughCpus() = scope.runBlockingTest { + fun testServerStartWithoutEnoughCpus() = scope.runBlockingSimulation { val client = service.newClient() val flavor = client.newFlavor("test", 1, 0) val image = client.newImage("test") @@ -180,7 +173,7 @@ internal class ComputeServiceTest { } @Test - fun testServerStartWithoutEnoughMemory() = scope.runBlockingTest { + fun testServerStartWithoutEnoughMemory() = scope.runBlockingSimulation { val client = service.newClient() val flavor = client.newFlavor("test", 0, 1024) val image = client.newImage("test") @@ -193,7 +186,7 @@ internal class ComputeServiceTest { } @Test - fun testServerStartWithoutEnoughResources() = scope.runBlockingTest { + fun testServerStartWithoutEnoughResources() = scope.runBlockingSimulation { val client = service.newClient() val flavor = client.newFlavor("test", 1, 1024) val image = client.newImage("test") @@ -206,7 +199,7 @@ internal class ComputeServiceTest { } @Test - fun testServerCancelRequest() = scope.runBlockingTest { + fun testServerCancelRequest() = scope.runBlockingSimulation { val client = service.newClient() val flavor = client.newFlavor("test", 1, 1024) val image = client.newImage("test") @@ -220,7 +213,7 @@ internal class ComputeServiceTest { } @Test - fun testServerCannotFitOnHost() = scope.runBlockingTest { + fun testServerCannotFitOnHost() = scope.runBlockingSimulation { val host = mockk(relaxUnitFun = true) every { host.model } returns HostModel(4, 2048) @@ -243,7 +236,7 @@ internal class ComputeServiceTest { } @Test - fun testHostAvailableAfterSomeTime() = scope.runBlockingTest { + fun testHostAvailableAfterSomeTime() = scope.runBlockingSimulation { val host = mockk(relaxUnitFun = true) val listeners = mutableListOf() @@ -274,7 +267,7 @@ internal class ComputeServiceTest { } @Test - fun testHostUnavailableAfterSomeTime() = scope.runBlockingTest { + fun testHostUnavailableAfterSomeTime() = scope.runBlockingSimulation { val host = mockk(relaxUnitFun = true) val listeners = mutableListOf() @@ -305,7 +298,7 @@ internal class ComputeServiceTest { } @Test - fun testServerInvalidType() = scope.runBlockingTest { + fun testServerInvalidType() = scope.runBlockingSimulation { val host = mockk(relaxUnitFun = true) val listeners = mutableListOf() @@ -328,7 +321,7 @@ internal class ComputeServiceTest { } @Test - fun testServerDeploy() = scope.runBlockingTest { + fun testServerDeploy() = scope.runBlockingSimulation { val host = mockk(relaxUnitFun = true) val listeners = mutableListOf() @@ -371,7 +364,7 @@ internal class ComputeServiceTest { } @Test - fun testServerDeployFailure() = scope.runBlockingTest { + fun testServerDeployFailure() = scope.runBlockingSimulation { val host = mockk(relaxUnitFun = true) val listeners = mutableListOf() diff --git a/simulator/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/InternalServerTest.kt b/simulator/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/InternalServerTest.kt index 81cb45df..20ea8d20 100644 --- a/simulator/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/InternalServerTest.kt +++ b/simulator/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/InternalServerTest.kt @@ -24,7 +24,6 @@ package org.opendc.compute.service import io.mockk.* import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.test.runBlockingTest import kotlinx.coroutines.yield import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.Test @@ -36,6 +35,7 @@ import org.opendc.compute.service.internal.ComputeServiceImpl import org.opendc.compute.service.internal.InternalFlavor import org.opendc.compute.service.internal.InternalImage import org.opendc.compute.service.internal.InternalServer +import org.opendc.simulator.core.runBlockingSimulation import java.util.* /** @@ -95,7 +95,7 @@ class InternalServerTest { } @Test - fun testStartTerminatedServer() = runBlockingTest { + fun testStartTerminatedServer() = runBlockingSimulation { val service = mockk() val uid = UUID.randomUUID() val flavor = mockk() @@ -111,7 +111,7 @@ class InternalServerTest { } @Test - fun testStartDeletedServer() = runBlockingTest { + fun testStartDeletedServer() = runBlockingSimulation { val service = mockk() val uid = UUID.randomUUID() val flavor = mockk() @@ -124,7 +124,7 @@ class InternalServerTest { } @Test - fun testStartProvisioningServer() = runBlockingTest { + fun testStartProvisioningServer() = runBlockingSimulation { val service = mockk() val uid = UUID.randomUUID() val flavor = mockk() @@ -139,7 +139,7 @@ class InternalServerTest { } @Test - fun testStartRunningServer() = runBlockingTest { + fun testStartRunningServer() = runBlockingSimulation { val service = mockk() val uid = UUID.randomUUID() val flavor = mockk() @@ -154,7 +154,7 @@ class InternalServerTest { } @Test - fun testStopProvisioningServer() = runBlockingTest { + fun testStopProvisioningServer() = runBlockingSimulation { val service = mockk() val uid = UUID.randomUUID() val flavor = mockk() @@ -172,7 +172,7 @@ class InternalServerTest { } @Test - fun testStopTerminatedServer() = runBlockingTest { + fun testStopTerminatedServer() = runBlockingSimulation { val service = mockk() val uid = UUID.randomUUID() val flavor = mockk() @@ -186,7 +186,7 @@ class InternalServerTest { } @Test - fun testStopDeletedServer() = runBlockingTest { + fun testStopDeletedServer() = runBlockingSimulation { val service = mockk() val uid = UUID.randomUUID() val flavor = mockk() @@ -200,7 +200,7 @@ class InternalServerTest { } @Test - fun testStopRunningServer() = runBlockingTest { + fun testStopRunningServer() = runBlockingSimulation { val service = mockk() val uid = UUID.randomUUID() val flavor = mockk() @@ -217,7 +217,7 @@ class InternalServerTest { } @Test - fun testDeleteProvisioningServer() = runBlockingTest { + fun testDeleteProvisioningServer() = runBlockingSimulation { val service = mockk(relaxUnitFun = true) val uid = UUID.randomUUID() val flavor = mockk() @@ -236,7 +236,7 @@ class InternalServerTest { } @Test - fun testDeleteTerminatedServer() = runBlockingTest { + fun testDeleteTerminatedServer() = runBlockingSimulation { val service = mockk(relaxUnitFun = true) val uid = UUID.randomUUID() val flavor = mockk() @@ -252,7 +252,7 @@ class InternalServerTest { } @Test - fun testDeleteDeletedServer() = runBlockingTest { + fun testDeleteDeletedServer() = runBlockingSimulation { val service = mockk(relaxUnitFun = true) val uid = UUID.randomUUID() val flavor = mockk() @@ -266,7 +266,7 @@ class InternalServerTest { } @Test - fun testDeleteRunningServer() = runBlockingTest { + fun testDeleteRunningServer() = runBlockingSimulation { val service = mockk(relaxUnitFun = true) val uid = UUID.randomUUID() val flavor = mockk() diff --git a/simulator/opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/SimHostTest.kt b/simulator/opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/SimHostTest.kt index cdbdd58b..5594fd59 100644 --- a/simulator/opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/SimHostTest.kt +++ b/simulator/opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/SimHostTest.kt @@ -29,7 +29,6 @@ import io.opentelemetry.sdk.metrics.data.MetricData import io.opentelemetry.sdk.metrics.export.MetricExporter import io.opentelemetry.sdk.metrics.export.MetricProducer import kotlinx.coroutines.* -import kotlinx.coroutines.test.runBlockingTest import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test @@ -47,7 +46,7 @@ import org.opendc.simulator.compute.model.MemoryUnit import org.opendc.simulator.compute.model.ProcessingNode import org.opendc.simulator.compute.model.ProcessingUnit import org.opendc.simulator.compute.workload.SimTraceWorkload -import org.opendc.simulator.core.DelayControllerClockAdapter +import org.opendc.simulator.core.runBlockingSimulation import org.opendc.telemetry.sdk.metrics.export.CoroutineMetricReader import org.opendc.telemetry.sdk.toOtelClock import java.util.UUID @@ -74,8 +73,7 @@ internal class SimHostTest { * Test overcommitting of resources by the hypervisor. */ @Test - fun testOvercommitted() = runBlockingTest { - val clock = DelayControllerClockAdapter(this) + fun testOvercommitted() = runBlockingSimulation { var requestedWork = 0L var grantedWork = 0L var overcommittedWork = 0L @@ -165,7 +163,7 @@ internal class SimHostTest { { assertEquals(4197600, requestedWork, "Requested work does not match") }, { assertEquals(2157600, grantedWork, "Granted work does not match") }, { assertEquals(2040000, overcommittedWork, "Overcommitted work does not match") }, - { assertEquals(1500001, currentTime) } + { assertEquals(1500001, clock.millis()) } ) } -- cgit v1.2.3