fix some bugs on detecting sync
add unit test