def test_circuit_half_open_after_alive_threshold(self): temp = {"retried": 0} url = new_url() cb_threshold = Config.cb_fail_threshold() def _fake_new_conn(self): temp["retried"] += 1 raise ConnectTimeoutError(self, "", (self.host, self.timeout)) with CustomFailureMock(_fake_new_conn): # Open the circuit try: requests.get(url, max_tries=cb_threshold + 2) except ConnectionError: pass self.assertEqual(temp["retried"], cb_threshold) # reset the counter temp["retried"] = 0 # Make requests on open circuit till the alive_threshold is reached # and the circuit is half open for i in range(Config.cb_alive_threshold()): try: requests.get(url) except ConnectionError: self.assertEqual(temp["retried"], 0) else: raise Exception("Should have raised ConnectionError") try: requests.get(url) except ConnectionError: pass self.assertEqual(temp["retried"], 1)
def test_circuit_closed_on_success(self): """Steps: 1. Open the circuit with failed calls. Even thought he retry count is more that cb_failure_threshold, the actual call count only reaches the cb_failure_threshold and no request is made on the closed circuit 2. make the circuit half open by making failed calls on open circuit to reach cb_fail_threshold 3. Make a success call on half open circuit to close it 4. make failed requests on closed circuit and check the count to validate that the circuit was closed """ temp = {"retried": 0} url = new_url() cb_threshold = Config.cb_fail_threshold() def _fake_new_conn(self): temp["retried"] += 1 raise ConnectTimeoutError(self, "", (self.host, self.timeout)) @all_requests def success_handler(url, request): return "Success" with CustomFailureMock(_fake_new_conn): # Open the circuit try: requests.get(url, max_tries=cb_threshold + 2) except ConnectionError: pass self.assertEqual(temp["retried"], cb_threshold) # Make circuit half open for i in range(Config.cb_alive_threshold()): try: requests.get(url) except ConnectionError: pass else: raise Exception("Should have raised ConnectionError") with HTTMock(success_handler): requests.get(url) temp["retried"] = 0 cb_threshold = Config.cb_fail_threshold() with CustomFailureMock(_fake_new_conn): try: requests.get(url, max_tries=cb_threshold + 2) except ConnectionError: pass self.assertEqual(temp["retried"], cb_threshold)
def test_default_connect_timeout(self): now = int(time.time()) timeout = Config.connect_timeout() try: requests.get("http://google.com:488", max_tries=0) except ConnectionError: self.assertTrue(int(time.time()) - now >= timeout) else: raise Exception("Should have raised ConnectionError")
def test_default_retry_on_get_500(self): counter = {"retried": -1} max_tries = Config.max_tries() with CustomHTTPResponseMock(counter, 500): # Open the circuit try: requests.get(new_url()) except RetryError: pass self.assertEqual(counter["retried"], max_tries)
def test_retry_put_on_500(self): counter = {"retried": 0} cb_threshold = Config.cb_fail_threshold() with CustomHTTPResponseMock(counter, 500): # Open the circuit try: requests.put(new_url(), max_tries=cb_threshold + 2) except RetryError: pass self.assertEqual(counter["retried"], cb_threshold)
def test_default_circuitbreaking(self): temp = {"retried": 0} cb_threshold = Config.cb_fail_threshold() retries = cb_threshold + 2 url = new_url() def _fake_new_conn(self): temp["retried"] += 1 raise ConnectTimeoutError(self, "", (self.host, self.timeout)) with CustomFailureMock(_fake_new_conn): try: requests.get(url, max_tries=retries) except ConnectionError as e: self.assertTrue(str(e) != "Open Circuit") else: raise Exception("Should have raised ConnectionError") self.assertEqual(temp["retried"], cb_threshold) try: requests.get(url) except ConnectionError as e: self.assertTrue(str(e), "Open Circuit") else: raise Exception("Should have raised ConnectionError") time.sleep(Config.cb_delay()) temp["retried"] = 0 with CustomFailureMock(_fake_new_conn): try: requests.get(url, max_tries=retries) except ConnectionError as e: self.assertTrue(str(e) != "Open Circuit") else: raise Exception("Should have raised ConnectionError") self.assertEqual(temp["retried"], 1)
def test_custom_retry(self): temp = {"retried": -1} retries = random.randrange(1, Config.cb_fail_threshold(), 1) def _fake_new_conn(self): temp["retried"] += 1 raise ConnectTimeoutError(self, "", (self.host, self.timeout)) with CustomFailureMock(_fake_new_conn): try: requests.get(new_url(), max_tries=retries) except ConnectionError as e: self.assertTrue(str(e) != "Open Circuit") else: raise Exception("Should have raised ConnectionError") self.assertEqual(temp["retried"], retries)
def test_default_retry(self): temp = {"retried": -1} max_tries = Config.max_tries() def _fake_new_conn(self): temp["retried"] += 1 raise ConnectTimeoutError(self, "", (self.host, self.timeout)) with CustomFailureMock(_fake_new_conn): returnedError = None try: requests.get(new_url()) except ConnectionError: returnedError = ConnectionError self.assertEqual(returnedError, ConnectionError) self.assertEqual(temp["retried"], max_tries)