Skip to content

Commit ddd7b83

Browse files
committed
Fix infrequent polling sample
1 parent c5bb4e4 commit ddd7b83

File tree

7 files changed

+53
-36
lines changed

7 files changed

+53
-36
lines changed

polling/frequent/activities.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
import asyncio
2-
from dataclasses import dataclass
32

43
from temporalio import activity
54

6-
from polling.test_service import TestService
7-
8-
9-
@dataclass
10-
class ComposeGreetingInput:
11-
greeting: str
12-
name: str
5+
from polling.test_service import ComposeGreetingInput, TestService
136

147

158
@activity.defn

polling/infrequent/activities.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
from dataclasses import dataclass
2-
31
from temporalio import activity
42

5-
from polling.test_service import TestService
6-
7-
8-
@dataclass
9-
class ComposeGreetingInput:
10-
greeting: str
11-
name: str
3+
from polling.test_service import ComposeGreetingInput, TestService
124

135

146
@activity.defn
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
from dataclasses import dataclass
1+
from typing import Any, NoReturn
22

33
from temporalio import activity
44

55

6-
@dataclass
7-
class ComposeGreetingInput:
8-
greeting: str
9-
name: str
10-
11-
126
@activity.defn
13-
async def compose_greeting(input: ComposeGreetingInput) -> str:
7+
async def compose_greeting(input: Any) -> NoReturn:
148
raise RuntimeError("Service is down")

polling/periodic_sequence/workflows.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
from temporalio.exceptions import ActivityError
77

88
with workflow.unsafe.imports_passed_through():
9-
from polling.periodic_sequence.activities import (
10-
ComposeGreetingInput,
11-
compose_greeting,
12-
)
9+
from polling.periodic_sequence.activities import compose_greeting
10+
from polling.test_service import ComposeGreetingInput
1311

1412

1513
@workflow.defn

polling/test_service.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1+
from dataclasses import dataclass
2+
3+
4+
@dataclass
5+
class ComposeGreetingInput:
6+
greeting: str
7+
name: str
8+
9+
10+
try_attempts = 0
11+
12+
113
class TestService:
214
def __init__(self):
3-
self.try_attempts = 0
415
self.error_attempts = 5
516

617
async def get_service_result(self, input):
7-
print(
8-
f"Attempt {self.try_attempts}"
9-
f" of {self.error_attempts} to invoke service"
10-
)
11-
self.try_attempts += 1
12-
if self.try_attempts % self.error_attempts == 0:
18+
global try_attempts
19+
print(f"Attempt {try_attempts} of {self.error_attempts} to invoke service")
20+
try_attempts += 1
21+
if try_attempts % self.error_attempts == 0:
1322
return f"{input.greeting}, {input.name}!"
1423
raise Exception("service is down")

tests/polling/infrequent/__init__.py

Whitespace-only changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import uuid
2+
3+
import pytest
4+
from temporalio.client import Client
5+
from temporalio.testing import WorkflowEnvironment
6+
from temporalio.worker import Worker
7+
8+
from polling.infrequent.activities import compose_greeting
9+
from polling.infrequent.workflows import GreetingWorkflow
10+
11+
12+
async def test_infrequent_polling_workflow(client: Client, env: WorkflowEnvironment):
13+
if not env.supports_time_skipping:
14+
pytest.skip("Too slow to test with time-skipping disabled")
15+
16+
# Start a worker that hosts the workflow and activity implementations.
17+
task_queue = f"tq-{uuid.uuid4()}"
18+
async with Worker(
19+
client,
20+
task_queue=task_queue,
21+
workflows=[GreetingWorkflow],
22+
activities=[compose_greeting],
23+
):
24+
handle = await client.start_workflow(
25+
GreetingWorkflow.run,
26+
"Temporal",
27+
id=f"infrequent-polling-{uuid.uuid4()}",
28+
task_queue=task_queue,
29+
)
30+
result = await handle.result()
31+
assert result == "Hello, Temporal!"

0 commit comments

Comments
 (0)