def test_become_leader_acquire_fail(m_redlock): m_redlock.return_value.acquire.return_value = False redis_cli = MockRedis() assert become_leader({'shadow': {}}, redis_cli) is False assert redis_cli.set_call_count == 0 assert redis_cli.get_parms[0][0] == 'tscached:shadow_server' assert redis_cli.get_call_count == 1
def handle_flushall(): """ Clears the entire Redis cache. Will cowardly refuse to act if the callee does not provide orly=yarly in the GET string. If we were truly RESTful this would not be method GET, but it's intended for ease of use. :return: 200 response, dict with key 'message' describing success/failure/cowardice. """ config = app.config['tscached'] redis_client = redis.StrictRedis(host=config['redis']['host'], port=config['redis']['port']) orly = request.args.get('orly') if orly == 'yarly': # Amusingly, we don't need to release the lock. It's implicit by virtue of deleting everything! lock = shadow.become_leader(config, redis_client) if lock: logging.info('Flushall acquired shadow lock') redis_client = redis.StrictRedis(host=config['redis']['host'], port=config['redis']['port']) ret = redis_client.flushall() message = 'Redis FLUSHALL executed; received response: %s' % ret else: message = 'Could not acquire shadow lock. Is shadow load taking place? (Or just try again.)' else: message = 'Cowardly refusing to act, add orly=yarly to execute Redis FLUSHALL.' return json.dumps({'message': message}), 200
def test_become_leader_catch_rediserror(m_redlock): m_redlock.return_value.acquire.return_value = False redis_cli = MockRedis() def newget(_): raise redis.exceptions.RedisError redis_cli.get = newget assert become_leader({'shadow': {}}, redis_cli) is False
def handle_flushall(): """ Clears the entire Redis cache. Will cowardly refuse to act if the callee does not provide orly=yarly in the GET string. If we were truly RESTful this would not be method GET, but it's intended for ease of use. :return: 200 response, dict with key 'message' describing success/failure/cowardice. """ config = app.config["tscached"] redis_client = redis.StrictRedis(host=config["redis"]["host"], port=config["redis"]["port"]) orly = request.args.get("orly") if orly == "yarly": # Amusingly, we don't need to release the lock. It's implicit by virtue of deleting everything! lock = shadow.become_leader(config, redis_client) if lock: logging.info("Flushall acquired shadow lock") redis_client = redis.StrictRedis(host=config["redis"]["host"], port=config["redis"]["port"]) ret = redis_client.flushall() message = "Redis FLUSHALL executed; received response: %s" % ret else: message = "Could not acquire shadow lock. Is shadow load taking place? (Or just try again.)" else: message = "Cowardly refusing to act, add orly=yarly to execute Redis FLUSHALL." return json.dumps({"message": message}), 200
def test_become_leader_catch_redlockerror(m_redlock): m_redlock.return_value.acquire.side_effect = redlock.RedLockError redis_cli = MockRedis() assert become_leader({'shadow': {}}, redis_cli) is False assert redis_cli.get_call_count == 0 assert redis_cli.set_call_count == 0
def test_become_leader_does_not_catch_other_error(m_redlock): m_redlock.return_value.acquire.side_effect = ValueError redis_cli = MockRedis() with pytest.raises(ValueError): become_leader({'shadow': {}}, redis_cli)