I am creating a test suite to run functional tests for an application that my team is developing. The functional tests are written by individual developers, and I observed that some testcases are throwing a OutOfMemoryError due to various reasons. This keeps happening intermittently.
To handle this situation I want to catch OutOfMemoryError in my test suite. I am hoping that catching this error would allow me to run subsequent testcases without having to quit the application all together. But I came across a post saying Out Of Memory Error is thrown after garbage collection is complete. In that case, Can I do something in my catch block of OutOfMemoryError to cleanup/re-instate JVM and then continue the execution?
Map<String, String> testcaseStatuses = new HashMap<String, String>();
String testcaseStatus = "FAIL";
for(Testcase testcase : testcases)
{
try
{
runTestcase(testcase);
testcaseStatus = "SUCCESS";
}
catch(OutOfMemoryError e)
{
logError(e);
testcaseStatus = "SYSTEM_ERROR";
// Do something creative to re-initialize JVM
}
catch (Exception e)
{
logError("Testcase failed with exception : " + e);
}
finally
{
testcaseStatuses.put(testcase.getName(), testcaseStatus);
}
}
// Do something with testcaseStatuses
I wouldn't want to stop running the remaining testcases if the first one fails. Is this something feasible?
NOTE: I did come across several posts here on SO, on using
-XX:OnOutOfMemoryError="New script to run"
This is something I am considering for the moment. But in order to follow this path, I would have to re-design the entire way I am executing the testcases which I want to avoid If I can.
Thanks in advance.
-
3Please. Please. Please. With sugar on top. Do the right thing, and fix the cause for the OutOfMemory errors. You are trying to tame chaos here.Gimby– Gimby2019年03月26日 15:06:24 +00:00Commented Mar 26, 2019 at 15:06
-
I am not resposible for the testcases. I am only responsible for running the testcases. My concern is that, if five developers come up with five testcases and the first testcase fails with Out of Memory Error, I don't want the other 4 to wait until the first testcase is fixed, to see if their testcases would pass.Sree Sake– Sree Sake2019年03月26日 15:13:28 +00:00Commented Mar 26, 2019 at 15:13
-
1I am curious for an answer to this. In a perfect world, @Gimby would have the whole answer. But often, you get what you get in our industry. Maybe you don't control all code involved. Maybe you don't have time to find all the memory problems. Maybe you figure this will happen again, and want to not break your test run next time too. I think your question has merit. I hope someone can provide an solution. All I can see to do is store the point where tests failed, and then restart your test and skip forward to that point and resume.CryptoFool– CryptoFool2019年03月26日 15:16:06 +00:00Commented Mar 26, 2019 at 15:16
-
2...trying to save a JVM that has run out of memory seems questionable to me.CryptoFool– CryptoFool2019年03月26日 15:17:20 +00:00Commented Mar 26, 2019 at 15:17
1 Answer 1
Impossible from within the JVM that ran out of memory.
Of course in theory you should fix the cause(s) of those OutOfMemoryErrors, or make sure that those who are responsible fix them. However given the real life scenario that you are in, I'd suggest redesigning your test application to encapsulate each test in its own JVM, e.g. like so:
Create one application that can execute the tests, but only ever one at a time, chosen by a runtime argument.
Create an application that runs the former application in a separate process, once for each test. Then use console output, logging data and/or the process return value to determine the state of the test, including if it ran out of memory.