Skip to content

Commit

Permalink
Reproduce issue #473
Browse files Browse the repository at this point in the history
  • Loading branch information
spanezz committed Oct 6, 2024
1 parent a916061 commit 4ac25d2
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tests/test_local.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import gc
import threading
from threading import Thread

import pytest

Expand Down Expand Up @@ -338,3 +339,39 @@ async def async_function():
# inner value was set inside a new async context, meaning that
# we do not see it, as context vars don't propagate up the stack
assert not hasattr(test_local_not_tc, "test_value")


def test_visibility_thread_asgiref() -> None:
"""Check visibility with subthreads."""
test_local = Local()
test_local.value = 0

def _test() -> None:
# Local() is cleared when changing thread
assert not hasattr(test_local, "value")
setattr(test_local, "value", 1)
assert test_local.value == 1

thread = Thread(target=_test)
thread.start()
thread.join()

assert test_local.value == 0


@pytest.mark.asyncio
async def test_visibility_task() -> None:
"""Check visibility with asyncio tasks."""
test_local = Local()
test_local.value = 0

async def _test() -> None:
# Local is inherited when changing task
assert test_local.value == 0
test_local.value = 1
assert test_local.value == 1

await asyncio.create_task(_test())

# Changes should not leak to the caller
assert test_local.value == 0

0 comments on commit 4ac25d2

Please sign in to comment.