-
Notifications
You must be signed in to change notification settings - Fork 82
/
conftest.py
118 lines (90 loc) · 3.83 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Copyright (c) 2022 Graphcore Ltd. All rights reserved.
import functools
import os
import time
from pathlib import Path
from subprocess import run
import pytest
base_dir = Path(__file__).parent
def pytest_addoption(parser):
parser.addoption(
"--serial",
action="store_true",
help="only run serial marked tests",
)
def pytest_collection_modifyitems(config, items):
run_only_serial = config.getoption("--serial")
for item in items:
if "serial" in item.keywords and not run_only_serial:
item.add_marker(
pytest.mark.skip(
reason=("This test requires running serially." " Use option --serial to run only serial tests")
)
)
elif "serial" not in item.keywords and run_only_serial:
item.add_marker(pytest.mark.skip(reason="Only running serial tests."))
@pytest.fixture
def ipu_static_ops(scope="session"):
"""This function builds the ipu_static_ops
library for any tests that rely on it.
"""
build_path = Path(base_dir, "static_ops")
shared_libs = ["custom_grouped_gather_scatter.so"]
paths = [Path(build_path, f) for f in shared_libs]
# Use exclusive lockfile to avoid race conditions on the build:
lock_path = Path(build_path, ".ipu_static_ops.pytest.build.lockfile")
@ExecuteOncePerFS(lockfile=lock_path, file_list=paths, timeout=120, retries=20)
def build_ipustaticops():
run(["make", "clean"], cwd=build_path)
run(["make", "-j"], cwd=build_path)
build_ipustaticops()
class ExecuteOncePerFS:
"""Adds synchronization to the execution of a function so it only executes
once per file-system."""
def __init__(self, lockfile, file_list, exe_list=[], timeout=60, retries=10):
self.lockfile = lockfile
self.file_list = file_list
self.exe_list = exe_list
self.timeout = timeout
self.retries = retries
def __call__(self, fn):
@functools.wraps(fn)
def wrapped(*args, **kwargs):
# Race to become master process
result = None
try:
with open(self.lockfile, "x"):
# Master process executes function
result = fn(*args, **kwargs)
except FileExistsError:
pass
# Every process waits for files to be created
attempts = 0
sleep_time = self.timeout / self.retries
remaining_files = self.file_list[:]
remaining_exes = self.exe_list[:]
while attempts < self.retries:
remaining_files = [path for path in remaining_files if not os.path.exists(path)]
remaining_exes = [path for path in remaining_exes if not os.access(path, os.R_OK | os.X_OK)]
if len(remaining_files) == 0 and len(remaining_exes) == 0:
return result
time.sleep(sleep_time)
attempts += 1
# If we are here it means that we timed out...
raise RuntimeError(
f"Timed out waiting for {remaining_files} to be made" f" and/or {remaining_exes} to become executable."
)
return wrapped
def build_global_dependencies():
# Build globally used objects objects
build_path = Path(base_dir).joinpath("data_utils/feature_generation")
shared_libs = ["path_algorithms.so"]
paths = [Path(build_path, f) for f in shared_libs]
# Use exclusive lockfile to avoid race conditions on the build:
lock_path = Path(build_path, ".path_algorithms.pytest.build.lockfile")
@ExecuteOncePerFS(lockfile=lock_path, file_list=paths, timeout=120, retries=20)
def build_path_algorithms():
run(["make", "clean"], cwd=build_path)
run(["make", "-j"], cwd=build_path)
build_path_algorithms()
build_global_dependencies()