improve race condition check and test cases