MCPcopy Create free account
hub / github.com/ccxt/ccxt / test_safe

Method test_safe

python/ccxt/test/tests_sync.py:278–350  ·  view source on GitHub ↗
(self, method_name, exchange, args=[], is_public=False)

Source from the content-addressed store, hash-verified

276 return final_skips
277
278 def test_safe(self, method_name, exchange, args=[], is_public=False):
279 # `testSafe` method does not throw an exception, instead mutes it. The reason we
280 # mute the thrown exceptions here is because we don't want to stop the whole
281 # tests queue if any single test-method fails. Instead, they are echoed with
282 # formatted message "[TEST_FAILURE] ..." and that output is then regex-matched by
283 # run-tests.js, so the exceptions are still printed out to console from there.
284 max_retries = 3
285 args_stringified = exchange.json(args) # args.join() breaks when we provide a list of symbols or multidimensional array; "args.toString()" breaks bcz of "array to string conversion"
286 for i in range(0, max_retries):
287 try:
288 self.test_method(method_name, exchange, args, is_public)
289 return True
290 except Exception as ex:
291 e = get_root_exception(ex)
292 is_load_markets = (method_name == 'loadMarkets')
293 is_auth_error = (isinstance(e, AuthenticationError))
294 is_not_supported = (isinstance(e, NotSupported))
295 is_operation_failed = (isinstance(e, OperationFailed)) # includes "DDoSProtection", "RateLimitExceeded", "RequestTimeout", "ExchangeNotAvailable", "OperationFailed", "InvalidNonce", ...
296 last_url_msg = '' if self.ws_tests else ' (Last url: ' + exchange.last_request_url + ' )'
297 if is_operation_failed:
298 # if last retry was gone with same `tempFailure` error, then let's eventually return false
299 if i == max_retries - 1:
300 is_on_maintenance = (isinstance(e, OnMaintenance))
301 is_exchange_not_available = (isinstance(e, ExchangeNotAvailable))
302 should_fail = None
303 ret_success = None
304 if is_load_markets:
305 # if "loadMarkets" does not succeed, we must return "false" to caller method, to stop tests continual
306 ret_success = False
307 # we might not break exchange tests, if exchange is on maintenance at this moment
308 if is_on_maintenance:
309 should_fail = False
310 else:
311 should_fail = True
312 else:
313 # for any other method tests:
314 if is_exchange_not_available and not is_on_maintenance:
315 # break exchange tests if "ExchangeNotAvailable" exception is thrown, but it's not maintenance
316 should_fail = True
317 ret_success = False
318 else:
319 # in all other cases of OperationFailed, show Warning, but don't mark test as failed
320 should_fail = False
321 ret_success = True
322 # output the message
323 fail_type = '[TEST_FAILURE]' if should_fail else '[TEST_WARNING]'
324 dump(fail_type, exchange.id, method_name, args_stringified, last_url_msg, 'Method could not be tested due to a repeated Network/Availability issues', ' | ', exception_message(e))
325 return ret_success
326 else:
327 # wait and retry again
328 # (increase wait time on every retry)
329 exchange.sleep((i + 1) * 1000)
330 else:
331 # if it's loadMarkets, then fail test, because it's mandatory for tests
332 if is_load_markets:
333 dump('[TEST_FAILURE]', exchange.id, method_name, args_stringified, last_url_msg, 'Exchange can not load markets', exception_message(e))
334 return False
335 # if the specific arguments to the test method throws "NotSupported" exception

Callers 2

run_testsMethod · 0.95
load_exchangeMethod · 0.95

Calls 7

test_methodMethod · 0.95
get_root_exceptionFunction · 0.90
dumpFunction · 0.90
exception_messageFunction · 0.90
rangeFunction · 0.50
jsonMethod · 0.45
sleepMethod · 0.45

Tested by

no test coverage detected