Skip to content

Commit 18c06d4

Browse files
committed
Reproduce issue #473
1 parent 8e39bcc commit 18c06d4

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

tests/test_local.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22
import gc
33
import threading
4+
from threading import Thread
45

56
import pytest
67

@@ -338,3 +339,39 @@ async def async_function():
338339
# inner value was set inside a new async context, meaning that
339340
# we do not see it, as context vars don't propagate up the stack
340341
assert not hasattr(test_local_not_tc, "test_value")
342+
343+
344+
def test_visibility_thread_asgiref() -> None:
345+
"""Check visibility with subthreads."""
346+
test_local = Local()
347+
test_local.value = 0
348+
349+
def _test() -> None:
350+
# Local() is cleared when changing thread
351+
assert not hasattr(test_local, "value")
352+
setattr(test_local, "value", 1)
353+
assert test_local.value == 1
354+
355+
thread = Thread(target=_test)
356+
thread.start()
357+
thread.join()
358+
359+
assert test_local.value == 0
360+
361+
362+
@pytest.mark.asyncio
363+
async def test_visibility_task() -> None:
364+
"""Check visibility with asyncio tasks."""
365+
test_local = Local()
366+
test_local.value = 0
367+
368+
async def _test() -> None:
369+
# Local is inherited when changing task
370+
assert test_local.value == 0
371+
test_local.value = 1
372+
assert test_local.value == 1
373+
374+
await asyncio.create_task(_test())
375+
376+
# Changes should not leak to the caller
377+
assert test_local.value == 0

0 commit comments

Comments
 (0)