Made the test more robust.
Fixed race condition in test.