https://kotlinlang.org logo
#ktor
Title
# ktor
t

Tyler Kinkade

03/31/2024, 11:55 PM
In Ktor, is it possible to make an integration test that involves more than one server? For example, a registration app and a notification app that interact. Would you have to use multiple TestApplication instances? Only one TestApplication? Or would I need to start each server with multiple embeddedServer calls?
👀 1
a

Arjan van Wieringen

04/01/2024, 9:37 AM
Basically you’re testing two systems. I’d normally test them in isolation based on the inputs and expected outputs.
t

Tyler Kinkade

04/02/2024, 2:44 AM
Thanks for your reply, @Arjan van Wieringen. I have already created tests which test each server in isolation using mock external services. However, for this project, I'm required to also test them as an integrated whole. Is it possible with Ktor? When I attempt to create a TestApplication and load Application modules (e.g.,
Application.configureDatabases()
) from more than one server, Kotlin complains that it can't identify which module goes with which app, and I can't figure out how to specify that. I've tried things like
com.example.appA.configureRouting()
, but that doesn't work either.
a

Aleksei Tirman [JB]

04/02/2024, 6:40 AM
Can you explain how those two servers intercact?
t

Tyler Kinkade

04/02/2024, 7:27 AM
For example, when the registration server receives a registration request from a user, a confirmation code is generated and sent via message queue to the notification server, which sends the user an email with the code prompting them to confirm registration, which is processed by the registration server. (The user would have to be mocked with an automated sendgrid server in the integration test.) An integration test would confirm that the data travels through that loop correctly: mock user -> registration request -> notification -> confirmation email (sendgrid server) -> registration confirmation
a

Aleksei Tirman [JB]

04/02/2024, 7:43 AM
The basic limitation of the
TestApplication
is that only HTTP clients "created" from the test server are able to communicate with it. So, for the two test servers at least two clients are required to communicate with the corresponding servers.
Unfortunately, there is no network abstraction to have a shared medium for the multiple clients and servers to communicate.
thank you color 1
t

Tyler Kinkade

04/02/2024, 7:49 AM
Ok, thank you. That makes sense. Would it be possible to use Ktor to create a third "integration" server which somehow starts the other two servers and sends some test data through the pipeline?
a

Aleksei Tirman [JB]

04/02/2024, 7:50 AM
What do you mean by sending some test data through the pipeline?
t

Tyler Kinkade

04/02/2024, 7:59 AM
Sorry, I'm not real sure, actually. I'm grasping at straws right now trying to come up with a solution. I came across this Ktor Github Issue about running multiple servers in a single process, so I thought maybe something like that might work, and then I could create a TestApplication for that "integrated" server which would send fake registration requests to the registration server and assert that the registration for each request is ultimately confirmed successfully (after traveling via the notification server and sendgrid server). But, I don't really know enough about Ktor to know if that's even feasible.
a

Aleksei Tirman [JB]

04/02/2024, 9:14 AM
I think the simplest way of solving the problem is to run both production servers in an isolated environment and test their interaction.
t

Tyler Kinkade

04/02/2024, 10:19 AM
Do you mean manually starting the servers and then running some test which pushes data through them?
a

Aleksei Tirman [JB]

04/02/2024, 10:20 AM
Yes
t

Tyler Kinkade

04/02/2024, 10:31 AM
Thank you for the suggestion, but I'm afraid that it needs to be automated for continuous testing in a CI/CD pipeline.
3 Views