diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2019-05-09 17:54:02 +0200 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2019-05-14 12:55:56 +0200 |
| commit | 2f6dcaef25d80a1411512e482953c83990149fd1 (patch) | |
| tree | 7832040f6c99c83140d99b732f6583298e167ef6 /odcsim-testkit/src/main/kotlin/com/atlarge | |
| parent | 1f77d1011577c54e98ad0cbbd898817f98000881 (diff) | |
refactor: Make actor spawning and stopping behavior consistent
This change makes the behavior for spawning and stopping a child actor
more consistent and better specified in the documentation.
Diffstat (limited to 'odcsim-testkit/src/main/kotlin/com/atlarge')
3 files changed, 17 insertions, 10 deletions
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 index 7035b908..79873141 100644 --- 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 @@ -43,11 +43,14 @@ internal class ActorContextStub<T : Any>(private val owner: BehaviorTestKitImpl< /** * The children of this context. */ - val children = HashMap<String, BehaviorTestKitImpl<*>>() + val childActors = HashMap<String, BehaviorTestKitImpl<*>>() override val self: ActorRef<T> get() = owner.ref + override val children: List<ActorRef<*>> + get() = childActors.values.map { it.ref } + override val time: Instant get() = owner.time @@ -59,6 +62,8 @@ internal class ActorContextStub<T : Any>(private val owner: BehaviorTestKitImpl< LoggerImpl(this) } + override fun getChild(name: String): ActorRef<*>? = childActors[name]?.ref + override fun <U : Any> send(ref: ActorRef<U>, msg: U, after: Duration) { if (ref !is TestInboxImpl.ActorRefImpl) { throw IllegalArgumentException("The referenced ActorRef is not part of the test kit") @@ -69,7 +74,7 @@ internal class ActorContextStub<T : Any>(private val owner: BehaviorTestKitImpl< override fun <U : Any> spawn(behavior: Behavior<U>, name: String): ActorRef<U> { val btk = BehaviorTestKitImpl(behavior, self.path.child(name)) - children[name] = btk + childActors[name] = btk return btk.ref } @@ -77,13 +82,9 @@ internal class ActorContextStub<T : Any>(private val owner: BehaviorTestKitImpl< return spawn(behavior, "$" + UUID.randomUUID()) } - override fun stop(child: ActorRef<*>): Boolean { - if (child.path.parent != self.path) { - // This is not a child of this actor - return false - } - children -= child.path.name - return true + 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<*>) {} 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 index f61c1d76..fee34a48 100644 --- 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 @@ -25,7 +25,9 @@ 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 @@ -49,4 +51,8 @@ internal class ActorSystemStub<T : Any>(private val owner: BehaviorTestKitImpl<T override val path: ActorPath get() = owner.ref.path + + override suspend fun <U : Any> spawnSystem(behavior: Behavior<U>, name: String): ActorRef<U> { + 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 index cb216614..2bd5b973 100644 --- 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 @@ -97,7 +97,7 @@ internal class BehaviorTestKitImpl<T : Any>( override fun <U : Any> childTestKit(name: String): BehaviorTestKit<U> { @Suppress("UNCHECKED_CAST") - return context.children[ref.path.name] as BehaviorTestKitImpl<U>? ?: throw IllegalArgumentException("$ref is not a child of $this") + return context.childActors[ref.path.name] as BehaviorTestKitImpl<U>? ?: throw IllegalArgumentException("$ref is not a child of $this") } override fun <U : Any> childTestKit(ref: ActorRef<U>): BehaviorTestKit<U> { |
