summaryrefslogtreecommitdiff
path: root/odcsim-testkit/src/main/kotlin/com/atlarge/odcsim/testkit/BehaviorTestKit.kt
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2019-04-29 14:56:51 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2019-05-14 12:55:55 +0200
commit7428262dcd8da85de0adca0ef82c57398cf411fc (patch)
treec38e8bcaf2a47fda8d57bbb402608e9462041e0d /odcsim-testkit/src/main/kotlin/com/atlarge/odcsim/testkit/BehaviorTestKit.kt
parent0f71f1e1c559bfebc20dad4ae979e7f6b58b7acf (diff)
feat: Add testkit for testing behavior
This change adds a testkit for synchronously testking Behavior implementations.
Diffstat (limited to 'odcsim-testkit/src/main/kotlin/com/atlarge/odcsim/testkit/BehaviorTestKit.kt')
-rw-r--r--odcsim-testkit/src/main/kotlin/com/atlarge/odcsim/testkit/BehaviorTestKit.kt138
1 files changed, 138 insertions, 0 deletions
diff --git a/odcsim-testkit/src/main/kotlin/com/atlarge/odcsim/testkit/BehaviorTestKit.kt b/odcsim-testkit/src/main/kotlin/com/atlarge/odcsim/testkit/BehaviorTestKit.kt
new file mode 100644
index 00000000..65782d52
--- /dev/null
+++ b/odcsim-testkit/src/main/kotlin/com/atlarge/odcsim/testkit/BehaviorTestKit.kt
@@ -0,0 +1,138 @@
+/*
+ * 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
+
+import com.atlarge.odcsim.ActorContext
+import com.atlarge.odcsim.ActorPath
+import com.atlarge.odcsim.ActorRef
+import com.atlarge.odcsim.Behavior
+import com.atlarge.odcsim.Instant
+import com.atlarge.odcsim.isAlive
+import com.atlarge.odcsim.testkit.internal.BehaviorTestKitImpl
+import java.util.UUID
+
+/**
+ * Utensils for synchronous testing of [Behavior] instances.
+ *
+ * @param T The shape of the messages the behavior accepts.
+ */
+interface BehaviorTestKit<T : Any> {
+ /**
+ * The current point in simulation time.
+ */
+ val time: Instant
+
+ /**
+ * The current behavior of the simulated actor.
+ */
+ val behavior: Behavior<T>
+
+ /**
+ * The self reference to the simulated actor inside the test kit.
+ */
+ val ref: ActorRef<T>
+
+ /**
+ * The context of the simulated actor.
+ */
+ val context: ActorContext<T>
+
+ /**
+ * The inbox of the simulated actor.
+ */
+ val inbox: TestInbox<T>
+
+ /**
+ * A flag indicating whether the [Behavior] instance is still alive.
+ */
+ val isAlive: Boolean get() = behavior.isAlive
+
+ /**
+ * Interpret the specified message at the current point in simulation time using the current behavior.
+ *
+ * @return `true` if the message was handled by the behavior, `false` if it was unhandled.
+ */
+ fun run(msg: T): Boolean
+
+ /**
+ * Interpret the oldest message in the inbox using the current behavior.
+ *
+ * @return `true` if the message was handled by the behavior, `false` if it was unhandled.
+ * @throws NoSuchElementException if the actor's inbox is empty.
+ */
+ fun runOne(): Boolean
+
+ /**
+ * Interpret the messages in the inbox until the specified point in simulation time is reached.
+ *
+ * @param time The time until which the messages in the inbox should be processed.
+ */
+ fun runTo(time: Instant)
+
+ /**
+ * Create an anonymous [TestInbox] for receiving messages.
+ */
+ fun <U : Any> createInbox(): TestInbox<U>
+
+ /**
+ * Get the child inbox for the child with the given name, or fail if there is no child with the given name
+ * spawned
+ */
+ fun <U : Any> childInbox(name: String): TestInbox<U>
+
+ /**
+ * Get the child inbox for the child referenced by [ref], or fail if it is not a child of this behavior.
+ */
+ fun <U : Any> childInbox(ref: ActorRef<U>): TestInbox<U>
+
+ /**
+ * Obtain the [BehaviorTestKit] for the child with the given name, or fail if there is no child with the given
+ * name spawned.
+ */
+ fun <U : Any> childTestKit(name: String): BehaviorTestKit<U>
+
+ /**
+ * Obtain the [BehaviorTestKit] for the given child [ActorRef].
+ */
+ fun <U : Any> childTestKit(ref: ActorRef<U>): BehaviorTestKit<U>
+
+ companion object {
+ /**
+ * Create a [BehaviorTestKit] instance for the specified [Behavior].
+ *
+ * @param behavior The behavior for which a test kit should be created.
+ */
+ operator fun <T : Any> invoke(behavior: Behavior<T>): BehaviorTestKit<T> =
+ BehaviorTestKitImpl(behavior, ActorPath.Root(name = "/" + UUID.randomUUID().toString()))
+
+ /**
+ * Create a [BehaviorTestKit] instance for the specified [Behavior].
+ *
+ * @param behavior The behavior for which a test kit should be created.
+ */
+ @JvmStatic
+ fun <T : Any> create(behavior: Behavior<T>): BehaviorTestKit<T> = invoke(behavior)
+ }
+}