Skip to content

Commit

Permalink
Fix infrequent polling sample
Browse files Browse the repository at this point in the history
  • Loading branch information
dandavison committed Nov 21, 2024
1 parent c5bb4e4 commit ddd7b83
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 36 deletions.
9 changes: 1 addition & 8 deletions polling/frequent/activities.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import asyncio
from dataclasses import dataclass

from temporalio import activity

from polling.test_service import TestService


@dataclass
class ComposeGreetingInput:
greeting: str
name: str
from polling.test_service import ComposeGreetingInput, TestService


@activity.defn
Expand Down
10 changes: 1 addition & 9 deletions polling/infrequent/activities.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
from dataclasses import dataclass

from temporalio import activity

from polling.test_service import TestService


@dataclass
class ComposeGreetingInput:
greeting: str
name: str
from polling.test_service import ComposeGreetingInput, TestService


@activity.defn
Expand Down
10 changes: 2 additions & 8 deletions polling/periodic_sequence/activities.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
from dataclasses import dataclass
from typing import Any, NoReturn

from temporalio import activity


@dataclass
class ComposeGreetingInput:
greeting: str
name: str


@activity.defn
async def compose_greeting(input: ComposeGreetingInput) -> str:
async def compose_greeting(input: Any) -> NoReturn:
raise RuntimeError("Service is down")
6 changes: 2 additions & 4 deletions polling/periodic_sequence/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
from temporalio.exceptions import ActivityError

with workflow.unsafe.imports_passed_through():
from polling.periodic_sequence.activities import (
ComposeGreetingInput,
compose_greeting,
)
from polling.periodic_sequence.activities import compose_greeting
from polling.test_service import ComposeGreetingInput


@workflow.defn
Expand Down
23 changes: 16 additions & 7 deletions polling/test_service.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
from dataclasses import dataclass


@dataclass
class ComposeGreetingInput:
greeting: str
name: str


try_attempts = 0


class TestService:
def __init__(self):
self.try_attempts = 0
self.error_attempts = 5

async def get_service_result(self, input):
print(
f"Attempt {self.try_attempts}"
f" of {self.error_attempts} to invoke service"
)
self.try_attempts += 1
if self.try_attempts % self.error_attempts == 0:
global try_attempts
print(f"Attempt {try_attempts} of {self.error_attempts} to invoke service")
try_attempts += 1
if try_attempts % self.error_attempts == 0:
return f"{input.greeting}, {input.name}!"
raise Exception("service is down")
Empty file.
31 changes: 31 additions & 0 deletions tests/polling/infrequent/workflow_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import uuid

import pytest
from temporalio.client import Client
from temporalio.testing import WorkflowEnvironment
from temporalio.worker import Worker

from polling.infrequent.activities import compose_greeting
from polling.infrequent.workflows import GreetingWorkflow


async def test_infrequent_polling_workflow(client: Client, env: WorkflowEnvironment):
if not env.supports_time_skipping:
pytest.skip("Too slow to test with time-skipping disabled")

# Start a worker that hosts the workflow and activity implementations.
task_queue = f"tq-{uuid.uuid4()}"
async with Worker(
client,
task_queue=task_queue,
workflows=[GreetingWorkflow],
activities=[compose_greeting],
):
handle = await client.start_workflow(
GreetingWorkflow.run,
"Temporal",
id=f"infrequent-polling-{uuid.uuid4()}",
task_queue=task_queue,
)
result = await handle.result()
assert result == "Hello, Temporal!"

0 comments on commit ddd7b83

Please sign in to comment.