Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions engine/src/main/kotlin/io/canopy/engine/core/flows/Context.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ fun <T : Any> Node<*>.fromContextOrNull(key: String): T? {

while (current != null) {
if (current is Context) {
current.provided[key]?.let {
return it.invoke() as? T ?: continue
val value = current.provided[key]
if (value != null) {
return value() as T
Comment on lines +92 to +94
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
}
current = current.parent
Expand Down
35 changes: 35 additions & 0 deletions engine/src/test/kotlin/io/canopy/engine/core/flows/ContextTests.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package io.canopy.engine.core.flows

import kotlin.test.assertFailsWith
import kotlin.time.Duration.Companion.seconds
import java.util.concurrent.atomic.AtomicBoolean
import io.canopy.engine.core.managers.ManagersRegistry
import io.canopy.engine.core.managers.SceneManager
import io.canopy.engine.core.nodes.Node
import io.canopy.engine.core.nodes.types.empty.EmptyNode
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -207,6 +214,34 @@ class ContextTests {
assertEquals(1, c.fromContext("keyA"))
}

@Test
fun `resolving un-provided data shouldn't hang the thread`() = runBlocking {
val root = n("root") {
Context {
n("a")
}
}

root.buildTree()

val a = root.getNode<EmptyNode>("./a")

val noHang = AtomicBoolean(false)

val job = launch(Dispatchers.Default) {
withTimeout(2.seconds) {
assertFailsWith<NoSuchElementException> {
a.fromContext<String>("data")
}
noHang.set(true)
}
}

job.join()

assertTrue(noHang.get())
}

// --- Tiny adapter -------------------------------------------------------
// If your Node doesn't expose children()/name, replace these calls with your real APIs.

Expand Down
Loading