From 6ca0ae07669d20a5a34ef697610df90754024035 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Wed, 20 Nov 2019 17:51:58 +0100 Subject: refactor: Move build logic to buildSrc --- .../odcsim/testkit/internal/ActorContextStub.kt | 99 ------------------ .../odcsim/testkit/internal/ActorSystemStub.kt | 58 ----------- .../odcsim/testkit/internal/BehaviorTestKitImpl.kt | 112 --------------------- .../odcsim/testkit/internal/TestInboxImpl.kt | 100 ------------------ 4 files changed, 369 deletions(-) delete mode 100644 odcsim-testkit/src/main/kotlin/com/atlarge/odcsim/testkit/internal/ActorContextStub.kt delete mode 100644 odcsim-testkit/src/main/kotlin/com/atlarge/odcsim/testkit/internal/ActorSystemStub.kt delete mode 100644 odcsim-testkit/src/main/kotlin/com/atlarge/odcsim/testkit/internal/BehaviorTestKitImpl.kt delete mode 100644 odcsim-testkit/src/main/kotlin/com/atlarge/odcsim/testkit/internal/TestInboxImpl.kt (limited to 'odcsim-testkit/src/main/kotlin/com/atlarge/odcsim/testkit/internal') diff --git a/odcsim-testkit/src/main/kotlin/com/atlarge/odcsim/testkit/internal/ActorContextStub.kt b/odcsim-testkit/src/main/kotlin/com/atlarge/odcsim/testkit/internal/ActorContextStub.kt deleted file mode 100644 index 79873141..00000000 --- a/odcsim-testkit/src/main/kotlin/com/atlarge/odcsim/testkit/internal/ActorContextStub.kt +++ /dev/null @@ -1,99 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2019 atlarge-research - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package com.atlarge.odcsim.testkit.internal - -import com.atlarge.odcsim.ActorContext -import com.atlarge.odcsim.ActorRef -import com.atlarge.odcsim.ActorSystem -import com.atlarge.odcsim.Behavior -import com.atlarge.odcsim.Duration -import com.atlarge.odcsim.Instant -import com.atlarge.odcsim.internal.logging.LoggerImpl -import org.slf4j.Logger -import java.util.UUID - -/** - * A stubbed [ActorContext] implementation for synchronous behavior testing. - * - * @property owner The owner of this context. - */ -internal class ActorContextStub(private val owner: BehaviorTestKitImpl) : ActorContext { - /** - * The children of this context. - */ - val childActors = HashMap>() - - override val self: ActorRef - get() = owner.ref - - override val children: List> - get() = childActors.values.map { it.ref } - - override val time: Instant - get() = owner.time - - override val system: ActorSystem<*> by lazy { - ActorSystemStub(owner) - } - - override val log: Logger by lazy { - LoggerImpl(this) - } - - override fun getChild(name: String): ActorRef<*>? = childActors[name]?.ref - - override fun send(ref: ActorRef, msg: U, after: Duration) { - if (ref !is TestInboxImpl.ActorRefImpl) { - throw IllegalArgumentException("The referenced ActorRef is not part of the test kit") - } - - ref.send(msg, after) - } - - override fun spawn(behavior: Behavior, name: String): ActorRef { - val btk = BehaviorTestKitImpl(behavior, self.path.child(name)) - childActors[name] = btk - return btk.ref - } - - override fun spawnAnonymous(behavior: Behavior): ActorRef { - return spawn(behavior, "$" + UUID.randomUUID()) - } - - override fun stop(child: ActorRef<*>) { - require(child.path.parent == self.path) { "Only direct children of an actor may be stopped through the actor context." } - childActors -= child.path.name - } - - override fun watch(target: ActorRef<*>) {} - - override fun unwatch(target: ActorRef<*>) {} - - override fun sync(target: ActorRef<*>) {} - - override fun unsync(target: ActorRef<*>) {} - - override fun isSync(target: ActorRef<*>): Boolean = true -} diff --git a/odcsim-testkit/src/main/kotlin/com/atlarge/odcsim/testkit/internal/ActorSystemStub.kt b/odcsim-testkit/src/main/kotlin/com/atlarge/odcsim/testkit/internal/ActorSystemStub.kt deleted file mode 100644 index fee34a48..00000000 --- a/odcsim-testkit/src/main/kotlin/com/atlarge/odcsim/testkit/internal/ActorSystemStub.kt +++ /dev/null @@ -1,58 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2019 atlarge-research - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package com.atlarge.odcsim.testkit.internal - -import com.atlarge.odcsim.ActorPath -import com.atlarge.odcsim.ActorRef -import com.atlarge.odcsim.ActorSystem -import com.atlarge.odcsim.Behavior -import com.atlarge.odcsim.Duration -import com.atlarge.odcsim.Instant - -/** - * A stubbed [ActorSystem] for synchronous testing of behavior. - * - * @property owner The owner of this actor system. - */ -internal class ActorSystemStub(private val owner: BehaviorTestKitImpl) : ActorSystem { - override val time: Instant - get() = owner.time - - override val name: String - get() = owner.ref.path.name - - override fun run(until: Duration) = throw IllegalStateException("Cannot run ActorSystem within actor") - - override fun send(msg: T, after: Duration) = owner.context.send(owner.context.self, msg, after) - - override fun terminate() {} - - override val path: ActorPath - get() = owner.ref.path - - override suspend fun spawnSystem(behavior: Behavior, name: String): ActorRef { - throw IllegalStateException("Cannot spawn system actor") - } -} diff --git a/odcsim-testkit/src/main/kotlin/com/atlarge/odcsim/testkit/internal/BehaviorTestKitImpl.kt b/odcsim-testkit/src/main/kotlin/com/atlarge/odcsim/testkit/internal/BehaviorTestKitImpl.kt deleted file mode 100644 index 2bd5b973..00000000 --- a/odcsim-testkit/src/main/kotlin/com/atlarge/odcsim/testkit/internal/BehaviorTestKitImpl.kt +++ /dev/null @@ -1,112 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2019 atlarge-research - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package com.atlarge.odcsim.testkit.internal - -import com.atlarge.odcsim.ActorPath -import com.atlarge.odcsim.ActorRef -import com.atlarge.odcsim.Behavior -import com.atlarge.odcsim.Instant -import com.atlarge.odcsim.internal.BehaviorInterpreter -import com.atlarge.odcsim.testkit.BehaviorTestKit -import com.atlarge.odcsim.testkit.TestInbox -import java.util.UUID -import kotlin.math.max - -/** - * Default implementation of the [BehaviorTestKit] interface. - * - * @param initialBehavior The initial behavior to initialize the actor of the test kit with. - * @param path The path to the actor. - */ -internal class BehaviorTestKitImpl( - initialBehavior: Behavior, - path: ActorPath -) : BehaviorTestKit { - /** - * The [BehaviorInterpreter] used to interpret incoming messages. - */ - private val interpreter = BehaviorInterpreter(initialBehavior) - - /** - * A flag to indicate whether the behavior was initially started. - */ - private var isStarted: Boolean = false - - override var time: Instant = .0 - private set - - override val inbox: TestInbox = TestInboxImpl(this, path) - - override val behavior: Behavior get() = interpreter.behavior - - override val ref: ActorRef = inbox.ref - - override val context: ActorContextStub = ActorContextStub(this) - - override fun run(msg: T): Boolean { - if (!isStarted) { - isStarted = true - interpreter.start(context) - } - - return interpreter.interpretMessage(context, msg) - } - - override fun runOne(): Boolean { - val (delivery, msg) = inbox.receiveEnvelope() - time = max(time, delivery) - return run(msg) - } - - override fun runTo(time: Instant) { - while (inbox.hasMessages && this.time <= time) { - runOne() - } - this.time = time - } - - override fun createInbox(): TestInbox { - return TestInboxImpl(this, ref.path.child(UUID.randomUUID().toString())) - } - - override fun childInbox(name: String): TestInbox = childTestKit(name).inbox - - override fun childInbox(ref: ActorRef): TestInbox = childTestKit(ref).inbox - - override fun childTestKit(name: String): BehaviorTestKit { - @Suppress("UNCHECKED_CAST") - return context.childActors[ref.path.name] as BehaviorTestKitImpl? ?: throw IllegalArgumentException("$ref is not a child of $this") - } - - override fun childTestKit(ref: ActorRef): BehaviorTestKit { - val btk = childTestKit(ref.path.name) - - if (btk.ref != ref) { - throw IllegalArgumentException("$ref is not a child of $this") - } - - return btk - } -} diff --git a/odcsim-testkit/src/main/kotlin/com/atlarge/odcsim/testkit/internal/TestInboxImpl.kt b/odcsim-testkit/src/main/kotlin/com/atlarge/odcsim/testkit/internal/TestInboxImpl.kt deleted file mode 100644 index 35abd758..00000000 --- a/odcsim-testkit/src/main/kotlin/com/atlarge/odcsim/testkit/internal/TestInboxImpl.kt +++ /dev/null @@ -1,100 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2019 atlarge-research - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package com.atlarge.odcsim.testkit.internal - -import com.atlarge.odcsim.ActorPath -import com.atlarge.odcsim.ActorRef -import com.atlarge.odcsim.Envelope -import com.atlarge.odcsim.Instant -import com.atlarge.odcsim.testkit.TestInbox -import org.junit.jupiter.api.Assertions.assertEquals -import java.util.PriorityQueue - -/** - * A helper class for testing messages sent to an [ActorRef]. - * - * @param owner The owner of the inbox. - * @param path The path to the test inbox. - * @param T The shape of the messages the inbox accepts. - */ -internal class TestInboxImpl(private val owner: BehaviorTestKitImpl<*>, path: ActorPath) : TestInbox { - /** - * The queue of received messages. - */ - private val inbox = PriorityQueue>() - - /** - * The identifier for the next message to be scheduled. - */ - private var nextId: Long = 0 - - override val ref: ActorRef = ActorRefImpl(path) - - override val hasMessages: Boolean - get() = inbox.isNotEmpty() - - override fun receiveEnvelope(): Envelope = inbox.remove() - - override fun receiveAll(): List> = inbox.toList().also { inbox.clear() } - - override fun clear() = inbox.clear() - - override fun expectMessage(expected: T) = assertEquals(expected, receiveMessage()) - - override fun expectMessage(expected: T, message: String) = assertEquals(expected, receiveMessage(), message) - - internal inner class ActorRefImpl(override val path: ActorPath) : ActorRef { - /** - * Send the specified message to the actor this reference is pointing to after the specified delay. - * - * @param msg The message to send. - * @param after The delay before the message is received. - */ - fun send(msg: T, after: Instant) { - inbox.add(EnvelopeImpl(nextId++, owner.time + after, msg)) - } - } - - /** - * A wrapper around a message that has been scheduled for processing. - * - * @property id The identifier of the message to keep the priority queue stable. - * @property time The point in time to deliver the message. - * @property message The message to wrap. - */ - private inner class EnvelopeImpl( - val id: Long, - override val time: Instant, - override val message: T - ) : Envelope { - override fun compareTo(other: Envelope<*>): Int { - val cmp = super.compareTo(other) - return if (cmp == 0 && other is EnvelopeImpl) - id.compareTo(other.id) - else - cmp - } - } -} -- cgit v1.2.3