summaryrefslogtreecommitdiff
path: root/odcsim/odcsim-api/src/test
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2019-11-29 16:05:09 +0100
committerFabian Mastenbroek <mail.fabianm@gmail.com>2019-11-29 16:05:09 +0100
commitce952cf3f27c154e06cfa56ca1ad7db9ba3eac7c (patch)
tree8203f7ba7ae829ed19e23d89950b6a1bf2bde2f8 /odcsim/odcsim-api/src/test
parente6b6416e3d78178701ff5246141d3418967976fd (diff)
refactor: Rename odcsim-core to odcsim-api
This change renames the main module of the odcsim library to odcsim-api, since it mainly contains the interfaces to be used by consumers of the API and implemented by the various frameworks.
Diffstat (limited to 'odcsim/odcsim-api/src/test')
-rw-r--r--odcsim/odcsim-api/src/test/kotlin/com/atlarge/odcsim/ActorPathTest.kt86
-rw-r--r--odcsim/odcsim-api/src/test/kotlin/com/atlarge/odcsim/BehaviorTest.kt77
-rw-r--r--odcsim/odcsim-api/src/test/kotlin/com/atlarge/odcsim/CoroutinesTest.kt63
3 files changed, 226 insertions, 0 deletions
diff --git a/odcsim/odcsim-api/src/test/kotlin/com/atlarge/odcsim/ActorPathTest.kt b/odcsim/odcsim-api/src/test/kotlin/com/atlarge/odcsim/ActorPathTest.kt
new file mode 100644
index 00000000..023d3efd
--- /dev/null
+++ b/odcsim/odcsim-api/src/test/kotlin/com/atlarge/odcsim/ActorPathTest.kt
@@ -0,0 +1,86 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2018 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
+
+import org.junit.jupiter.api.Assertions.assertEquals
+import org.junit.jupiter.api.DisplayName
+import org.junit.jupiter.api.Test
+import org.junit.jupiter.api.assertThrows
+
+/**
+ * A test suite for the [ActorPath] class.
+ */
+@DisplayName("ActorPath")
+class ActorPathTest {
+ /**
+ * Test whether an [ActorPath.Root] may only start with a slash.
+ */
+ @Test
+ fun `root node may only start with a slash`() {
+ ActorPath.Root() // Assert slash at start
+ assertThrows<IllegalArgumentException> { ActorPath.Root("abc/") }
+ }
+
+ /**
+ * Test whether an [ActorPath.Child] disallows names with a slash.
+ */
+ @Test
+ fun `child node should not allow name with a slash`() {
+ assertThrows<IllegalArgumentException> { ActorPath.Child(ActorPath.Root(), "/") }
+ }
+
+ /**
+ * Test whether a root node can have a custom name.
+ */
+ @Test
+ fun `root node can have a custom name`() {
+ val name = "user"
+ assertEquals(name, ActorPath.Root(name).name)
+ }
+
+ /**
+ * Test whether a child node can be created on a root.
+ */
+ @Test
+ fun `child node can be created on a root`() {
+ val root = ActorPath.Root(name = "/user")
+ val child = root.child("child")
+
+ assertEquals(root, child.parent)
+ assertEquals("child", child.name)
+ }
+
+ /**
+ * Test whether a child node can be created on a child.
+ */
+ @Test
+ fun `child node can be created on a child`() {
+ val root = ActorPath.Root(name = "/user").child("child")
+ val child = root.child("child")
+
+ assertEquals(root, child.parent)
+ assertEquals("child", child.name)
+ }
+}
diff --git a/odcsim/odcsim-api/src/test/kotlin/com/atlarge/odcsim/BehaviorTest.kt b/odcsim/odcsim-api/src/test/kotlin/com/atlarge/odcsim/BehaviorTest.kt
new file mode 100644
index 00000000..1eb4f3b9
--- /dev/null
+++ b/odcsim/odcsim-api/src/test/kotlin/com/atlarge/odcsim/BehaviorTest.kt
@@ -0,0 +1,77 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2018 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
+
+import com.atlarge.odcsim.internal.BehaviorInterpreter
+import com.nhaarman.mockitokotlin2.mock
+import org.junit.jupiter.api.DisplayName
+import org.junit.jupiter.api.Test
+import org.junit.jupiter.api.assertThrows
+
+/**
+ * Test suite for [Behavior] and [BehaviorInterpreter].
+ */
+@DisplayName("Behavior")
+class BehaviorTest {
+ /**
+ * Test whether we cannot start an actor with the [unhandled] behavior.
+ */
+ @Test
+ fun `should not start with unhandled behavior`() {
+ val ctx = mock<ActorContext<Unit>>()
+ val interpreter = BehaviorInterpreter(unhandled<Unit>())
+ assertThrows<IllegalArgumentException> { interpreter.start(ctx) }
+ }
+
+ /**
+ * Test whether we cannot start an actor with deferred unhandled behavior.
+ */
+ @Test
+ fun `should not start with deferred unhandled behavior`() {
+ val ctx = mock<ActorContext<Unit>>()
+ val interpreter = BehaviorInterpreter(setup<Unit> { unhandled() })
+ assertThrows<IllegalArgumentException> { interpreter.start(ctx) }
+ }
+
+ /**
+ * Test whether deferred behavior that returns [same] fails.
+ */
+ @Test
+ fun `should not allow setup to return same`() {
+ val ctx = mock<ActorContext<Unit>>()
+ val interpreter = BehaviorInterpreter(setup<Unit> { same() })
+ assertThrows<IllegalArgumentException> { interpreter.start(ctx) }
+ }
+
+ /**
+ * Test whether deferred behavior that returns [unhandled] fails.
+ */
+ @Test
+ fun `should not allow setup to return unhandled`() {
+ val ctx = mock<ActorContext<Unit>>()
+ val interpreter = BehaviorInterpreter(setup<Unit> { unhandled() })
+ assertThrows<IllegalArgumentException> { interpreter.start(ctx) }
+ }
+}
diff --git a/odcsim/odcsim-api/src/test/kotlin/com/atlarge/odcsim/CoroutinesTest.kt b/odcsim/odcsim-api/src/test/kotlin/com/atlarge/odcsim/CoroutinesTest.kt
new file mode 100644
index 00000000..b59c5ea7
--- /dev/null
+++ b/odcsim/odcsim-api/src/test/kotlin/com/atlarge/odcsim/CoroutinesTest.kt
@@ -0,0 +1,63 @@
+/*
+ * 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
+
+import com.atlarge.odcsim.coroutines.SuspendingBehavior
+import com.atlarge.odcsim.coroutines.suspending
+import com.atlarge.odcsim.internal.BehaviorInterpreter
+import com.atlarge.odcsim.internal.EmptyBehavior
+import com.nhaarman.mockitokotlin2.mock
+import kotlin.coroutines.suspendCoroutine
+import org.junit.jupiter.api.Assertions.assertTrue
+import org.junit.jupiter.api.DisplayName
+import org.junit.jupiter.api.Test
+
+/**
+ * Test suite for [SuspendingBehavior] using Kotlin Coroutines.
+ */
+@DisplayName("Coroutines")
+internal class CoroutinesTest {
+
+ @Test
+ fun `should immediately return new behavior`() {
+ val ctx = mock<ActorContext<Nothing>>()
+ val behavior = suspending<Nothing> { empty() }
+ val interpreter = BehaviorInterpreter(behavior)
+ interpreter.start(ctx)
+ assertTrue(interpreter.behavior as Behavior<*> is EmptyBehavior)
+ }
+
+ @Test
+ fun `should be able to invoke regular suspend methods`() {
+ val ctx = mock<ActorContext<Unit>>()
+ val behavior = suspending<Unit> {
+ suspendCoroutine<Unit> {}
+ stopped()
+ }
+ val interpreter = BehaviorInterpreter(behavior)
+ interpreter.start(ctx)
+ interpreter.interpretMessage(ctx, Unit)
+ }
+}