2020import tempfile
2121import threading
2222
23- # Pick a per-xdist-worker bucket and pin env vars *before* any dimos
24- # module is imported. ``LCMConfig`` captures ``LCM_DEFAULT_URL`` at import
25- # time; ``GlobalConfig`` captures ``MCP_PORT``; ``run_registry`` captures
26- # ``XDG_STATE_HOME``. ``LCM_DEFAULT_URL`` in particular has to be an env
27- # var (not just a fixture) because subprocess workers spawned by
23+ # With pytest-xdist, pick a per-worker bucket and pin env vars *before*
24+ # any dimos module is imported, so parallel workers don't share LCM bus,
25+ # MCP port, or state directory. ``LCMConfig`` captures ``LCM_DEFAULT_URL``
26+ # at import time; ``GlobalConfig`` captures ``MCP_PORT``; ``run_registry``
27+ # captures ``XDG_STATE_HOME``. ``LCM_DEFAULT_URL`` in particular has to be
28+ # an env var (not just a fixture) because subprocess workers spawned by
2829# ``ModuleCoordinator`` create their own ``LCMConfig`` / ``LCMRPC``
2930# instances internally and can't receive a fixture value — they inherit
3031# our env at fork time.
31- _worker = os .environ .get ("PYTEST_XDIST_WORKER" , "main" )
32- _BUCKET = int .from_bytes (hashlib .blake2b (_worker .encode (), digest_size = 2 ).digest (), "big" ) % 5000
33- os .environ ["LCM_DEFAULT_URL" ] = f"udpm://239.255.76.67:{ 7700 + _BUCKET } ?ttl=0"
34- os .environ ["MCP_PORT" ] = str (20000 + _BUCKET )
35- os .environ ["XDG_STATE_HOME" ] = tempfile .mkdtemp (prefix = f"dimos-test-state-{ _worker } -" )
32+ #
33+ # Single-worker runs (no xdist) keep the defaults, so external processes
34+ # with hard-coded ports (e.g. the dimsim Deno bridge, which binds to LCM
35+ # 7667) can still talk to the test bus.
36+ _worker = os .environ .get ("PYTEST_XDIST_WORKER" )
37+ if _worker :
38+ _BUCKET = (
39+ int .from_bytes (hashlib .blake2b (_worker .encode (), digest_size = 2 ).digest (), "big" ) % 5000
40+ )
41+ os .environ ["LCM_DEFAULT_URL" ] = f"udpm://239.255.76.67:{ 7700 + _BUCKET } ?ttl=0"
42+ os .environ ["MCP_PORT" ] = str (20000 + _BUCKET )
43+ os .environ ["XDG_STATE_HOME" ] = tempfile .mkdtemp (prefix = f"dimos-test-state-{ _worker } -" )
3644
3745# Raise the open-file limit. Each LCM transport opens at least one
3846# multicast socket; with pytest-xdist workers running many in parallel,
@@ -77,6 +85,7 @@ def pytest_configure(config):
7785 "self_hosted: tests that need the self-hosted runner (LFS, ROS, CUDA, etc.)" ,
7886 )
7987 config .addinivalue_line ("markers" , "mujoco: tests which open mujoco" )
88+ config .addinivalue_line ("markers" , "dimsim: tests which require dimsim" )
8089 config .addinivalue_line ("markers" , "skipif_in_ci: skip when CI env var is set" )
8190 config .addinivalue_line ("markers" , "skipif_no_openai: skip when OPENAI_API_KEY is not set" )
8291 config .addinivalue_line ("markers" , "skipif_no_alibaba: skip when ALIBABA_API_KEY is not set" )
@@ -90,20 +99,20 @@ def pytest_configure(config):
9099
91100@pytest .fixture (scope = "session" )
92101def mcp_port () -> int :
93- """The MCP server port pinned for this xdist worker."""
94- return 20000 + _BUCKET
102+ """The MCP server port pinned for this xdist worker (or the default) ."""
103+ return int ( os . environ . get ( "MCP_PORT" , "9990" ))
95104
96105
97106@pytest .fixture (scope = "session" )
98107def mcp_url (mcp_port : int ) -> str :
99- """The MCP server URL pinned for this xdist worker."""
108+ """The MCP server URL pinned for this xdist worker (or the default) ."""
100109 return f"http://localhost:{ mcp_port } /mcp"
101110
102111
103112@pytest .fixture (scope = "session" )
104113def lcm_url () -> str :
105- """The LCM bus URL pinned for this xdist worker."""
106- return f" udpm://239.255.76.67:{ 7700 + _BUCKET } ?ttl=0"
114+ """The LCM bus URL pinned for this xdist worker (or the default) ."""
115+ return os . environ . get ( "LCM_DEFAULT_URL" , " udpm://239.255.76.67:7667 ?ttl=0")
107116
108117
109118@pytest .hookimpl (tryfirst = True )
0 commit comments