Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/workerd/server/tests/python/durable-object/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ def __init__(self, state, env):
self.storage = state.storage
self.alarm_triggered = False

# Test blockConcurrencyWhile in the constructor with a Python async callback.
# This is a common pattern for initializing DO state.
async def init_callback():
await self.storage.put("initialized", True)

self.ctx.blockConcurrencyWhile(init_callback)

async def fetch(self, request):
assert isinstance(request, Request)

Expand Down Expand Up @@ -55,6 +62,23 @@ async def args_method(self, arg):
def mutate_dict(self, my_dict):
my_dict["foo"] = 42

async def test_block_concurrency_while(self):
# Verify the constructor's blockConcurrencyWhile ran successfully
initialized = await self.storage.get("initialized")
assert initialized, f"Expected True but got {initialized}"

# Test blockConcurrencyWhile with a Python async callback that returns a value.
async def my_callback():
await self.storage.put("blocked", "yes")
return 42

result = await self.ctx.blockConcurrencyWhile(my_callback)
assert result == 42, f"Expected 42 but got {result}"
blocked = await self.storage.get("blocked")
assert blocked == "yes", f"Expected 'yes' but got {blocked}"

return True

async def test_self_call(self):
test_dict = dict()
test_dict["test"] = 1
Expand Down Expand Up @@ -89,6 +113,8 @@ async def test(self, ctrl):
arg_resp = await obj.args_method("test")
assert arg_resp == "value from python test"

assert await obj.test_block_concurrency_while()

assert await obj.test_self_call()

if pyodide.__version__ != "0.26.0a2":
Expand Down
Loading