Skip to content

Commit

Permalink
Update mock to work on non-linux platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
mtreinish committed Jan 23, 2024
1 parent 7c4527a commit 4f55abe
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions test/python/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,22 @@ class TestUtil(QiskitTestCase):

def test_local_hardware_five_cpu_count(self):
"""Test cpu count is half when sched affinity is 5"""
with mock.patch.object(multiprocessing.os, "sched_getaffinity", return_value=set(range(5))):
with mock.patch.object(multiprocessing, "os"):
multiprocessing.os.sched_getaffinity = mock.MagicMock(return_value=set(range(5)))
result = multiprocessing.local_hardware_info()
self.assertEqual(2, result["cpus"])

def test_local_hardware_sixty_four_cpu_count(self):
"""Test cpu count is 32 when sched affinity is 64"""
with mock.patch.object(multiprocessing.os, "sched_getaffinity", return_value=set(range(5))):
with mock.patch.object(multiprocessing, "os"):
multiprocessing.os.sched_getaffinity = mock.MagicMock(return_value=set(range(64)))
result = multiprocessing.local_hardware_info()
self.assertEqual(2, result["cpus"])
self.assertEqual(32, result["cpus"])

def test_local_hardware_no_cpu_count(self):
"""Test cpu count fallback to 1 when true value can't be determined"""
with mock.patch.object(multiprocessing.os, "sched_getaffinity", return_value=set()):
with mock.patch.object(multiprocessing, "os"):
multiprocessing.os.sched_getaffinity = mock.MagicMock(return_value=set())
result = multiprocessing.local_hardware_info()
self.assertEqual(1, result["cpus"])

Expand All @@ -47,6 +50,14 @@ def test_local_hardware_no_sched_five_count(self):
result = multiprocessing.local_hardware_info()
self.assertEqual(2, result["cpus"])

def test_local_hardware_no_sched_five_count(self):
"""Test cpu cound if sched affinity method is missing and cpu count is 64."""
with mock.patch.object(multiprocessing, "os", spec=[]):
multiprocessing.os.cpu_count = mock.MagicMock(return_value=64)
del multiprocessing.os.sched_getaffinity
result = multiprocessing.local_hardware_info()
self.assertEqual(32, result["cpus"])

def test_local_hardware_no_sched_no_count(self):
"""Test cpu count fallback to 1 when no sched getaffinity available."""
with mock.patch.object(multiprocessing, "os", spec=[]):
Expand Down

0 comments on commit 4f55abe

Please sign in to comment.