I understand that RestTempalte is thread-safe once constructed, which is also said in this post on StackOverflow Is RestTemplate thread safe?
But I'd like to be able to test that this is indeed thread-safe, I have no idea how to go about it seeing as the restTempate is going to be used to make the call to a service and I do not have control over the service it is going to call, so I cannot stall a request and then have another one sent in at the same time to test it. What are the other options I can look at?
I just would like to have 2 calls using the singleton restTemplate instance and be successful. The requirement to do this is because I want this to be demonstratable. Are there any inbuilt RestTemplate utility methods I can make use of to demonstrate this?
First of all it is not necessary to call the actual service. You can write your own service and call it simultaneously and stall it or whatever your plan is.
Second - it doesn't make sense to test code outside of your code base. RestTemplate
is thread-safe - no need to prove that.
That being said - you can create a bunch of threads, inside of which you invoke the same calls to your single restTemplate instance. You can sleep the threads for some seconds to be "sure" that the restTemplates call will be fired simultaneously.
Go one abstraction level higher and use ExecutorService
(e.g. Executors.newFixedThreadPool(N);
) and submit callables to it (that call the same restTemplate instance).
Some external to JDK options are JMeter and concurrent-junit.