Fix random test failures by waiting for threads to finish.