https://kotlinlang.org logo
#arrow
Title
# arrow
n

Navneet

03/13/2024, 8:42 AM
Hello, Does anyone know how to write an integration test for SuspendApp in Arrow? I’ve found documentation about conducting similar tests with Ktor using testApplication, but I am struggling to find any resources for Suspend App in Arrow. I’d like to run tests treating the entire application as a black box, with external dependencies passed through environment variables, and then conduct tests through API probing. Can anyone provide some insights or clue?
@Kristian Frøhlich
@simon.vergauwen, Do you have any idea?
s

simon.vergauwen

03/14/2024, 9:56 AM
Hey @Navneet,
I’d like to run tests treating the entire application as a black box, with external dependencies passed through environment variables, and then conduct tests through API probing. Can anyone provide some insights or clue?
If you want to do this, I would opt for dockerising your own application. So launch your application in docker, and test it by sending http request to it. TBH, this wouldn't be my first suggestion 😅 I typically test my modules using
testApplication
, by splitting my
main
code like this. https://github.com/nomisRev/ktor-arrow-example/blob/147b8642fbb2f8c351436ff213610b9472153334/src/main/kotlin/io/github/nomisrev/main.kt#L25 From my tests, I am using the production dependencies but with a test configuration meaning that I am connecting to Postgress TestContainers instead of a real one. This way you can start your server with
testApplication
instead of Docker, which is much faster but still get the same guarantees of testing. Testing SuspendApp specifically is really hard, since you'd be testing a JVM process. It might be possible, but probably really hard and not worth it. Hope this helps, but I'd be happy to discuss it further ☺️
n

Navneet

03/14/2024, 2:58 PM
Thanks for reply @simon.vergauwen, I thought about the docker, as you mentioned, it is heavy and worst case yes. My worry is
testApplication
might not able to capture all run time scenario. Lets take example like this
Copy code
with(statelessServiceA) {
    with(serviceWithStateB()){
        with(serviceWithStateC()) {
            suspendProcessX()
        }
        with(serviceWithStateD()) {
            suspendProcessY()
         }
    }
}
“In this case, should there be any hidden dependencies between
suspendProcessX
and
suspendProcessY
, such scenarios might remain unnoticed since these processes are encapsulated by a service with certain states. ( Or this actual behaviour is independent from suspendApp 🙂 ) Additionally, I’m curious as to why you chose to bundle dependencies rather than using a
context
. Was there a specific reason behind this?
s

simon.vergauwen

03/14/2024, 3:21 PM
Oof, a lot of question. Not usre I understood all of them.
In this case, should there be any hidden dependencies between
suspendProcessX
and
suspendProcessY
, such scenarios might remain unnoticed since these processes are encapsulated by a service with certain states.
I'm not entirely sure what you mean, in the example I shared my entire Ktor Application is defined by a single function
module
. Which does all Ktor specific things except setup the
server
.
server
comes from SuspendApp, but it's just
embeddedServer
+ wiring the
shutdown
function to the
ResourceScope
. So, that behaviour is completely independent from SuspendApp.
Additionally, I’m curious as to why you chose to bundle dependencies rather than using a
context
. Was there a specific reason behind this?
Not sure what you mean by rather than using a
context
,
if you mean why I don't use
CoroutineContext
that's because it's the same as service locator pattern (reflection / runtime errors). I try to group my dependencies in logical parts, and typically create a graph similar to how you do it in many DI frameworks but without the framework 😅 So I do very basic manually constructing of classes + passing them around. I find it much simpler, and I've compared with many DI frameworks and it typically results in less code. Most important piece in my DI setup is that I rely on
ResourceScope
to make sure all my dependencies are closed correctly when the server shuts down. I've talked about this extensively in two talks if you're interested. • Talk:

https://www.youtube.com/watch?v=A69_t_oEP_E

• Demo:

https://www.youtube.com/watch?v=zKrTBH8jqH4

• Here is a talk I gave specifically on the example project I shared before,

https://www.youtube.com/watch?v=g79A6HmbW5M

Let me know I misunderstood a question, or you have any other questions! ☺️
n

Navneet

03/15/2024, 7:44 AM
Thanks, Even though you had difficulty in understanding the question, but you answered my question, 🙂
🙌 1
2 Views