File tree Expand file tree Collapse file tree 7 files changed +53
-36
lines changed
Expand file tree Collapse file tree 7 files changed +53
-36
lines changed Original file line number Diff line number Diff line change 11import asyncio
2- from dataclasses import dataclass
32
43from 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
Original file line number Diff line number Diff line change 1- from dataclasses import dataclass
2-
31from 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
Original file line number Diff line number Diff line change 1- from dataclasses import dataclass
1+ from typing import Any , NoReturn
22
33from 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" )
Original file line number Diff line number Diff line change 66from temporalio .exceptions import ActivityError
77
88with 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
Original file line number Diff line number Diff line change 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+
113class 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" )
Original file line number Diff line number Diff line change 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!"
You can’t perform that action at this time.
0 commit comments