How do you add tests for multi threaded support?
I have a Java service which now will execute in a batch mode. Multi threaded support is added to the service so for every batch request a thread pool will be dedicated to execute the batch. The question is how do I test this? I have functional tests that pass under the threaded version of the service but, somehow, I feel there must be an idiom for testing this.
Asked by: John599 | Posted: 23-01-2022
Answer 1
There really isn't a "good" way to do this. The best thing I can suggest would be TestNG, which allows you to annotate your test methods and cause them to be executed in n threads concurrently. For example:
@Test(invocationCount=10, threadPool=10)
public void testSomethingConcurrently() {
...
}
My TestNG knowledge is rusty at best, but AFAIK that should invoke the testSomethingConcurrently
method 10 times concurrently. This is a nice and declarative way to run multi-threaded tests against your code.
You can of course do something similar in JUnit by spawning threads manually in a loop, but that's insanely ugly and difficult to work with. I had to do this once for a project I was working on; those tests were a nightmare to deal with since their failures weren't always repeatable.
Concurrency testing is difficult and prone to frustration, due to its non-deterministic nature. This is part of why there is such a big push these days to use better concurrency abstractions, ones which are easier to "reason about" and convince one's self of correctness.
Answered by: Richard789 | Posted: 24-02-2022Answer 2
Usually the things that bring down multithreaded applications are issues of timing. I suspect that to be able to perform unit testing on the full multithreaded environment would require huge changes to the code base to do that.
What you may well be able to do, though, is to test the implementation of the thread pool in isolation.
By substituting the body of the threads with test code you should be able to construct your pathological conditions of locking and resource usage.
Then, unit test the functional elements in a single threaded environment, where you can ignore timing.
While this isn't perfect it does guarantee repeatability which is of great importance for your unit testing. (as alluded to in Daniel Spiewak's answer)
Answered by: Aida370 | Posted: 24-02-2022Answer 3
I used
@Test(threadPoolSize = 100, invocationCount = 100)
@DataProvider(parallel = true)
executed 100 threads in parallel 100 times.
Answered by: Patrick114 | Posted: 24-02-2022Answer 4
I agree with Daniel, concurrency testing is indeed very difficult.
I don't have a solution for concurrency testing, but I will tell you what I do when I want to test code involving multi-threading. Most of my testing is done using JUnit and JMock. Since JMock doesn't work well with multiple threads, I use a extension to JMock that provides synchronous versions of Executor and ScheduledExecutorService. These allow me to test code targeted to be run by multiple threads in a single thread, where I'm also able to control the execution flow. As I said before, this doesn't test concurrency. It only checks the behavior of my code in a single threaded way, but it reduces the amount of errors I get when I switch to the multi thread executors.
Anyway I recommend the use of the new Java concurrency API. It makes things a lot more easy.
Answered by: Elise713 | Posted: 24-02-2022Similar questions
java - Running a threaded class as a daemon
I am writing a multi-threaded solution in Java to connect two systems, A & B. System A is completly serial, not threaded, and will supply data to send to system B. System B accepts data from multiple sources asynchronously all at the same time.
I am using ThreadPoolExecutor to manage the threads. I am using a static singleton instance of a class, TP, that wraps around ThreadPoolExecutor (which is also static...
editor - Threaded drawing onto a canvas in Java SWT
I've been working quite a bit on something I can only describe as a "threaded canvas" for some time now. In a moment I'll describe what I have, but since I'm really open to new ideas, existing solutions or a completely fresh start, I'll formulate the problem.
The canvas is intended to display genetic information (although the specific purpose is somewhat irrelevant). As a conventional text editor, this genetic code...
java - What design pattern to use for a threaded queue
I have a very complex system (100+ threads) which need to send email without blocking. My solution to the problem was to implement a class called EmailQueueSender which is started at the beginning of execution and has a ScheduledExecutorService which looks at an internal queue every 500ms and if size()>0 it empties it.
While this is going on there's a synchronized static method called
java - multi threaded servlet; single threaded ejb
In a traditional n tier web app with servlets for web layer and ejbs(2.0) for biz layer, what is the rationale behind making the servlet model multi threaded and the ejb model single threaded?
i.e there is only 1 servlet instance for all requests, but for ejbs, for each request, there is a new bean instance assigned from the bean pool.
java - Multi threaded insert using ORM?
I have one application where "persisting to database" is consuming 85% time of the entire application flow.
I was thinking of using multiple threads to do the insert because inserts are mostly independent here. Is there any way to achieve multi threaded insert using any of JPA implementation ? Or is it worth doing the mutli threaded insert, from improving the performance perspective ?
Note: Ins...
java - Where's the error in this piece of threaded code?
I created a class which is used as a cache provider. It uses a Map, timestamped map entries and it spawns a Thread which performs cleanup every so often. This class is used in a web application. This web application had a problem where POST would take 30 seconds. I traced the problem to this cache class, eliminating it resolves the problem.
I have tried my best to find the error in this class but I can't. Please he...
sockets - Threaded Java server with inner class and final variable
I've written the following code to implement a Threaded Server:
ServerSocket passiveSocket = new ServerSocket(port, maxConnections);
while(true){
final Socket socket = passiveSocket.accept();
new Thread(new Runnable() {
public void run() {
//access socket as needed to communicate. E.g.:
PrintWriter writer = new PrintWriter(socket.getOutputStream());
...
java - Why is my threaded program only using one CPU?
I have a program that performs a long-time computations, so I want to speed up its performance. So I tried to launch 3 threads at the moment, but java.exe still occupies 25% of CPU usage (so, only one CPU is used), and it's remains even if I try to use .setPriority(Thread.MAX_PRIORITY); and set priority of java.exe at realtime (24). I tried to use RealtimeThread but seems...
java - How can I get my threaded program to print specific output
I am having problem dealing with synchronization java threads, applying wait and notify..
I want to figure out how could I implement these in a program where I can print out the answer alternately.. for example person1 will count numbers 1-5 as well as person2, the output should be like this.
person1 count 1
person2 count 1
person1 count 2
person2 count 2
person1 count 3
person2 count 3
person1 coun...
java - Testing a threaded program
I have developed a solution for Readers-Writers problem using thread.
I have one monitor class and one Reader and one Writer class.Reader and writer class extend thread.
Now I am testing the code like this:
public static void main(String[] args) {
ReadersWriters controller = new ReadersWriters();
Reader r0...
Still can't find your answer? Check out these amazing Java communities for help...
Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)