Skip to content

Commit ebc8ebc

Browse files
committed
Add unit tests for helpers.cpu_count()
1 parent b2fcb23 commit ebc8ebc

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

tests/test_helpers.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,3 +503,42 @@ def test_python3_executable_err(mock_shutil, mock_sys, capsys):
503503
"[ERROR] No python3 in $PATH, components which use `${_python3}` will fail."
504504
in out
505505
)
506+
507+
508+
class MockOS:
509+
_cpu_count: Optional[int]
510+
_sched_affinity: list[int]
511+
512+
def __init__(self):
513+
self._cpu_count = None
514+
self._sched_affinitiy = []
515+
516+
def cpu_count(self) -> Optional[int]:
517+
return self._cpu_count
518+
519+
520+
class MockOSAff(MockOS):
521+
def sched_getaffinity(self, pid: int) -> list[int]:
522+
assert pid == 0
523+
return self._sched_affinity
524+
525+
526+
@pytest.mark.parametrize(
527+
"cpu_count,fallback", [(None, None), (None, 0), (4, None), (4, 0)]
528+
)
529+
@patch.object(helpers, "os", new_callable=MockOS)
530+
def test_cpu_count(mock_os, cpu_count, fallback):
531+
mock_os._cpu_count = cpu_count
532+
r = helpers.cpu_count(fallback=fallback)
533+
if cpu_count is None:
534+
assert r == fallback
535+
else:
536+
assert r == cpu_count
537+
538+
539+
@pytest.mark.parametrize("sched_affinity", [[0, 3], [0, 1, 2, 3, 4, 5, 6]])
540+
@patch.object(helpers, "os", new_callable=MockOSAff)
541+
def test_cpu_count_affinity(mock_os, sched_affinity):
542+
mock_os._cpu_count = 4
543+
mock_os._sched_affinity = sched_affinity
544+
assert helpers.cpu_count() == len(sched_affinity)

0 commit comments

Comments
 (0)