Fixed a few thread safety issues in unit test.