From cec4bf0b0eac7e39cecd8f3e7882563062903d0f Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Mon, 25 Oct 2021 12:28:51 -0700 Subject: [PATCH] Retry upon test failure if in Windows (#468) --- src/julia/tests/test_core.py | 7 +++++++ src/julia/tests/test_libjulia.py | 6 ++++++ src/julia/tests/test_utils.py | 17 ++++++++++++++++ src/julia/tests/utils.py | 34 ++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+) diff --git a/src/julia/tests/test_core.py b/src/julia/tests/test_core.py index 37e7ba5b..41fdc4f0 100644 --- a/src/julia/tests/test_core.py +++ b/src/julia/tests/test_core.py @@ -11,6 +11,8 @@ from julia import JuliaError from julia.core import jl_name, py_name +from .utils import retry_failing_if_windows + python_version = sys.version_info @@ -155,6 +157,11 @@ def test_module_dir(julia): @pytest.mark.pyjulia__using_default_setup @pytest.mark.julia def test_import_without_setup(): + check_import_without_setup() + + +@retry_failing_if_windows +def check_import_without_setup(): command = [sys.executable, "-c", "from julia import Base"] print("Executing:", *command) subprocess.check_call(command) diff --git a/src/julia/tests/test_libjulia.py b/src/julia/tests/test_libjulia.py index 67d1e083..c915f9b1 100644 --- a/src/julia/tests/test_libjulia.py +++ b/src/julia/tests/test_libjulia.py @@ -1,6 +1,7 @@ import pytest from julia.core import JuliaInfo +from julia.tests.utils import retry_failing_if_windows from .test_compatible_exe import runcode @@ -10,6 +11,11 @@ @pytest.mark.skipif("juliainfo.version_info < (0, 7)") @pytest.mark.julia def test_compiled_modules_no(): + check_compiled_modules_no() + + +@retry_failing_if_windows +def check_compiled_modules_no(): runcode( """ from julia.core import Julia diff --git a/src/julia/tests/test_utils.py b/src/julia/tests/test_utils.py index 5132d27c..ad76e33f 100644 --- a/src/julia/tests/test_utils.py +++ b/src/julia/tests/test_utils.py @@ -9,6 +9,7 @@ from julia.core import UnsupportedPythonError from .test_compatible_exe import runcode +from .utils import _retry_on_failure, retry_failing_if_windows try: from types import SimpleNamespace @@ -35,9 +36,25 @@ def test_unsupported_python_error_dynamically_linked(): assert "have to match exactly" in str(err) +def test_retry_on_failure(): + c = [0] + + def f(): + c[0] += 1 + assert c[0] >= 2 + + _retry_on_failure(f) + assert c[0] == 2 + + @pytest.mark.pyjulia__using_default_setup @pytest.mark.julia def test_atexit(): + check_atexit() + + +@retry_failing_if_windows +def check_atexit(): proc = runcode( ''' import os diff --git a/src/julia/tests/utils.py b/src/julia/tests/utils.py index 29f06bc7..29fab924 100644 --- a/src/julia/tests/utils.py +++ b/src/julia/tests/utils.py @@ -1,5 +1,9 @@ +from __future__ import print_function + import os import sys +import traceback +from functools import wraps import pytest @@ -30,3 +34,33 @@ """ Tests that are known to fail in Windows in GitHub Actions. """ + + +def _retry_on_failure(*fargs, **kwargs): + f = fargs[0] + args = fargs[1:] + for i in range(10): + try: + return f(*args, **kwargs) + except Exception: + print() + print("{}-th try of {} failed".format(i, f)) + traceback.print_exc() + return f(*args, **kwargs) + + +def retry_failing_if_windows(test): + """ + Retry upon test failure if in Windows. + + This is an ugly workaround for occasional STATUS_ACCESS_VIOLATION failures + in Windows: https://github.com/JuliaPy/pyjulia/issues/462 + """ + if not is_windows: + return test + + @wraps(test) + def repeater(*args, **kwargs): + _retry_on_failure(test, *args, **kwargs) + + return repeater