In Ktor, is it possible to make an integration tes...
# ktor
t
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
Basically you’re testing two systems. I’d normally test them in isolation based on the inputs and expected outputs.
t
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
Can you explain how those two servers intercact?
t
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
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
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
What do you mean by sending some test data through the pipeline?
t
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
I think the simplest way of solving the problem is to run both production servers in an isolated environment and test their interaction.
t
Do you mean manually starting the servers and then running some test which pushes data through them?
a
Yes
t
Thank you for the suggestion, but I'm afraid that it needs to be automated for continuous testing in a CI/CD pipeline.