summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_os.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_os.py')
-rw-r--r--Lib/test/test_os.py36
1 files changed, 32 insertions, 4 deletions
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index 66aece2..c1a78a7 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -3996,14 +3996,42 @@ class OSErrorTests(unittest.TestCase):
self.fail(f"No exception thrown by {func}")
class CPUCountTests(unittest.TestCase):
+ def check_cpu_count(self, cpus):
+ if cpus is None:
+ self.skipTest("Could not determine the number of CPUs")
+
+ self.assertIsInstance(cpus, int)
+ self.assertGreater(cpus, 0)
+
def test_cpu_count(self):
cpus = os.cpu_count()
- if cpus is not None:
- self.assertIsInstance(cpus, int)
- self.assertGreater(cpus, 0)
- else:
+ self.check_cpu_count(cpus)
+
+ def test_process_cpu_count(self):
+ cpus = os.process_cpu_count()
+ self.assertLessEqual(cpus, os.cpu_count())
+ self.check_cpu_count(cpus)
+
+ @unittest.skipUnless(hasattr(os, 'sched_setaffinity'),
+ "don't have sched affinity support")
+ def test_process_cpu_count_affinity(self):
+ ncpu = os.cpu_count()
+ if ncpu is None:
self.skipTest("Could not determine the number of CPUs")
+ # Disable one CPU
+ mask = os.sched_getaffinity(0)
+ if len(mask) <= 1:
+ self.skipTest(f"sched_getaffinity() returns less than "
+ f"2 CPUs: {sorted(mask)}")
+ self.addCleanup(os.sched_setaffinity, 0, list(mask))
+ mask.pop()
+ os.sched_setaffinity(0, mask)
+
+ # test process_cpu_count()
+ affinity = os.process_cpu_count()
+ self.assertEqual(affinity, ncpu - 1)
+
# FD inheritance check is only useful for systems with process support.
@support.requires_subprocess()