diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2021-04-22 17:18:59 +0200 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2021-05-03 21:17:48 +0200 |
| commit | 980b016452b3889585feaf2dbbe3244c921123b0 (patch) | |
| tree | 845a418358c656f2917f7cc9127b3141d1ce48b5 /opendc-simulator/opendc-simulator-resources/src/test | |
| parent | 5c8cecaf5b8d24ffcd99ce45b922c5a853bd492d (diff) | |
simulator: Add generic approach for reporting resource events
This change introduces a generic approach for reporting resource events
to resource consumers. This way we reduce the boilerplate of the
SimResourceConsumer interface.
Diffstat (limited to 'opendc-simulator/opendc-simulator-resources/src/test')
4 files changed, 28 insertions, 21 deletions
diff --git a/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceContextTest.kt b/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceContextTest.kt index be909556..8c15ec71 100644 --- a/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceContextTest.kt +++ b/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceContextTest.kt @@ -40,7 +40,7 @@ class SimResourceContextTest { val context = object : SimAbstractResourceContext(4200.0, clock, consumer) { override fun onIdle(deadline: Long) {} override fun onConsume(work: Double, limit: Double, deadline: Long) {} - override fun onFinish(cause: Throwable?) {} + override fun onFinish() {} } context.flush() @@ -53,7 +53,7 @@ class SimResourceContextTest { val context = spyk(object : SimAbstractResourceContext(4200.0, clock, consumer) { override fun onIdle(deadline: Long) {} - override fun onFinish(cause: Throwable?) {} + override fun onFinish() {} override fun onConsume(work: Double, limit: Double, deadline: Long) {} }) @@ -71,7 +71,7 @@ class SimResourceContextTest { val context = spyk(object : SimAbstractResourceContext(4200.0, clock, consumer) { override fun onIdle(deadline: Long) {} - override fun onFinish(cause: Throwable?) {} + override fun onFinish() {} override fun onConsume(work: Double, limit: Double, deadline: Long) {} }) @@ -83,7 +83,7 @@ class SimResourceContextTest { assertAll( { verify(exactly = 2) { context.onIdle(any()) } }, - { verify(exactly = 1) { context.onFinish(null) } } + { verify(exactly = 1) { context.onFinish() } } ) } @@ -94,7 +94,7 @@ class SimResourceContextTest { val context = object : SimAbstractResourceContext(4200.0, clock, consumer) { override fun onIdle(deadline: Long) {} - override fun onFinish(cause: Throwable?) {} + override fun onFinish() {} override fun onConsume(work: Double, limit: Double, deadline: Long) {} } diff --git a/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceSourceTest.kt b/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceSourceTest.kt index 39f74481..361a1516 100644 --- a/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceSourceTest.kt +++ b/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceSourceTest.kt @@ -77,7 +77,7 @@ class SimResourceSourceTest { provider.capacity = 0.5 } assertEquals(3000, clock.millis()) - verify(exactly = 1) { consumer.onCapacityChanged(any(), true) } + verify(exactly = 1) { consumer.onEvent(any(), SimResourceEvent.Capacity) } } finally { scheduler.close() provider.close() @@ -119,13 +119,13 @@ class SimResourceSourceTest { val provider = SimResourceSource(capacity, clock, scheduler) val consumer = object : SimResourceConsumer { - override fun onStart(ctx: SimResourceContext) { - ctx.interrupt() - } - override fun onNext(ctx: SimResourceContext): SimResourceCommand { return SimResourceCommand.Exit } + + override fun onEvent(ctx: SimResourceContext, event: SimResourceEvent) { + ctx.interrupt() + } } try { @@ -145,8 +145,12 @@ class SimResourceSourceTest { val consumer = object : SimResourceConsumer { var isFirst = true - override fun onStart(ctx: SimResourceContext) { - resCtx = ctx + + override fun onEvent(ctx: SimResourceContext, event: SimResourceEvent) { + when (event) { + SimResourceEvent.Start -> resCtx = ctx + else -> {} + } } override fun onNext(ctx: SimResourceContext): SimResourceCommand { @@ -181,7 +185,7 @@ class SimResourceSourceTest { val provider = SimResourceSource(capacity, clock, scheduler) val consumer = mockk<SimResourceConsumer>(relaxUnitFun = true) - every { consumer.onStart(any()) } + every { consumer.onEvent(any(), eq(SimResourceEvent.Start)) } .throws(IllegalStateException()) try { diff --git a/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceSwitchExclusiveTest.kt b/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceSwitchExclusiveTest.kt index f7d17867..1b1f7790 100644 --- a/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceSwitchExclusiveTest.kt +++ b/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceSwitchExclusiveTest.kt @@ -120,8 +120,11 @@ internal class SimResourceSwitchExclusiveTest { val workload = object : SimResourceConsumer { var isFirst = true - override fun onStart(ctx: SimResourceContext) { - isFirst = true + override fun onEvent(ctx: SimResourceContext, event: SimResourceEvent) { + when (event) { + SimResourceEvent.Start -> isFirst = true + else -> {} + } } override fun onNext(ctx: SimResourceContext): SimResourceCommand { diff --git a/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceTransformerTest.kt b/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceTransformerTest.kt index d2ad73bc..e3ca5845 100644 --- a/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceTransformerTest.kt +++ b/opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceTransformerTest.kt @@ -118,7 +118,7 @@ internal class SimResourceTransformerTest { forwarder.startConsumer(consumer) forwarder.cancel() - verify(exactly = 0) { consumer.onFinish(any(), null) } + verify(exactly = 0) { consumer.onEvent(any(), SimResourceEvent.Exit) } } @Test @@ -136,8 +136,8 @@ internal class SimResourceTransformerTest { yield() forwarder.cancel() - verify(exactly = 1) { consumer.onStart(any()) } - verify(exactly = 1) { consumer.onFinish(any(), null) } + verify(exactly = 1) { consumer.onEvent(any(), SimResourceEvent.Start) } + verify(exactly = 1) { consumer.onEvent(any(), SimResourceEvent.Exit) } } @Test @@ -155,8 +155,8 @@ internal class SimResourceTransformerTest { yield() source.cancel() - verify(exactly = 1) { consumer.onStart(any()) } - verify(exactly = 1) { consumer.onFinish(any(), null) } + verify(exactly = 1) { consumer.onEvent(any(), SimResourceEvent.Start) } + verify(exactly = 1) { consumer.onEvent(any(), SimResourceEvent.Exit) } } @Test @@ -191,7 +191,7 @@ internal class SimResourceTransformerTest { } assertEquals(3000, clock.millis()) - verify(exactly = 1) { consumer.onCapacityChanged(any(), true) } + verify(exactly = 1) { consumer.onEvent(any(), SimResourceEvent.Capacity) } } @Test |
