https://github.com/python/cpython/commit/08673d8a51cabc1f8a628a0c8e3be375523f76ff
commit: 08673d8a51cabc1f8a628a0c8e3be375523f76ff
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2025-10-07T18:23:58Z
summary:

[3.14] gh-116738: add multi-threaded tests for `resource` module on 
free-threading builds (GH-138504) (#138793)

gh-116738: add multi-threaded tests for `resource` module on free-threading 
builds (GH-138504)
(cherry picked from commit 32e1e0699ffda8ec1dd5a0eb178b052352ab7d31)

Co-authored-by: Alper <[email protected]>
Co-authored-by: Kumar Aditya <[email protected]>

files:
A Lib/test/test_free_threading/test_resource.py

diff --git a/Lib/test/test_free_threading/test_resource.py 
b/Lib/test/test_free_threading/test_resource.py
new file mode 100644
index 00000000000000..ecd0e535c442b4
--- /dev/null
+++ b/Lib/test/test_free_threading/test_resource.py
@@ -0,0 +1,41 @@
+import unittest
+from test.support import import_helper, threading_helper
+
+resource = import_helper.import_module("resource")
+
+
+NTHREADS = 10
+LOOP_PER_THREAD = 1000
+
+
+@threading_helper.requires_working_threading()
+class ResourceTest(unittest.TestCase):
+    @unittest.skipUnless(hasattr(resource, "getrusage"), "needs getrusage")
+    @unittest.skipUnless(
+        hasattr(resource, "RUSAGE_THREAD"), "needs RUSAGE_THREAD"
+    )
+    def test_getrusage(self):
+        ru_utime_lst = []
+
+        def dummy_work(ru_utime_lst):
+            for _ in range(LOOP_PER_THREAD):
+                pass
+
+            usage_process = resource.getrusage(resource.RUSAGE_SELF)
+            usage_thread = resource.getrusage(resource.RUSAGE_THREAD)
+            # Process user time should be greater than thread user time
+            self.assertGreater(usage_process.ru_utime, usage_thread.ru_utime)
+            ru_utime_lst.append(usage_thread.ru_utime)
+
+        threading_helper.run_concurrently(
+            worker_func=dummy_work, args=(ru_utime_lst,), nthreads=NTHREADS
+        )
+
+        usage_process = resource.getrusage(resource.RUSAGE_SELF)
+        self.assertEqual(len(ru_utime_lst), NTHREADS)
+        # Process user time should be greater than sum of all thread user times
+        self.assertGreater(usage_process.ru_utime, sum(ru_utime_lst))
+
+
+if __name__ == "__main__":
+    unittest.main()

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to