Skip to content

Commit 69b2fa7

Browse files
test(utils): Make threading mocks answer only the thread under test
1 parent fbcdcbd commit 69b2fa7

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

‎tests/test_utils.py‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,13 +1180,19 @@ def target():
11801180
assert (thread.ident, thread.name) == results.get(timeout=1)
11811181

11821182

1183-
def get_current_thread_meta_patched(**patch_kwargs):
1183+
def get_current_thread_meta_patched(fake_thread):
11841184
results = Queue(maxsize=1)
11851185
patched = threading.Event()
11861186
other_caller_done = threading.Event()
1187+
real_current_thread = threading.current_thread
1188+
1189+
def current_thread():
1190+
if threading.get_ident() == thread.ident:
1191+
return fake_thread
1192+
return real_current_thread()
11871193

11881194
def target():
1189-
with mock.patch("threading.current_thread", **patch_kwargs):
1195+
with mock.patch("threading.current_thread", current_thread):
11901196
patched.set()
11911197
other_caller_done.wait()
11921198
results.put(get_current_thread_meta())
@@ -1202,31 +1208,29 @@ def target():
12021208
return results.get(timeout=1)
12031209

12041210

1205-
@pytest.mark.xfail(strict=True, raises=StopIteration)
12061211
def test_get_current_thread_meta_bad_running_thread():
12071212
main_thread = threading.main_thread()
12081213
assert (
12091214
main_thread.ident,
12101215
main_thread.name,
1211-
) == get_current_thread_meta_patched(side_effect=["fake thread"])
1216+
) == get_current_thread_meta_patched("fake thread")
12121217

12131218

1214-
@pytest.mark.xfail(strict=True, raises=StopIteration)
12151219
def test_get_current_thread_meta_main_thread():
12161220
main_thread = threading.main_thread()
12171221
# mock that somehow the current thread doesn't exist
12181222
assert (
12191223
main_thread.ident,
12201224
main_thread.name,
1221-
) == get_current_thread_meta_patched(side_effect=[None])
1225+
) == get_current_thread_meta_patched(None)
12221226

12231227

12241228
def test_get_current_thread_meta_failed_to_get_main_thread():
12251229
main_thread = threading.main_thread()
12261230
assert (
12271231
main_thread.ident,
12281232
main_thread.name,
1229-
) == get_current_thread_meta_patched(return_value="fake thread")
1233+
) == get_current_thread_meta_patched("fake thread")
12301234

12311235

12321236
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)