Skip to content

Commit 4bca496

Browse files
committed
Make Nexus HTTP port usage in tests work with external server
1 parent 91a795e commit 4bca496

6 files changed

+25
-31
lines changed

tests/helpers/nexus.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import temporalio.workflow
99
from temporalio.client import Client
1010
from temporalio.converter import FailureConverter, PayloadConverter
11+
from temporalio.testing import WorkflowEnvironment
1112

1213
with temporalio.workflow.unsafe.imports_passed_through():
1314
import httpx
@@ -75,6 +76,13 @@ async def cancel_operation(
7576
params={"token": token},
7677
)
7778

79+
@staticmethod
80+
def default_server_address(env: WorkflowEnvironment) -> str:
81+
# TODO(nexus-preview): nexus tests are making http requests directly but this is
82+
# not officially supported.
83+
http_port = getattr(env, "_http_port", 7243)
84+
return f"http://127.0.0.1:{http_port}"
85+
7886

7987
def dataclass_as_dict(dataclass: Any) -> dict[str, Any]:
8088
"""

tests/nexus/test_dynamic_creation_of_user_handler_classes.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
from temporalio.nexus._util import get_operation_factory
1111
from temporalio.testing import WorkflowEnvironment
1212
from temporalio.worker import Worker
13-
from tests.helpers.nexus import create_nexus_endpoint
14-
15-
HTTP_PORT = 7243
13+
from tests.helpers.nexus import ServiceClient, create_nexus_endpoint
1614

1715

1816
@workflow.defn
@@ -102,9 +100,10 @@ async def test_run_nexus_service_from_programmatically_created_service_handler(
102100
task_queue=task_queue,
103101
nexus_service_handlers=[service_handler],
104102
):
103+
server_address = ServiceClient.default_server_address(env)
105104
async with httpx.AsyncClient() as http_client:
106105
response = await http_client.post(
107-
f"http://127.0.0.1:{HTTP_PORT}/nexus/endpoints/{endpoint}/services/{service_name}/increment",
106+
f"http://{server_address}/nexus/endpoints/{endpoint}/services/{service_name}/increment",
108107
json=1,
109108
)
110109
assert response.status_code == 201
@@ -147,7 +146,9 @@ async def _increment_op(
147146
@pytest.mark.skip(
148147
reason="Dynamic creation of service contract using type() is not supported"
149148
)
150-
async def test_dynamic_creation_of_user_handler_classes(client: Client):
149+
async def test_dynamic_creation_of_user_handler_classes(
150+
client: Client, env: WorkflowEnvironment
151+
):
151152
task_queue = str(uuid.uuid4())
152153

153154
service_cls, handler_cls = (
@@ -165,9 +166,10 @@ async def test_dynamic_creation_of_user_handler_classes(client: Client):
165166
task_queue=task_queue,
166167
nexus_service_handlers=[handler_cls()],
167168
):
169+
server_address = ServiceClient.default_server_address(env)
168170
async with httpx.AsyncClient() as http_client:
169171
response = await http_client.post(
170-
f"http://127.0.0.1:{HTTP_PORT}/nexus/endpoints/{endpoint}/services/{service_name}/increment",
172+
f"http://{server_address}/nexus/endpoints/{endpoint}/services/{service_name}/increment",
171173
json=1,
172174
)
173175
assert response.status_code == 200

tests/nexus/test_handler.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@
6262
dataclass_as_dict,
6363
)
6464

65-
HTTP_PORT = 7243
66-
6765

6866
@dataclass
6967
class Input:
@@ -622,7 +620,7 @@ async def _test_start_operation_with_service_definition(
622620
task_queue = str(uuid.uuid4())
623621
endpoint = (await create_nexus_endpoint(task_queue, env.client)).endpoint.id
624622
service_client = ServiceClient(
625-
server_address=server_address(env),
623+
server_address=ServiceClient.default_server_address(env),
626624
endpoint=endpoint,
627625
service=(test_case.service_defn),
628626
)
@@ -656,7 +654,7 @@ async def _test_start_operation_without_service_definition(
656654
task_queue = str(uuid.uuid4())
657655
endpoint = (await create_nexus_endpoint(task_queue, env.client)).endpoint.id
658656
service_client = ServiceClient(
659-
server_address=server_address(env),
657+
server_address=ServiceClient.default_server_address(env),
660658
endpoint=endpoint,
661659
service=MyServiceHandler.__name__,
662660
)
@@ -744,7 +742,7 @@ async def test_start_operation_without_type_annotations(
744742
task_queue = str(uuid.uuid4())
745743
endpoint = (await create_nexus_endpoint(task_queue, env.client)).endpoint.id
746744
service_client = ServiceClient(
747-
server_address=server_address(env),
745+
server_address=ServiceClient.default_server_address(env),
748746
endpoint=endpoint,
749747
service=MyServiceWithOperationsWithoutTypeAnnotations.__name__,
750748
)
@@ -791,7 +789,7 @@ async def test_logger_uses_operation_context(env: WorkflowEnvironment, caplog: A
791789
resp = await create_nexus_endpoint(task_queue, env.client)
792790
endpoint = resp.endpoint.id
793791
service_client = ServiceClient(
794-
server_address=server_address(env),
792+
server_address=ServiceClient.default_server_address(env),
795793
endpoint=endpoint,
796794
service=service_name,
797795
)
@@ -950,7 +948,7 @@ async def test_cancel_operation_with_invalid_token(env: WorkflowEnvironment):
950948
task_queue = str(uuid.uuid4())
951949
endpoint = (await create_nexus_endpoint(task_queue, env.client)).endpoint.id
952950
service_client = ServiceClient(
953-
server_address=server_address(env),
951+
server_address=ServiceClient.default_server_address(env),
954952
endpoint=endpoint,
955953
service=MyService.__name__,
956954
)
@@ -982,7 +980,7 @@ async def test_request_id_is_received_by_sync_operation(
982980
task_queue = str(uuid.uuid4())
983981
endpoint = (await create_nexus_endpoint(task_queue, env.client)).endpoint.id
984982
service_client = ServiceClient(
985-
server_address=server_address(env),
983+
server_address=ServiceClient.default_server_address(env),
986984
endpoint=endpoint,
987985
service=MyService.__name__,
988986
)
@@ -1056,7 +1054,7 @@ async def test_request_id_becomes_start_workflow_request_id(env: WorkflowEnviron
10561054
task_queue = str(uuid.uuid4())
10571055
endpoint = (await create_nexus_endpoint(task_queue, env.client)).endpoint.id
10581056
service_client = ServiceClient(
1059-
server_address=server_address(env),
1057+
server_address=ServiceClient.default_server_address(env),
10601058
endpoint=endpoint,
10611059
service=ServiceHandlerForRequestIdTest.__name__,
10621060
)
@@ -1124,8 +1122,3 @@ async def start_two_workflows_in_a_single_operation(
11241122
await start_two_workflows_in_a_single_operation(
11251123
request_id_1, 500, "Workflow execution already started"
11261124
)
1127-
1128-
1129-
def server_address(env: WorkflowEnvironment) -> str:
1130-
http_port = getattr(env, "_http_port", 7243)
1131-
return f"http://127.0.0.1:{http_port}"

tests/nexus/test_handler_async_operation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ async def test_async_operation_lifecycle(
151151
task_queue = str(uuid.uuid4())
152152
endpoint = (await create_nexus_endpoint(task_queue, env.client)).endpoint.id
153153
service_client = ServiceClient(
154-
f"http://127.0.0.1:{env._http_port}", # type: ignore
154+
ServiceClient.default_server_address(env),
155155
endpoint,
156156
service_handler_cls.__name__,
157157
)

tests/nexus/test_handler_interface_implementation.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
from temporalio import nexus
99
from temporalio.nexus import WorkflowRunOperationContext, workflow_run_operation
1010

11-
HTTP_PORT = 7243
12-
1311

1412
class _InterfaceImplementationTestCase:
1513
Interface: Type[Any]

tests/nexus/test_workflow_run_operation.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
dataclass_as_dict,
2727
)
2828

29-
HTTP_PORT = 7243
30-
3129

3230
@dataclass
3331
class Input:
@@ -97,7 +95,7 @@ async def test_workflow_run_operation(
9795
endpoint = (await create_nexus_endpoint(task_queue, env.client)).endpoint.id
9896
assert (service_defn := nexusrpc.get_service_definition(service_handler_cls))
9997
service_client = ServiceClient(
100-
server_address=server_address(env),
98+
server_address=ServiceClient.default_server_address(env),
10199
endpoint=endpoint,
102100
service=service_defn.name,
103101
)
@@ -117,8 +115,3 @@ async def test_workflow_run_operation(
117115
assert re.search(message, failure.message)
118116
else:
119117
assert resp.status_code == 201
120-
121-
122-
def server_address(env: WorkflowEnvironment) -> str:
123-
http_port = getattr(env, "_http_port", 7243)
124-
return f"http://127.0.0.1:{http_port}"

0 commit comments

Comments
 (0)