def test_retry_set_on_connection_error(self): redis_storage = store.RedisStorage() redis_storage.db.connected = False redis_storage.db.set = MagicMock(side_effect=ConnectionError()) storage = store.Storage(redis_storage) self.assertEqual(storage.cache_set("key", "value"), None) self.assertEqual(redis_storage.db.set.call_count, store.Storage.MAX_RETRIES)
class MainHTTPHandler(BaseHTTPRequestHandler): router = {"method": method_handler} store = store.Storage(store.RedisStorage()) def get_request_id(self, headers): return headers.get('HTTP_X_REQUEST_ID', uuid.uuid4().hex) def do_POST(self): response, code = {}, OK context = {"request_id": self.get_request_id(self.headers)} request = None try: data_string = self.rfile.read(int(self.headers['Content-Length'])) data_string = data_string.decode("utf-8") request = json.loads(data_string) except: code = BAD_REQUEST if request: path = self.path.strip("/") logging.info("%s: %s %s" % (self.path, data_string, context["request_id"])) if path in self.router: try: response, code = self.router[path]({ "body": request, "headers": self.headers }, context, self.store) except Exception as e: logging.exception("Unexpected error: %s" % e) code = INTERNAL_ERROR else: code = NOT_FOUND self.send_response(code) self.send_header("Content-Type", "application/json") self.end_headers() if code not in ERRORS: r = {"response": response, "code": code} else: r = { "error": response or ERRORS.get(code, "Unknown Error"), "code": code } context.update(r) logging.info(context) self.wfile.write(bytes(json.dumps(r), "utf-8")) return
def setUp(self): self.context = {} self.headers = {} self.settings = store.Storage(store.RedisStorage())
def test_cache_set(self): redis_storage = store.RedisStorage() redis_storage.db.connected = False storage = store.Storage(redis_storage) self.assertEqual(storage.cache_set("key", "value"), True)
def test_cache_get(self): redis_storage = store.RedisStorage() redis_storage.db.connected = False storage = store.Storage(redis_storage) self.assertEqual(storage.cache_get("key"), None)
def test_get_raises_on_connection_error(self): redis_storage = store.RedisStorage() redis_storage.db.connected = False storage = store.Storage(redis_storage) with self.assertRaises(ConnectionError): storage.get("key")
def test_not_raises_on_connection_error(self): redis_storage = store.RedisStorage() redis_storage.db.connected = False storage = store.Storage(redis_storage) self.assertEqual(storage.cache_get("key"), None) self.assertEqual(storage.cache_set("key", "value"), None)