2022-07-08 16:47:18 +08:00
|
|
|
import time
|
|
|
|
|
|
|
|
|
2023-12-21 03:03:06 +08:00
|
|
|
def mg_sleep_and_assert(expected_value, function_to_retrieve_data, max_duration=20, time_between_attempt=0.2):
|
2022-07-08 16:47:18 +08:00
|
|
|
result = function_to_retrieve_data()
|
|
|
|
start_time = time.time()
|
|
|
|
while result != expected_value:
|
2023-12-21 03:03:06 +08:00
|
|
|
duration = time.time() - start_time
|
2022-07-08 16:47:18 +08:00
|
|
|
if duration > max_duration:
|
2022-08-09 17:29:55 +08:00
|
|
|
assert (
|
|
|
|
False
|
|
|
|
), f" mg_sleep_and_assert has tried for too long and did not get the expected result! Last result was: {result}"
|
2022-07-08 16:47:18 +08:00
|
|
|
|
|
|
|
time.sleep(time_between_attempt)
|
|
|
|
result = function_to_retrieve_data()
|
|
|
|
|
|
|
|
return result
|
2024-02-27 03:05:49 +08:00
|
|
|
|
|
|
|
|
2024-03-04 15:24:18 +08:00
|
|
|
def mg_sleep_and_assert_any_function(
|
|
|
|
expected_value, functions_to_retrieve_data, max_duration=20, time_between_attempt=0.2
|
|
|
|
):
|
|
|
|
result = [f() for f in functions_to_retrieve_data]
|
|
|
|
if any((x == expected_value for x in result)):
|
|
|
|
return result
|
|
|
|
start_time = time.time()
|
|
|
|
while result != expected_value:
|
|
|
|
duration = time.time() - start_time
|
|
|
|
if duration > max_duration:
|
|
|
|
assert (
|
|
|
|
False
|
|
|
|
), f" mg_sleep_and_assert has tried for too long and did not get the expected result! Last result was: {result}"
|
|
|
|
|
|
|
|
time.sleep(time_between_attempt)
|
|
|
|
result = [f() for f in functions_to_retrieve_data]
|
|
|
|
if any((x == expected_value for x in result)):
|
|
|
|
return result
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2024-02-27 03:05:49 +08:00
|
|
|
def mg_sleep_and_assert_collection(
|
|
|
|
expected_value, function_to_retrieve_data, max_duration=20, time_between_attempt=0.2
|
|
|
|
):
|
|
|
|
result = function_to_retrieve_data()
|
|
|
|
start_time = time.time()
|
|
|
|
while len(result) != len(expected_value) or any((x not in result for x in expected_value)):
|
|
|
|
duration = time.time() - start_time
|
|
|
|
if duration > max_duration:
|
|
|
|
assert (
|
|
|
|
False
|
|
|
|
), f" mg_sleep_and_assert has tried for too long and did not get the expected result! Last result was: {result}"
|
|
|
|
|
|
|
|
time.sleep(time_between_attempt)
|
|
|
|
result = function_to_retrieve_data()
|
|
|
|
|
|
|
|
return result
|