Use CountDownLatch instead of Thread.sleep()

... to wait for ApplicationContext to close in the 3 tests
that we needed to do so.

Fixes gh-664