Skip to content

Commit 8a116ea

Browse files
Implemented usage of CylcError
1 parent 69f014e commit 8a116ea

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

cylc/flow/scripts/profiler.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@
3131
from functools import partial
3232
from dataclasses import dataclass
3333

34-
from cylc.flow.network.client_factory import get_client
35-
from cylc.flow.option_parsers import CylcOptionParser as COP
34+
from cylc.flow.exceptions import CylcError
3635
from cylc.flow.terminal import cli_function
36+
from cylc.flow.option_parsers import CylcOptionParser as COP
37+
from cylc.flow.network.client_factory import get_client
3738

3839

3940
PID_REGEX = re.compile(r"([^:]*\d{6,}.*)")
@@ -117,6 +118,8 @@ def parse_memory_file(process: Process):
117118
if "total_rss" in line:
118119
return int(''.join(filter(str.isdigit, line)))
119120

121+
raise CylcError("Unable to find memory usage data")
122+
120123

121124
def parse_memory_allocated(process: Process) -> int:
122125
"""Open the memory stat file and copy the appropriate data"""
@@ -148,7 +151,7 @@ def parse_cpu_file(process: Process) -> int:
148151
# Cgroups v1 uses nanoseconds
149152
return int(line) // 1000000
150153

151-
raise ValueError("Unable to find cpu usage data")
154+
raise CylcError("Unable to find cpu usage data")
152155

153156

154157
def get_cgroup_version(cgroup_location: str, cgroup_name: str) -> int:
@@ -177,8 +180,8 @@ def get_cgroup_name():
177180
result = PID_REGEX.search(result).group()
178181
return result
179182
except FileNotFoundError as err:
180-
raise FileNotFoundError(
181-
'/proc/' + str(pid) + '/cgroup not found') from err
183+
raise CylcError(
184+
'/proc/' + str(pid) + '/cgroup not found') from None
182185

183186
except AttributeError as err:
184187
raise AttributeError("No cgroup found for process:", pid) from err
@@ -206,7 +209,7 @@ def get_cgroup_paths(location) -> Process:
206209
memory_allocated_path="",
207210
cgroup_version=cgroup_version,
208211
)
209-
raise ValueError("Unable to determine cgroup version")
212+
raise CylcError("Unable to determine cgroup version")
210213

211214

212215
def profile(_process: Process, delay, keep_looping=lambda: True):

tests/unit/scripts/test_profiler.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,13 @@
2727
Process)
2828
import pytest
2929
from unittest import mock
30+
from cylc.flow.exceptions import CylcError
3031

3132

3233
def test_stop_profiler(mocker, monkeypatch, tmpdir):
3334
monkeypatch.setenv('CYLC_WORKFLOW_ID', "test_value")
3435

35-
def mock_get_client(env_var, timeout=None):
36-
return True
37-
38-
class MockedClient():
36+
class MockedClient:
3937
def __init__(self, *a, **k):
4038
pass
4139

@@ -45,7 +43,7 @@ async def async_request(self, *a, **k):
4543
mocker.patch("cylc.flow.scripts.profiler.get_client", MockedClient)
4644

4745
mem_file = tmpdir.join("memory_file.txt")
48-
mem_file.write('1234')
46+
mem_file.write('total_rss 1234')
4947
cpu_file = tmpdir.join("cpu_file.txt")
5048
cpu_file.write('5678')
5149
mem_allocated_file = tmpdir.join("memory_allocated.txt")
@@ -251,7 +249,7 @@ def mock_os_pid():
251249
return 'The Thing That Should Not Be'
252250

253251
mocker.patch("os.getpid", mock_os_pid)
254-
with pytest.raises(FileNotFoundError):
252+
with pytest.raises(CylcError):
255253
get_cgroup_name()
256254

257255

@@ -297,7 +295,7 @@ def test_get_cgroup_paths(mocker):
297295
return_value='test_name')
298296
mocker.patch("cylc.flow.scripts.profiler.get_cgroup_version",
299297
return_value=3)
300-
with pytest.raises(ValueError):
298+
with pytest.raises(CylcError):
301299
get_cgroup_paths("test_location/")
302300

303301

0 commit comments

Comments
 (0)