Gradle test hangs forever

I have suite of tests which successfully run and complete in maven. However with gradle I find that its just hung forever at the last step

 Building 58% > :MyProject:test > 674 tests completed, 2 failed

Any pointers towards debugging will be appreciated. I suspect its a thread that one of the tests start and doesn’t finish, Is there a way I can ensure threads spun by tests are killed at the end ?

as a first step I would try to figure out which test is causing your build to hang. Have a look at http://gradle.org/docs/current/dsl/org.gradle.api.tasks.testing.logging.TestLoggingContainer.html to configure an appropriate test logging to figure out the culprit test.
Gradle has no explicit support for killing threads in the test JVM.

Update : I am now led to believe the issue lies in either Gradle or JUnit framework and not with the test class itself. What is seemingly happening is that for the last test class (in a suite of 92 tests now), the method annotated with @AfterClass in the test’s super is never called. This causes some embedded Jetty servers in my tests not being stopped and hence threads go into waiting state. I will look into this more to see if I can find out the root cause

Phew. If I put a breakpoint in JUnit framework’s code (ParentRunner method that actually gets the Testclass’ method annoted with @AfterClass), and just that a little pause when the debugger stops the code for a while seems to be enough to do the trick tests don’t hang anymore.

I just for the sake of it tried to set values for forkEvery as 1 and maxParallelForks as 1 and it actually worked and tests run.

So what could possibly be the cause of this issue ? And what are the default values for forkEvery and maxParallelForks. I guess in my case the maxParallelForks need to be 1 as my mock servers that are spun by every test class uses the same port to bind.

The default for forkevery is null which means all tests are processed
in the same test vm.
The default for maxParallelForks is 1, which means that only one test
vm is created. Have you used different values for these settings when
the tests hang?

I tested unsuccessfully with a value of 3 for forkEvery - The tests started hanging then. I stuck to maxParallelForks as 1 for my mock http servers are started for every test (not ideal, but its what it is) and so they cannot run parallely.

so you’re saying you need forkEvery = 1 to succeed with your tests? it seems something is not cleaned up correctly then.

Not sure where the cleaning is not happening though, I atleast as of now, do not think its a fault with the test class , it seems somewhere in the code (either JUnit framework or gradle) is “waiting” on something before it can call the methods annotated @AfterClass on the test class. Java debugging is not helping as the call just proceeds when execution stops at the breakpoint. Will thread dumps help see if its an issue with gradle ?

It could be a bug in Gradle itself, for example this bug in JUnit5 runner simply freezes Gradle: https://github.com/gradle/gradle/issues/5737
Obtaining a stack dump of the Gradle JUnit runner process will therefore point towards the direction of what component is at fault: Gradle, JUnit runner, or your test being blocked.

we’ve the same problem and we’re not able to find the reason of why. the only thing is that we use gradle 6.0.1 and powermock. if i dump all thread only gradle threads are running and all of them are waiting:

  1. “Forward input” WAITING in java.util.concurrent.locks.LockSupport.park
  2. “DisconnectableInputStream source reader” RUNNABLE in java.io.FileInputStream.readBytes
  3. “pool-1-thread-1” TIMED_WAITING in java.util.concurrent.locks.LockSupport.parkNanos
  4. “main” RUNNABLE in org.gradle.internal.serialize.kryo.KryoBackedDecoder.readSmallInt

is there any progress with it?