Fix race conditions

Replace `sleep(1)` in unit tests with `wait_for_reader` and `wait_for_writer`.