|
13 | 13 | from test.support import warnings_helper |
14 | 14 | from test.support import asyncore |
15 | 15 | import array |
| 16 | +import contextlib |
16 | 17 | import re |
17 | 18 | import socket |
18 | 19 | import select |
@@ -2998,44 +2999,38 @@ def test_echo(self): |
2998 | 2999 | @unittest.skipUnless(support.Py_GIL_DISABLED, "test is only useful if the GIL is disabled") |
2999 | 3000 | def test_ssl_in_multiple_threads(self): |
3000 | 3001 | # See GH-124984: OpenSSL is not thread safe. |
| 3002 | + self.enterContext( |
| 3003 | + support.swap_item(globals(), 'USE_SAME_TEST_CONTEXT', True)) |
| 3004 | + self.enterContext( |
| 3005 | + support.swap_attr(self, 'subTest', |
| 3006 | + lambda *args, **kwargs: contextlib.nullcontext())) |
| 3007 | + warnings_filters = sys.flags.context_aware_warnings |
| 3008 | + funcs = ( |
| 3009 | + self.test_echo, |
| 3010 | + self.test_alpn_protocols, |
| 3011 | + self.test_getpeercert, |
| 3012 | + self.test_crl_check, |
| 3013 | + functools.partial( |
| 3014 | + self.test_check_hostname_idn, |
| 3015 | + warnings_filters=warnings_filters, |
| 3016 | + ), |
| 3017 | + self.test_wrong_cert_tls12, |
| 3018 | + self.test_wrong_cert_tls13, |
| 3019 | + ) |
| 3020 | + # Be careful with the number of threads here. |
| 3021 | + # Too many can result in failing tests. |
3001 | 3022 | threads = [] |
| 3023 | + for num in range(5): |
| 3024 | + for func in funcs: |
| 3025 | + threads.append(Thread(target=func)) |
3002 | 3026 |
|
3003 | | - warnings_filters = sys.flags.context_aware_warnings |
3004 | | - global USE_SAME_TEST_CONTEXT |
3005 | | - USE_SAME_TEST_CONTEXT = True |
3006 | | - try: |
3007 | | - for func in ( |
3008 | | - self.test_echo, |
3009 | | - self.test_alpn_protocols, |
3010 | | - self.test_getpeercert, |
3011 | | - self.test_crl_check, |
3012 | | - functools.partial( |
3013 | | - self.test_check_hostname_idn, |
3014 | | - warnings_filters=warnings_filters, |
3015 | | - ), |
3016 | | - self.test_wrong_cert_tls12, |
3017 | | - self.test_wrong_cert_tls13, |
3018 | | - ): |
3019 | | - # Be careful with the number of threads here. |
3020 | | - # Too many can result in failing tests. |
3021 | | - for num in range(5): |
3022 | | - with self.subTest(func=func, num=num): |
3023 | | - threads.append(Thread(target=func)) |
3024 | | - |
3025 | | - with threading_helper.catch_threading_exception() as cm: |
3026 | | - for thread in threads: |
3027 | | - with self.subTest(thread=thread): |
3028 | | - thread.start() |
3029 | | - |
3030 | | - for thread in threads: |
3031 | | - with self.subTest(thread=thread): |
3032 | | - thread.join() |
3033 | | - if cm.exc_value is not None: |
3034 | | - # Some threads can skip their test |
3035 | | - if not isinstance(cm.exc_value, unittest.SkipTest): |
3036 | | - raise cm.exc_value |
3037 | | - finally: |
3038 | | - USE_SAME_TEST_CONTEXT = False |
| 3027 | + with threading_helper.catch_threading_exception() as cm: |
| 3028 | + with threading_helper.start_threads(threads): |
| 3029 | + pass |
| 3030 | + if cm.exc_value is not None: |
| 3031 | + # Some threads can skip their test |
| 3032 | + if not isinstance(cm.exc_value, unittest.SkipTest): |
| 3033 | + raise cm.exc_value |
3039 | 3034 |
|
3040 | 3035 | def test_getpeercert(self): |
3041 | 3036 | if support.verbose: |
|
0 commit comments