From 5136773babf9fcab23fe92bc7d02eb81ff4e7ea4 Mon Sep 17 00:00:00 2001 From: Mario Corchero Date: Mon, 3 Jul 2023 16:49:08 +0200 Subject: [PATCH 1/2] Mark `testthreadingmock.py` with threading_helper.requires_working_threading The test uses threading and it should declare that it uses it to prevent failures on archs where creating threads don't work. --- Lib/test/test_unittest/testmock/testthreadingmock.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/test/test_unittest/testmock/testthreadingmock.py b/Lib/test/test_unittest/testmock/testthreadingmock.py index 2b4dca3a4be179..bae5edd4a6bef1 100644 --- a/Lib/test/test_unittest/testmock/testthreadingmock.py +++ b/Lib/test/test_unittest/testmock/testthreadingmock.py @@ -2,9 +2,13 @@ import unittest import concurrent.futures +from test.support import threading_helper from unittest.mock import patch, ThreadingMock, call +threading_helper.requires_working_threading(module=True) + + class Something: def method_1(self): pass From 78d903158475d4d45f747201841b24a0c1c8a3a3 Mon Sep 17 00:00:00 2001 From: Mario Corchero Date: Mon, 3 Jul 2023 21:57:02 +0200 Subject: [PATCH 2/2] testthreadingmock: Reduce chance of race condition Add longer delays to reduce the change of a race conditions on the tests that validate short timeouts. --- .../test_unittest/testmock/testthreadingmock.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/Lib/test/test_unittest/testmock/testthreadingmock.py b/Lib/test/test_unittest/testmock/testthreadingmock.py index bae5edd4a6bef1..c6f179490d01f8 100644 --- a/Lib/test/test_unittest/testmock/testthreadingmock.py +++ b/Lib/test/test_unittest/testmock/testthreadingmock.py @@ -137,11 +137,9 @@ def test_wait_failed_with_timeout_override(self): with patch(f"{__name__}.Something", waitable_mock): something = Something() - self.run_async(something.method_1, delay=0.1) + self.run_async(something.method_1, delay=0.5) with self.assertRaises(AssertionError): something.method_1.wait_until_called(timeout=0.05) - with self.assertRaises(AssertionError): - something.method_1.wait_until_any_call_with(timeout=0.05) def test_wait_success_called_before(self): waitable_mock = self._make_mock() @@ -167,10 +165,10 @@ def test_wait_until_any_call_with_positional(self): with patch(f"{__name__}.Something", waitable_mock): something = Something() - self.run_async(something.method_1, 1, delay=0.1) - self.run_async(something.method_1, 2, delay=0.2) - self.run_async(something.method_1, 3, delay=0.3) + self.run_async(something.method_1, 1, delay=0.2) self.assertNotIn(call(1), something.method_1.mock_calls) + self.run_async(something.method_1, 2, delay=0.5) + self.run_async(something.method_1, 3, delay=0.6) something.method_1.wait_until_any_call_with(1) something.method_1.assert_called_with(1) @@ -186,10 +184,10 @@ def test_wait_until_any_call_with_keywords(self): with patch(f"{__name__}.Something", waitable_mock): something = Something() - self.run_async(something.method_1, a=1, delay=0.1) - self.run_async(something.method_1, b=2, delay=0.2) - self.run_async(something.method_1, c=3, delay=0.3) + self.run_async(something.method_1, a=1, delay=0.2) self.assertNotIn(call(a=1), something.method_1.mock_calls) + self.run_async(something.method_1, b=2, delay=0.5) + self.run_async(something.method_1, c=3, delay=0.6) something.method_1.wait_until_any_call_with(a=1) something.method_1.assert_called_with(a=1)