-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
64 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,10 @@ | ||
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, get_service_result | ||
|
||
|
||
@activity.defn | ||
async def compose_greeting(input: ComposeGreetingInput) -> str: | ||
test_service = TestService() | ||
# If this raises an exception because it's not done yet, the activity will | ||
# continually be scheduled for retry | ||
return await test_service.get_service_result(input) | ||
return await get_service_result(input) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,23 @@ | ||
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: | ||
return f"{input.greeting}, {input.name}!" | ||
raise Exception("service is down") | ||
from dataclasses import dataclass | ||
from typing import Counter | ||
|
||
from temporalio import activity | ||
|
||
attempts = Counter[str]() | ||
ERROR_ATTEMPTS = 5 | ||
|
||
|
||
@dataclass | ||
class ComposeGreetingInput: | ||
greeting: str | ||
name: str | ||
|
||
|
||
async def get_service_result(input): | ||
workflow_id = activity.info().workflow_id | ||
attempts[workflow_id] += 1 | ||
|
||
print(f"Attempt {attempts[workflow_id]} of {ERROR_ATTEMPTS} to invoke service") | ||
if attempts[workflow_id] == ERROR_ATTEMPTS: | ||
return f"{input.greeting}, {input.name}!" | ||
raise Exception("service is down") |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" |