From f616b720406250b1415593ff04c9d910b1fda54c Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Mon, 22 Mar 2021 12:13:23 +0100 Subject: simulator: Expose capacity and remaining work outside consumer callback This change changes the consumer and context interfaces to expose the provider capacity and remaining work via the context instance as opposed to only via the callback. This simplifies aggregation of resources. --- .../simulator/resources/SimResourceContextTest.kt | 8 ++++---- .../resources/SimResourceForwarderTest.kt | 18 ++++++---------- .../simulator/resources/SimResourceSourceTest.kt | 24 +++++++++++----------- .../resources/SimResourceSwitchExclusiveTest.kt | 8 +++----- .../resources/SimResourceSwitchMaxMinTest.kt | 2 +- 5 files changed, 26 insertions(+), 34 deletions(-) (limited to 'simulator/opendc-simulator/opendc-simulator-resources/src/test') 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 0bc87473..5d4eb46d 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 @@ -45,7 +45,7 @@ class SimResourceContextTest { val resource = SimCpu(4200.0) val consumer = mockk>(relaxUnitFun = true) - every { consumer.onNext(any(), any(), any()) } returns SimResourceCommand.Consume(10.0, 1.0) andThen SimResourceCommand.Exit + every { consumer.onNext(any()) } returns SimResourceCommand.Consume(10.0, 1.0) andThen SimResourceCommand.Exit val context = object : SimAbstractResourceContext(resource, clock, consumer) { override fun onIdle(deadline: Long) {} @@ -64,7 +64,7 @@ class SimResourceContextTest { val resource = SimCpu(4200.0) val consumer = mockk>(relaxUnitFun = true) - every { consumer.onNext(any(), any(), any()) } returns SimResourceCommand.Consume(10.0, 1.0) andThen SimResourceCommand.Exit + every { consumer.onNext(any()) } returns SimResourceCommand.Consume(10.0, 1.0) andThen SimResourceCommand.Exit val context = spyk(object : SimAbstractResourceContext(resource, clock, consumer) { override fun onIdle(deadline: Long) {} @@ -85,7 +85,7 @@ class SimResourceContextTest { val resource = SimCpu(4200.0) val consumer = mockk>(relaxUnitFun = true) - every { consumer.onNext(any(), any(), any()) } returns SimResourceCommand.Idle(10) andThen SimResourceCommand.Exit + every { consumer.onNext(any()) } returns SimResourceCommand.Idle(10) andThen SimResourceCommand.Exit val context = spyk(object : SimAbstractResourceContext(resource, clock, consumer) { override fun onIdle(deadline: Long) {} @@ -111,7 +111,7 @@ class SimResourceContextTest { val resource = SimCpu(4200.0) val consumer = mockk>(relaxUnitFun = true) - every { consumer.onNext(any(), any(), any()) } returns SimResourceCommand.Idle(10) andThen SimResourceCommand.Exit + every { consumer.onNext(any()) } returns SimResourceCommand.Idle(10) andThen SimResourceCommand.Exit val context = object : SimAbstractResourceContext(resource, clock, consumer) { override fun onIdle(deadline: Long) {} diff --git a/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceForwarderTest.kt b/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceForwarderTest.kt index b1b959ba..47794bdd 100644 --- a/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceForwarderTest.kt +++ b/simulator/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceForwarderTest.kt @@ -60,9 +60,7 @@ internal class SimResourceForwarderTest { forwarder.consume(object : SimResourceConsumer { override fun onNext( - ctx: SimResourceContext, - capacity: Double, - remainingWork: Double + ctx: SimResourceContext ): SimResourceCommand { return SimResourceCommand.Exit } @@ -88,9 +86,7 @@ internal class SimResourceForwarderTest { var isFirst = true override fun onNext( - ctx: SimResourceContext, - capacity: Double, - remainingWork: Double + ctx: SimResourceContext ): SimResourceCommand { return if (isFirst) { isFirst = false @@ -109,9 +105,7 @@ internal class SimResourceForwarderTest { val forwarder = SimResourceForwarder(SimCpu(1000.0)) val consumer = object : SimResourceConsumer { override fun onNext( - ctx: SimResourceContext, - capacity: Double, - remainingWork: Double + ctx: SimResourceContext ): SimResourceCommand = SimResourceCommand.Exit } @@ -134,7 +128,7 @@ internal class SimResourceForwarderTest { val forwarder = SimResourceForwarder(SimCpu(1000.0)) val consumer = mockk>(relaxUnitFun = true) - every { consumer.onNext(any(), any(), any()) } returns SimResourceCommand.Exit + every { consumer.onNext(any()) } returns SimResourceCommand.Exit forwarder.startConsumer(consumer) forwarder.cancel() @@ -150,7 +144,7 @@ internal class SimResourceForwarderTest { val source = SimResourceSource(SimCpu(2000.0), clock, scheduler) val consumer = mockk>(relaxUnitFun = true) - every { consumer.onNext(any(), any(), any()) } returns SimResourceCommand.Idle(10) + every { consumer.onNext(any()) } returns SimResourceCommand.Idle(10) source.startConsumer(forwarder) yield() @@ -170,7 +164,7 @@ internal class SimResourceForwarderTest { val source = SimResourceSource(SimCpu(2000.0), clock, scheduler) val consumer = mockk>(relaxUnitFun = true) - every { consumer.onNext(any(), any(), any()) } returns SimResourceCommand.Idle(10) + every { consumer.onNext(any()) } returns SimResourceCommand.Idle(10) source.startConsumer(forwarder) yield() 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 18f18ded..c0ed8c9e 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 @@ -49,7 +49,7 @@ class SimResourceSourceTest { val provider = SimResourceSource(SimCpu(4200.0), clock, scheduler) val consumer = mockk>(relaxUnitFun = true) - every { consumer.onNext(any(), any(), any()) } + every { consumer.onNext(any()) } .returns(SimResourceCommand.Consume(1000 * provider.resource.speed, provider.resource.speed)) .andThen(SimResourceCommand.Exit) @@ -74,7 +74,7 @@ class SimResourceSourceTest { val provider = SimResourceSource(SimCpu(4200.0), clock, scheduler) val consumer = mockk>(relaxUnitFun = true) - every { consumer.onNext(any(), any(), any()) } + every { consumer.onNext(any()) } .returns(SimResourceCommand.Consume(1000 * provider.resource.speed, 2 * provider.resource.speed)) .andThen(SimResourceCommand.Exit) @@ -107,7 +107,7 @@ class SimResourceSourceTest { ctx.interrupt() } - override fun onNext(ctx: SimResourceContext, capacity: Double, remainingWork: Double): SimResourceCommand { + override fun onNext(ctx: SimResourceContext): SimResourceCommand { return SimResourceCommand.Exit } } @@ -133,8 +133,8 @@ class SimResourceSourceTest { resCtx = ctx } - override fun onNext(ctx: SimResourceContext, capacity: Double, remainingWork: Double): SimResourceCommand { - assertEquals(0.0, remainingWork) + override fun onNext(ctx: SimResourceContext): SimResourceCommand { + assertEquals(0.0, ctx.remainingWork) return if (isFirst) { isFirst = false SimResourceCommand.Consume(4.0, 1.0) @@ -185,7 +185,7 @@ class SimResourceSourceTest { val provider = SimResourceSource(SimCpu(4200.0), clock, scheduler) val consumer = mockk>(relaxUnitFun = true) - every { consumer.onNext(any(), any(), any()) } + every { consumer.onNext(any()) } .returns(SimResourceCommand.Consume(1.0, 1.0)) .andThenThrows(IllegalStateException()) @@ -206,7 +206,7 @@ class SimResourceSourceTest { val provider = SimResourceSource(SimCpu(4200.0), clock, scheduler) val consumer = mockk>(relaxUnitFun = true) - every { consumer.onNext(any(), any(), any()) } + every { consumer.onNext(any()) } .returns(SimResourceCommand.Consume(1.0, 1.0)) .andThenThrows(IllegalStateException()) @@ -230,7 +230,7 @@ class SimResourceSourceTest { val provider = SimResourceSource(SimCpu(4200.0), clock, scheduler) val consumer = mockk>(relaxUnitFun = true) - every { consumer.onNext(any(), any(), any()) } + every { consumer.onNext(any()) } .returns(SimResourceCommand.Consume(1.0, 1.0)) .andThenThrows(IllegalStateException()) @@ -252,7 +252,7 @@ class SimResourceSourceTest { val provider = SimResourceSource(SimCpu(4200.0), clock, scheduler) val consumer = mockk>(relaxUnitFun = true) - every { consumer.onNext(any(), any(), any()) } + every { consumer.onNext(any()) } .returns(SimResourceCommand.Consume(1.0, 1.0)) .andThenThrows(IllegalStateException()) @@ -275,7 +275,7 @@ class SimResourceSourceTest { val provider = SimResourceSource(SimCpu(4200.0), clock, scheduler) val consumer = mockk>(relaxUnitFun = true) - every { consumer.onNext(any(), any(), any()) } + every { consumer.onNext(any()) } .returns(SimResourceCommand.Idle(clock.millis() + 500)) .andThen(SimResourceCommand.Exit) @@ -298,7 +298,7 @@ class SimResourceSourceTest { val provider = SimResourceSource(SimCpu(4200.0), clock, scheduler) val consumer = mockk>(relaxUnitFun = true) - every { consumer.onNext(any(), any(), any()) } + every { consumer.onNext(any()) } .returns(SimResourceCommand.Idle()) .andThenThrows(IllegalStateException()) @@ -319,7 +319,7 @@ class SimResourceSourceTest { val provider = SimResourceSource(SimCpu(4200.0), clock, scheduler) val consumer = mockk>(relaxUnitFun = true) - every { consumer.onNext(any(), any(), any()) } + every { consumer.onNext(any()) } .returns(SimResourceCommand.Idle(2)) .andThen(SimResourceCommand.Exit) 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 354dab93..dc90a43e 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 @@ -100,7 +100,7 @@ internal class SimResourceSwitchExclusiveTest { val duration = 5 * 60L * 1000 val workload = mockk>(relaxUnitFun = true) - every { workload.onNext(any(), any(), any()) } returns SimResourceCommand.Consume(duration / 1000.0, 1.0) andThen SimResourceCommand.Exit + every { workload.onNext(any()) } returns SimResourceCommand.Consume(duration / 1000.0, 1.0) andThen SimResourceCommand.Exit val switch = SimResourceSwitchExclusive() val source = SimResourceSource(SimCpu(3200.0), clock, scheduler) @@ -135,9 +135,7 @@ internal class SimResourceSwitchExclusiveTest { } override fun onNext( - ctx: SimResourceContext, - capacity: Double, - remainingWork: Double + ctx: SimResourceContext ): SimResourceCommand { return if (isFirst) { isFirst = false @@ -175,7 +173,7 @@ internal class SimResourceSwitchExclusiveTest { val duration = 5 * 60L * 1000 val workload = mockk>(relaxUnitFun = true) - every { workload.onNext(any(), any(), any()) } returns SimResourceCommand.Consume(duration / 1000.0, 1.0) andThen SimResourceCommand.Exit + every { workload.onNext(any()) } returns SimResourceCommand.Consume(duration / 1000.0, 1.0) andThen SimResourceCommand.Exit val switch = SimResourceSwitchExclusive() val source = SimResourceSource(SimCpu(3200.0), clock, scheduler) 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 e8f5a13c..8b989334 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 @@ -57,7 +57,7 @@ internal class SimResourceSwitchMaxMinTest { val provider = switch.addOutput(SimCpu(1000.0)) val consumer = mockk>(relaxUnitFun = true) - every { consumer.onNext(any(), any(), any()) } returns SimResourceCommand.Consume(1.0, 1.0) andThen SimResourceCommand.Exit + every { consumer.onNext(any()) } returns SimResourceCommand.Consume(1.0, 1.0) andThen SimResourceCommand.Exit try { provider.consume(consumer) -- cgit v1.2.3