diff options
Diffstat (limited to 'opendc-simulator/opendc-simulator-resources')
| -rw-r--r-- | opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/SimResourceForwarder.kt (renamed from opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/SimResourceTransformer.kt) | 74 | ||||
| -rw-r--r-- | opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/SimResourceSwitchExclusive.kt | 4 | ||||
| -rw-r--r-- | opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceForwarderTest.kt (renamed from opendc-simulator/opendc-simulator-resources/src/test/kotlin/org/opendc/simulator/resources/SimResourceTransformerTest.kt) | 18 |
3 files changed, 32 insertions, 64 deletions
diff --git a/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/SimResourceTransformer.kt b/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/SimResourceForwarder.kt index 76a3bdd7..0cd2bfc7 100644 --- a/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/SimResourceTransformer.kt +++ b/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/SimResourceForwarder.kt @@ -26,16 +26,11 @@ import org.opendc.simulator.resources.impl.SimResourceCountersImpl import java.time.Clock /** - * A [SimResourceConsumer] and [SimResourceProvider] that transforms the resource commands emitted by the resource - * commands to the resource provider. + * A class that acts as a [SimResourceConsumer] and [SimResourceProvider] at the same time. * * @param isCoupled A flag to indicate that the transformer will exit when the resource consumer exits. - * @param transform The function to transform the received resource command. */ -public class SimResourceTransformer( - private val isCoupled: Boolean = false, - private val transform: (SimResourceContext, Long) -> Long -) : SimResourceConsumer, SimResourceProvider, AutoCloseable { +public class SimResourceForwarder(private val isCoupled: Boolean = false) : SimResourceConsumer, SimResourceProvider, AutoCloseable { /** * The delegate [SimResourceConsumer]. */ @@ -49,25 +44,25 @@ public class SimResourceTransformer( /** * The exposed [SimResourceContext]. */ - private val ctx = object : SimResourceContext { + private val _ctx = object : SimResourceContext { override val clock: Clock - get() = _ctx!!.clock + get() = _innerCtx!!.clock override val capacity: Double - get() = _ctx?.capacity ?: 0.0 + get() = _innerCtx?.capacity ?: 0.0 override val demand: Double - get() = _ctx?.demand ?: 0.0 + get() = _innerCtx?.demand ?: 0.0 override val speed: Double - get() = _ctx?.speed ?: 0.0 + get() = _innerCtx?.speed ?: 0.0 override fun interrupt() { - _ctx?.interrupt() + _innerCtx?.interrupt() } override fun push(rate: Double) { - _ctx?.push(rate) + _innerCtx?.push(rate) _limit = rate } @@ -75,9 +70,9 @@ public class SimResourceTransformer( val delegate = checkNotNull(delegate) { "Delegate not active" } if (isCoupled) - _ctx?.close() + _innerCtx?.close() else - _ctx?.push(0.0) + _innerCtx?.push(0.0) // Warning: resumption of the continuation might change the entire state of the forwarder. Make sure we // reset beforehand the existing state and check whether it has been updated afterwards @@ -90,19 +85,19 @@ public class SimResourceTransformer( /** * The [SimResourceContext] in which the forwarder runs. */ - private var _ctx: SimResourceContext? = null + private var _innerCtx: SimResourceContext? = null override val isActive: Boolean get() = delegate != null override val capacity: Double - get() = ctx.capacity + get() = _ctx.capacity override val speed: Double - get() = ctx.speed + get() = _ctx.speed override val demand: Double - get() = ctx.demand + get() = _ctx.demand override val counters: SimResourceCounters get() = _counters @@ -118,27 +113,27 @@ public class SimResourceTransformer( } override fun interrupt() { - ctx.interrupt() + _ctx.interrupt() } override fun cancel() { val delegate = delegate - val ctx = _ctx + val ctx = _innerCtx if (delegate != null) { this.delegate = null if (ctx != null) { - delegate.onEvent(this.ctx, SimResourceEvent.Exit) + delegate.onEvent(this._ctx, SimResourceEvent.Exit) } } } override fun close() { - val ctx = _ctx + val ctx = _innerCtx if (ctx != null) { - this._ctx = null + this._innerCtx = null ctx.interrupt() } } @@ -152,38 +147,34 @@ public class SimResourceTransformer( updateCounters(ctx, delta) - return if (delegate != null) { - transform(ctx, delegate.onNext(this.ctx, now, delta)) - } else { - Long.MAX_VALUE - } + return delegate?.onNext(this._ctx, now, delta) ?: Long.MAX_VALUE } override fun onEvent(ctx: SimResourceContext, event: SimResourceEvent) { when (event) { SimResourceEvent.Start -> { - _ctx = ctx + _innerCtx = ctx } SimResourceEvent.Exit -> { - _ctx = null + _innerCtx = null val delegate = delegate if (delegate != null) { reset() - delegate.onEvent(this.ctx, SimResourceEvent.Exit) + delegate.onEvent(this._ctx, SimResourceEvent.Exit) } } - else -> delegate?.onEvent(this.ctx, event) + else -> delegate?.onEvent(this._ctx, event) } } override fun onFailure(ctx: SimResourceContext, cause: Throwable) { - _ctx = null + _innerCtx = null val delegate = delegate if (delegate != null) { reset() - delegate.onFailure(this.ctx, cause) + delegate.onFailure(this._ctx, cause) } } @@ -192,7 +183,7 @@ public class SimResourceTransformer( */ private fun start() { val delegate = delegate ?: return - delegate.onEvent(checkNotNull(_ctx), SimResourceEvent.Start) + delegate.onEvent(checkNotNull(_innerCtx), SimResourceEvent.Start) hasDelegateStarted = true } @@ -227,12 +218,3 @@ public class SimResourceTransformer( counters.overcommit += (work - actualWork) } } - -/** - * Constructs a [SimResourceTransformer] that forwards the received resource command with an identity transform. - * - * @param isCoupled A flag to indicate that the transformer will exit when the resource consumer exits. - */ -public fun SimResourceForwarder(isCoupled: Boolean = false): SimResourceTransformer { - return SimResourceTransformer(isCoupled) { _, command -> command } -} diff --git a/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/SimResourceSwitchExclusive.kt b/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/SimResourceSwitchExclusive.kt index 2be8ccb0..f1e004d2 100644 --- a/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/SimResourceSwitchExclusive.kt +++ b/opendc-simulator/opendc-simulator-resources/src/main/kotlin/org/opendc/simulator/resources/SimResourceSwitchExclusive.kt @@ -37,7 +37,7 @@ public class SimResourceSwitchExclusive : SimResourceSwitch { private val _inputs = mutableSetOf<SimResourceProvider>() override val inputs: Set<SimResourceProvider> get() = _inputs - private val _availableInputs = ArrayDeque<SimResourceTransformer>() + private val _availableInputs = ArrayDeque<SimResourceForwarder>() override val counters: SimResourceCounters = object : SimResourceCounters { override val demand: Double @@ -114,7 +114,7 @@ public class SimResourceSwitchExclusive : SimResourceSwitch { /** * An output of the resource switch. */ - private inner class Output(private val forwarder: SimResourceTransformer) : SimResourceProvider by forwarder { + private inner class Output(private val forwarder: SimResourceForwarder) : SimResourceProvider by forwarder { /** * Close the output. */ 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/SimResourceForwarderTest.kt index d7d0924f..49e60f68 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/SimResourceForwarderTest.kt @@ -33,9 +33,9 @@ import org.opendc.simulator.resources.consumer.SimWorkConsumer import org.opendc.simulator.resources.impl.SimResourceInterpreterImpl /** - * A test suite for the [SimResourceTransformer] class. + * A test suite for the [SimResourceForwarder] class. */ -internal class SimResourceTransformerTest { +internal class SimResourceForwarderTest { @Test fun testCancelImmediately() = runBlockingSimulation { val forwarder = SimResourceForwarder() @@ -199,20 +199,6 @@ internal class SimResourceTransformerTest { } @Test - fun testTransformExit() = runBlockingSimulation { - val forwarder = SimResourceTransformer { ctx, _ -> ctx.close(); Long.MAX_VALUE } - val scheduler = SimResourceInterpreterImpl(coroutineContext, clock) - val source = SimResourceSource(1.0, scheduler) - - val consumer = spyk(SimWorkConsumer(2.0, 1.0)) - source.startConsumer(forwarder) - forwarder.consume(consumer) - - assertEquals(0, clock.millis()) - verify(exactly = 1) { consumer.onNext(any(), any(), any()) } - } - - @Test fun testCounters() = runBlockingSimulation { val forwarder = SimResourceForwarder() val scheduler = SimResourceInterpreterImpl(coroutineContext, clock) |
