Run ktor test using `testApplication` without a client call - kotlin

Is it possible to write a ktor test without any client call? We have a ktor kafka consumer service that executes http calls. The shape of our test is:
startAndConfigureWiremock()
testApplication{
application{
changeConfiguration()
}
sendKafkaMessage()
verifyExternalCall()
}
but the tests with any client calls do not work. Test code verifyExternalCall() is executed before the service is up and blocks the startup without any testBlocking.
When we try to add parallelism like GlobalScope.launch it runs the application, but it just starts and stops.
Looks like testApplication needs a client call to work at all and even ktor test relay on it: https://github.com/ktorio/ktor/blob/8b784f45a6339728ce7181498a5854b29bf9d2a5/ktor-server/ktor-server-core/jvmAndNix/test/io/ktor/server/application/HooksTest.kt#L81

This might not be the answer you are looking for but...
This definately looks like the incorrect place and method for performing this test.
Ktor is for building web APIs, it is for dealing with things like routing and serialisation. Why are you testing if an internal service is being started and is polling kafka for messages? This has nothing to do with ktor.
If you really want to do an integration test on this service rather:
do not start ktor
start kafka
start your Kafka polling service
send your message
do your verification
If you really want to check that your kafka service is started by your ktor application just do a verification that some "startKafkaConsumer" function was called in your start up. This should most likely be done on some injected mock of your consuming service.

Related

Bytebuddy agent for REST services

I want to setup instrumentation for bytebuddy for my REST services. Jax-rs services is running on remote host. I want to run a separate process on some other machine which will attach a java agent to the running service and should do the following.
Print something whenever it enters an actual rest method.
Send some other response like 401,402 .... without hitting REST service
Trace flow on each method enter and exit so as to come up with automatic flow chart based on a code flow for any user activity
I am not sure how to setup bytebuddy for the above use cases. Any pointers please ...

WCF Service calling a Java Service. Should it be async?

I currently have an application whereby a user makes a request to my ASP.Net UI, which in turns makes an async call into a WCF service to keep the ASP.net thread free.
The WCF service is essentially a translation module, sat on the restricted network, and bridging the DMZ to the Trusted network. This service calls out to a Java service, which will hit the DB, and I currently have this as a synchronous call.
My WCF services are setup with ConcurrencyMode.Single, and InstanceContextMode.PerCall, so I guess that when I run out of threads on the service host, I'll start backing up requests because the UI is calling async, allowing the user to send multiple requests.
Should I be calling the Java service as an async task, like I do in the UI?
async-await is almost always a good idea. It doesn't really matter which IO you are using (in this case a network call to a java service) as long as it can benefit from treating it as an asynchronous by releasing threads while waiting for IO to complete.
Of course you would get a bigger benefit by also making the java service fully asynchronous, but it isn't necessary.

Test REST WCF Endpoints

This is a different question than my previous post on testing WCF.
This time after I've created my service, I want to test it not via WSDL, but I want to send an ajax request using $.ajax via jQuery.
I'm not sure how to wire up the service so that it's ready to recieve requests. Do I need the service setup and running in IIS? Or is there a way I can run the WCF project to run the service and then somehow in my NUnit Unit test create the jquery to make an HttpRequest..meaning would it know that the service is up and running? how?
You can host your service in the IIS, As a windows service or in a console app. To me it sounds wrong to call the service in a Unit test. Normally you mock out all external dependencies in a Unit test.

WCF Server waiting for return before proceeding

Here is what I would like to do.
1. Service hosted in WCF
2. Client calls asking for a payload of messages
3. Service returns payload of messages and waits for client to respond
3.A. Client returns 200 (OK) status or something confirming messages received.
3.B. Client returns bad error status stating to not delete the messages on server.
4. Depending on 3.A or 3.B Service will take appropriate action.
I would like to do this by doing something like extending IDispatcher and writing extension methods. VS creating another service and having the client call that service to signal which messages it received. Unless that's best practices.
Thanks in advanced.
If acting on HTTP status codes is a requirement then WCF is probably not what you want to use. WCF was created to be able to write transport independent code so the bindings could be changed purely through configuration; no code changes required. The HTTP request handling is buried so deeply into HTTP-based bindings that you're better off using something like the OpenRasta framework to implement your HTTP (REST) style service. It is a very HTTP request aware framework.
Otherwise, look at this wsDualHttpBinding intro to accomplish something similar through the application API level.

WCF MSMQ Unit testing

I have created a custom msmq service with WCF, which uses a custom binding as it needs to do some custom logic at the channel layer, where it calls another wcf service. The service is going to be a core pience of functionality for our systems for at the the next few years. i want to do what I can to make sure the service is robust, but, i'm not sure where to start. Ie testing the response, should I create a mock queue object? how do I test the service is calling another service ?
Best way I have found to unit test msmq services is to actually unit test the service implementation, then do an integration test using msmq with a mock repository. To see if writes are working.