I want to run jUnit test cases in a java program and to stop the run after a timeout. To execute the tests I use the run(Request) method of JUnitCore.
The test classes contain methods which take a lot of time and/or do not terminate. Thus, I want to be able to stop the execution.
However, jUnit's pleaseStop flag is only taken in account after a completed test case, the methods can't be interrupted and the content (e. g. used IO resources which could be closed) is not known. Furthermore, I would like to avoid using a process.
Is there any other way to stop the execution of a single test case?
Would it be ok to use Thread.suspend() under the condition that I don't share any resources among threads?
(I've read Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?)
Would it be ok to use Thread.suspend() under the condition that I don't share any resources among threads?
I would say that for tests it should be fine to use the deprecated Thread.suspend()
method. I think it is important to reiterate the reasons why it is deprecated. You may suspend threads while they are holding locks which may dramatically alter how your code operates and even cause deadlocks.
If possible, I would consider using other mechanisms to achieve the same behavior.
To quote from the javadocs:
Deprecated. This method has been deprecated, as it is inherently deadlock-prone. If the target thread holds a lock on the monitor protecting a critical system resource when it is suspended, no thread can access this resource until the target thread is resumed. If the thread that would resume the target thread attempts to lock this monitor prior to calling resume, deadlock results. Such deadlocks typically manifest themselves as "frozen" processes. For more information, see Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?.