Hey, we are now using SuspendedApp and resourceSco...
# arrow
g
Hey, we are now using SuspendedApp and resourceScope for our new Kotlin Backend Apps and it feels great, but we now lack the chance of testing everything nicely. Is there a way to test the release functions of a resource, or to test the shutdown of a SuspendedApp?
s
Glad you're happy with that 🙌 Testing the shutdown of
SuspendApp
is tricky, as that's coupled to OS signals 🤔 It's potentially possible on JVM, but might require some really nasty
Signal
OS intercepting code. I've never looked that deep.
If your resources are split from
SuspendApp
you can test it the finalisers, but that not sure if that's worth the time and effort. Since you'd be retesting
Resource
which is already covered in Arrow. In general I personally find it bad practice to re-test library code, it's typically very cost ineffective.
Maybe I misunderstood, can you share an example of the kind-of code you're trying to test?
g
I was able to test resource teardown with resourceScope by using it in the tests but not awaiting any cancellation, so in the end the finallizer is called. Idk why I never came up with that idea. Kind of a problem is that (in comparison to classic ktor) our main method is kind of big:
Copy code
SuspendedApp{ either { resourceScope{
  Do crazy amount of stuff
  server and port bind
  await
}}}
I guess this is our fault , so we have to restructure that to make it testable, or parts of it
You should advertise it more, SuspendedApp solves a LOT of problems. I had no idea then to put closing methods in functions with event listeners and wait a ridicolous amount of time with Thread.sleep in the Java native runtime closing method. It was very ugly, but without better ideas and a company that didn't really want to use ktor this was the best I came up with
🙌 1
🙌🏾 1
https://kotlindevday.com/videos/grateful-shutdown-with-structured-concurrency-simon-vergauwen/ this was an eye opener for me and my team in the new company. This really opened the door for so much more. Thank you so much for that @simon.vergauwen
❤️ 2
s
I'm super happy to hear that ☺️ I/we should advertise it more! 😄 It's hard doing promotion, OSS development, work, etc. And my marketing/promotion skills is probably the weakest 😅
Yes, splitting the
main
in different functions is quite useful for testing