File
Blob: src/workerd/server/tests/python/worker-entrypoint/worker.py
| 1 | # Copyright (c) 2023 Cloudflare, Inc. |
| 2 | # Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | # https://opensource.org/licenses/Apache-2.0 |
| 4 | |
| 5 | import os |
| 6 | |
| 7 | from workers import WorkerEntrypoint |
| 8 | |
| 9 | |
| 10 | class WorkerEntrypointExample(WorkerEntrypoint): |
| 11 | def __init__(self, state, env): |
| 12 | self.state = state |
| 13 | self.counter = 0 |
| 14 | |
| 15 | async def rpc_trigger_func(self): |
| 16 | # Test that entropy works in WorkerEntrypoint... |
| 17 | os.urandom(4) |
| 18 | self.counter += 1 |
| 19 | return f"hello from python {self.counter}" |
| 20 | |
| 21 | |
| 22 | async def test(ctrl, env, ctx): |
| 23 | first_resp = await env.entrypoint.rpc_trigger_func() |
| 24 | assert first_resp == "hello from python 1" |
| 25 | |
| 26 | second_resp = await env.entrypoint.rpc_trigger_func() |
| 27 | # We expect the counter to stay the same since this isn't a durable object. |
| 28 | assert second_resp == "hello from python 1" |