File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1
1
import asyncio
2
2
import gc
3
3
import threading
4
+ from threading import Thread
4
5
5
6
import pytest
6
7
@@ -338,3 +339,39 @@ async def async_function():
338
339
# inner value was set inside a new async context, meaning that
339
340
# we do not see it, as context vars don't propagate up the stack
340
341
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
You can’t perform that action at this time.
0 commit comments