python-botocore/tests/functional/test_six_threading.py

62 lines
1.5 KiB
Python
Raw Permalink Normal View History

2017-07-10 09:39:11 +02:00
"""
Regression test for six issue #98 (https://github.com/benjaminp/six/issues/98)
"""
import sys
import threading
import time
from botocore.vendored import six
2022-05-26 00:10:07 +02:00
from tests import mock
2017-07-10 09:39:11 +02:00
_original_setattr = six.moves.__class__.__setattr__
def _wrapped_setattr(key, value):
# Monkey patch six.moves.__setattr__ to simulate
# a poorly-timed thread context switch
time.sleep(0.1)
return _original_setattr(six.moves, key, value)
def _reload_six():
# Issue #98 is caused by a race condition in six._LazyDescr.__get__
# which is only called once per moved module. Reload six so all the
# moved modules are reset.
2021-11-03 18:14:15 +01:00
import importlib
2022-05-26 00:10:07 +02:00
2021-11-03 18:14:15 +01:00
importlib.reload(six)
2017-07-10 09:39:11 +02:00
class _ExampleThread(threading.Thread):
def __init__(self):
2022-05-26 00:10:07 +02:00
super().__init__()
2017-07-10 09:39:11 +02:00
self.daemon = False
self.exc_info = None
def run(self):
try:
# Simulate use of six by
# botocore.configloader.raw_config_parse()
# Should raise AttributeError if six < 1.9.0
six.moves.configparser.RawConfigParser()
except Exception:
self.exc_info = sys.exc_info()
def test_six_thread_safety():
_reload_six()
2021-09-22 22:53:42 +02:00
with mock.patch(
'botocore.vendored.six.moves.__class__.__setattr__',
2022-05-26 00:10:07 +02:00
wraps=_wrapped_setattr,
2021-09-22 22:53:42 +02:00
):
2017-07-10 09:39:11 +02:00
threads = []
for i in range(2):
t = _ExampleThread()
threads.append(t)
t.start()
while threads:
t = threads.pop()
t.join()
if t.exc_info:
six.reraise(*t.exc_info)