Skip to content
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

autoClose breaks Lazy #2388

Closed
Tracked by #2226
nomisRev opened this issue Jul 30, 2021 · 3 comments · Fixed by #2395
Closed
Tracked by #2226

autoClose breaks Lazy #2388

nomisRev opened this issue Jul 30, 2021 · 3 comments · Fixed by #2395
Labels
bug 🐛 Issues that report a problem or error in the code. framework 🏗️ Pertains to the core structure and components of the Kotest framework.
Milestone

Comments

@nomisRev
Copy link
Contributor

Which version of Kotest are you using

1.4.6

Here Lazy is immediately invoked which initializes the Lazy value, to then pass it to the immediate autoClose function which rewraps in Lazy here.

If delegation happens to other way around, meaning fun <T : AutoCloseable> autoClose(closeable: T): T uses fun <T : AutoCloseable> autoClose(closeable: Lazy<T>): Lazy<T> than Lazy'ness can be preserved.

When closing the AutoCloseable in invokeAfterSpec you can check Lazy#isInitialised before calling lazy.value.close() to avoid initializing values that are not used, and thus don't need to be closed.

@nomisRev
Copy link
Contributor Author

Causing following issue:

Integration tests with Docker hosted Postgres, and HikariDataSource.

i.e:

class IntegrationTest : StringSpec({
  val container = PostgressContainer().apply { ... }
  listeners(container.perSpec())
  val dataSource by autoClose(lazy { dataSource(container, ...) })
})

Since autoClose breaks Lazy it will try to initialize itself immediately which means it doesn't respect the lifecycle of container which was should be started before autoClose is used for the first time.

@nomisRev
Copy link
Contributor Author

Happy to open a PR for this 👍

@sksamuel
Copy link
Member

sksamuel commented Jul 30, 2021 via email

@sksamuel sksamuel added framework 🏗️ Pertains to the core structure and components of the Kotest framework. bug 🐛 Issues that report a problem or error in the code. labels Aug 1, 2021
@sksamuel sksamuel added this to the 5.0 milestone Aug 1, 2021
@sksamuel sksamuel mentioned this issue Aug 1, 2021
71 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Issues that report a problem or error in the code. framework 🏗️ Pertains to the core structure and components of the Kotest framework.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants