Rewrite test_cursor_close to remove race