Skip to content

Commit fe22301

Browse files
fix: health check port handling (#138)
* fix: health check port handling * add test covering case
1 parent 18e438a commit fe22301

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

src/agentex/lib/core/temporal/workers/worker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def __init__(
112112
task_queue,
113113
max_workers: int = 10,
114114
max_concurrent_activities: int = 10,
115-
health_check_port: int = int(os.environ.get("HEALTH_CHECK_PORT")),
115+
health_check_port: int | None = None,
116116
plugins: list = [],
117117
):
118118
self.task_queue = task_queue
@@ -121,7 +121,7 @@ def __init__(
121121
self.max_concurrent_activities = max_concurrent_activities
122122
self.health_check_server_running = False
123123
self.healthy = False
124-
self.health_check_port = health_check_port
124+
self.health_check_port = health_check_port if health_check_port is not None else EnvironmentVariables.refresh().HEALTH_CHECK_PORT
125125
self.plugins = plugins
126126

127127
@overload

tests/lib/test_agentex_worker.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import os
2+
from unittest.mock import patch
3+
4+
import pytest
5+
6+
7+
class TestAgentexWorker:
8+
"""Tests for AgentexWorker initialization and configuration."""
9+
10+
@pytest.fixture(autouse=True)
11+
def cleanup_env(self):
12+
"""Cleanup environment variables after each test."""
13+
yield
14+
# Clean up HEALTH_CHECK_PORT if it was set during test
15+
os.environ.pop("HEALTH_CHECK_PORT", None)
16+
17+
def test_worker_init_uses_default_health_check_port(self):
18+
"""Test that worker uses default health_check_port of 80 when not provided."""
19+
from agentex.lib.core.temporal.workers.worker import AgentexWorker
20+
21+
# Ensure HEALTH_CHECK_PORT is not in environment
22+
os.environ.pop("HEALTH_CHECK_PORT", None)
23+
24+
# Mock EnvironmentVariables.refresh to avoid loading .env files
25+
with patch("agentex.lib.core.temporal.workers.worker.EnvironmentVariables") as mock_env_vars:
26+
mock_instance = mock_env_vars.refresh.return_value
27+
mock_instance.HEALTH_CHECK_PORT = 80
28+
29+
worker = AgentexWorker(task_queue="test-queue")
30+
31+
assert worker.health_check_port == 80, "Worker should use default health_check_port of 80"
32+
33+
def test_worker_init_with_explicit_health_check_port(self):
34+
"""Test that worker uses explicit health_check_port parameter when provided."""
35+
from agentex.lib.core.temporal.workers.worker import AgentexWorker
36+
37+
worker = AgentexWorker(task_queue="test-queue", health_check_port=8080)
38+
39+
assert worker.health_check_port == 8080, "Worker should use explicitly provided health_check_port"
40+
41+
def test_worker_init_explicit_port_overrides_environment(self):
42+
"""Test that explicit health_check_port parameter overrides environment variable."""
43+
from agentex.lib.core.temporal.workers.worker import AgentexWorker
44+
45+
# Set environment variable
46+
os.environ["HEALTH_CHECK_PORT"] = "9000"
47+
48+
worker = AgentexWorker(task_queue="test-queue", health_check_port=8080)
49+
50+
assert worker.health_check_port == 8080, "Explicit parameter should override environment variable"
51+
52+
@pytest.mark.parametrize(
53+
"env_port,expected_port",
54+
[
55+
(None, 80), # No env var, should use default
56+
("8080", 8080), # Env var set, should use it
57+
("443", 443), # Different port
58+
],
59+
)
60+
def test_worker_init_respects_environment_variable(self, env_port, expected_port):
61+
"""Test that worker respects HEALTH_CHECK_PORT from EnvironmentVariables."""
62+
from agentex.lib.core.temporal.workers.worker import AgentexWorker
63+
64+
# Mock EnvironmentVariables.refresh to return expected port
65+
with patch("agentex.lib.core.temporal.workers.worker.EnvironmentVariables") as mock_env_vars:
66+
mock_instance = mock_env_vars.refresh.return_value
67+
mock_instance.HEALTH_CHECK_PORT = expected_port
68+
69+
worker = AgentexWorker(task_queue="test-queue")
70+
71+
assert worker.health_check_port == expected_port, f"Worker should use health_check_port {expected_port}"
72+
73+
def test_worker_init_basic_attributes(self):
74+
"""Test that worker initializes with correct basic attributes."""
75+
from agentex.lib.core.temporal.workers.worker import AgentexWorker
76+
77+
worker = AgentexWorker(
78+
task_queue="test-queue",
79+
max_workers=20,
80+
max_concurrent_activities=15,
81+
health_check_port=8080,
82+
)
83+
84+
assert worker.task_queue == "test-queue"
85+
assert worker.max_workers == 20
86+
assert worker.max_concurrent_activities == 15
87+
assert worker.health_check_port == 8080
88+
assert worker.health_check_server_running is False
89+
assert worker.healthy is False
90+
assert worker.plugins == []

0 commit comments

Comments
 (0)