Skip to content

Fix interceptors in testing environment #364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion temporalio/testing/_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,5 +562,5 @@ def _client_with_interceptors(
config = client.config()
config_interceptors = list(config["interceptors"])
config_interceptors.extend(interceptors)
config["interceptors"] = interceptors
config["interceptors"] = config_interceptors
return temporalio.client.Client(**config)
41 changes: 39 additions & 2 deletions tests/testing/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
import uuid
from datetime import datetime, timedelta, timezone
from time import monotonic
from typing import Optional, Union
from typing import Any, List, Optional, Union

import pytest

from temporalio import activity, workflow
from temporalio.client import Client, WorkflowFailureError
from temporalio.client import (
Client,
Interceptor,
OutboundInterceptor,
StartWorkflowInput,
WorkflowFailureError,
WorkflowHandle,
)
from temporalio.common import RetryPolicy
from temporalio.exceptions import (
ActivityError,
Expand Down Expand Up @@ -176,7 +183,36 @@ def some_signal(self) -> None:
assert "foo" == "bar"


class SimpleClientInterceptor(Interceptor):
def __init__(self) -> None:
self.events: List[str] = []

def intercept_client(self, next: OutboundInterceptor) -> OutboundInterceptor:
return SimpleClientOutboundInterceptor(self, super().intercept_client(next))


class SimpleClientOutboundInterceptor(OutboundInterceptor):
def __init__(
self, root: SimpleClientInterceptor, next: OutboundInterceptor
) -> None:
super().__init__(next)
self.root = root

async def start_workflow(
self, input: StartWorkflowInput
) -> WorkflowHandle[Any, Any]:
self.root.events.append(f"start: {input.workflow}")
return await super().start_workflow(input)


async def test_workflow_env_assert(client: Client):
# Set the interceptor on the client. This used to fail for being
# accidentally overridden.
client_config = client.config()
interceptor = SimpleClientInterceptor()
client_config["interceptors"] = [interceptor]
client = Client(**client_config)

def assert_proper_error(err: Optional[BaseException]) -> None:
assert isinstance(err, ApplicationError)
# In unsandboxed workflows, this message has extra diff info appended
Expand All @@ -195,6 +231,7 @@ def assert_proper_error(err: Optional[BaseException]) -> None:
task_queue=worker.task_queue,
)
assert_proper_error(err.value.cause)
assert interceptor.events

# Start a new one and check signal
handle = await env.client.start_workflow(
Expand Down