New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added lazy mountable extensions #3187
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...ne/src/jvmTest/kotlin/com/sksamuel/kotest/engine/extensions/LazyMountableExtensionTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.sksamuel.kotest.engine.extensions | ||
|
||
import io.kotest.core.extensions.LazyMaterialized | ||
import io.kotest.core.extensions.LazyMountableExtension | ||
import io.kotest.core.extensions.install | ||
import io.kotest.core.listeners.BeforeSpecListener | ||
import io.kotest.core.spec.Spec | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.shouldBe | ||
import kotlinx.coroutines.delay | ||
|
||
class LazyMountableExtensionTest : FunSpec() { | ||
|
||
private val mountable = MyLazyMountable() | ||
private val m: LazyMaterialized<String> = install(mountable) | ||
|
||
init { | ||
test("lazy materialized values") { | ||
m.get() shouldBe "ready" | ||
} | ||
} | ||
} | ||
|
||
class MyLazyMountable : LazyMountableExtension<Unit, String>, BeforeSpecListener { | ||
|
||
private val m = LazyMaterialized("notready") | ||
|
||
override suspend fun beforeSpec(spec: Spec) { | ||
delay(5) // simulate slow db | ||
m.set("ready") | ||
} | ||
|
||
override fun mount(configure: (Unit) -> Unit): LazyMaterialized<String> { | ||
return m | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this not be protected from incorrect access during initialisation? It can only be safely accessed from
TestScope
? That actually depends on how the underlyingExtension
initialises it, right?This would work for my use-case, but the API is rather unsafe.
In the PR with the original use-case here there is
LazyMaterialized
withsuspend fun get()
so it can only be accessed fromsuspend TestScope.() -> Unit
. If it's access before it needs to be somehow in asuspend
context and will get loaded lazily then. If it's accessed afterclear/close
is called (onafterSpec
or similar) then it'll throw anIllegalStateException
.Should we have a similarly "safer" API? I.e. the example here
Kafka
also involves some heavy I/O, blocking code both onstart
andclose
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, short-answer: yes this works.
Long-answer: should we try to make the API safer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree the API isn't great, but I don't know how we can limit it to test scope, or even if we care about that, as long as it supports suspension.
Like you might want to initialize in a before spec listener. That's a suspendable context.
Let me think more on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I have followed your guidelines and it is much clearer now.
A
LazyMountableExtension
returns aLazyMaterialized
which has a SAM.Once get is invoked, the implementation can initialize if required.
Here is the example from the tests: