Retry a code block for several times until a test function succeeds