test_router_e2e_with_mockers.py 28.7 KB
Newer Older
1
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
# SPDX-License-Identifier: Apache-2.0
3
4
5
6
7
8

# Parallelization: Hermetic tests (xdist-safe via dynamic ports + per-test namespaces).
# Tested on: Linux container.
# Combined pre_merge wall time (this file):
# - Serialized: 304.01s.
# - Parallel (-n auto): 34.55s (269.46s saved, 8.80x).
9
10
import logging
import os
11
from typing import Any, Dict, Optional
12
13
14

import pytest

15
from tests.router.common import (  # utilities
16
    _test_busy_threshold_endpoint,
17
18
19
    _test_python_router_bindings,
    _test_router_basic,
    _test_router_decisions,
20
    _test_router_decisions_disagg,
21
22
23
24
25
26
27
    _test_router_indexers_sync,
    _test_router_overload_503,
    _test_router_query_instance_id,
    _test_router_two_routers,
    generate_random_suffix,
    get_runtime,
)
Alec's avatar
Alec committed
28
from tests.utils.constants import ROUTER_MODEL_NAME
29
from tests.utils.managed_process import ManagedProcess
30
from tests.utils.port_utils import allocate_ports, deallocate_ports
31

32
33
34
35
logger = logging.getLogger(__name__)

MODEL_NAME = ROUTER_MODEL_NAME

36
37
38
39
pytestmark = [
    pytest.mark.pre_merge,
    pytest.mark.gpu_0,
    pytest.mark.integration,
40
    pytest.mark.parallel,
41
    pytest.mark.model(MODEL_NAME),
42
]
43
44
NUM_MOCKERS = 2
SPEEDUP_RATIO = 10.0
45
BASE_PORT = 9100  # Base port for all tests (high port to avoid conflicts)
46
NUM_REQUESTS = 100
47
BLOCK_SIZE = 16
48
49


50
def get_unique_ports(
51
52
53
54
    request,
    num_ports: int = 1,
    store_backend: str = "etcd",
    request_plane: str = "nats",
55
    registration_order: str = "prefill_first",
56
) -> list[int]:
57
    """Allocate random free ports for xdist-safe router tests.
58

59
60
61
    This replaces the previous "test-name offset" scheme with the shared flock-backed
    allocator from `tests.utils.port_utils`, which avoids collisions across pytest-xdist
    worker processes.
62

63
64
65
66
    Notes:
    - The extra parameters are kept for call-site compatibility (they no longer affect
      the chosen ports).
    - Ports are released at the end of the test via a pytest finalizer.
67
    """
68
69
70
    _ = (store_backend, request_plane, registration_order)
    ports = allocate_ports(num_ports, BASE_PORT)
    request.addfinalizer(lambda: deallocate_ports(ports))
71
72
73
    return ports


74
75
76
77
78
79
80
81
82
83
84
85
86
# Shared test payload for all tests
TEST_PAYLOAD: Dict[str, Any] = {
    "model": MODEL_NAME,
    "messages": [
        {
            "role": "user",
            "content": "In a quiet meadow tucked between rolling hills, a plump gray rabbit nibbled on clover beneath the shade of a gnarled oak tree. Its ears twitched at the faint rustle of leaves, but it remained calm, confident in the safety of its burrow just a few hops away. The late afternoon sun warmed its fur, and tiny dust motes danced in the golden light as bees hummed lazily nearby. Though the rabbit lived a simple life, every day was an adventure of scents, shadows, and snacks—an endless search for the tastiest patch of greens and the softest spot to nap.",
        }
    ],
    "stream": True,
    "max_tokens": 10,
}

87

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
def _build_mocker_command(
    endpoint: str,
    store_backend: str,
    num_workers: int,
    mocker_args: Dict[str, Any],
    worker_type: Optional[str] = None,
) -> list[str]:
    """Build the mocker CLI command with all arguments.

    Args:
        endpoint: The dynamo endpoint string
        store_backend: Storage backend ("etcd" or "file")
        num_workers: Number of workers to spawn (uses --num-workers flag)
        mocker_args: Dictionary of mocker arguments
        worker_type: Optional worker type ("prefill" or "decode") for disagg mode

    Returns:
        List of command arguments for subprocess
    """
    command = [
        "python",
        "-m",
        "dynamo.mocker",
        "--model-path",
        MODEL_NAME,
        "--endpoint",
        endpoint,
        "--store-kv",
        store_backend,
        "--num-workers",
        str(num_workers),
    ]

    # Add worker type flag for disaggregated mode
    if worker_type == "prefill":
        command.append("--is-prefill-worker")
    elif worker_type == "decode":
        command.append("--is-decode-worker")

    # Add individual CLI arguments from mocker_args
    if "speedup_ratio" in mocker_args:
        command.extend(["--speedup-ratio", str(mocker_args["speedup_ratio"])])
    if "block_size" in mocker_args:
        command.extend(["--block-size", str(mocker_args["block_size"])])
    if "num_gpu_blocks" in mocker_args:
        command.extend(
            ["--num-gpu-blocks-override", str(mocker_args["num_gpu_blocks"])]
        )
    if "max_num_seqs" in mocker_args:
        command.extend(["--max-num-seqs", str(mocker_args["max_num_seqs"])])
    if "max_num_batched_tokens" in mocker_args:
        command.extend(
            ["--max-num-batched-tokens", str(mocker_args["max_num_batched_tokens"])]
        )
    if "enable_prefix_caching" in mocker_args:
        if mocker_args["enable_prefix_caching"]:
            command.append("--enable-prefix-caching")
        else:
            command.append("--no-enable-prefix-caching")
    if "enable_chunked_prefill" in mocker_args:
        if mocker_args["enable_chunked_prefill"]:
            command.append("--enable-chunked-prefill")
        else:
            command.append("--no-enable-chunked-prefill")
    if "watermark" in mocker_args:
        command.extend(["--watermark", str(mocker_args["watermark"])])
    if "dp_size" in mocker_args:
        command.extend(["--data-parallel-size", str(mocker_args["dp_size"])])
156
157
    if mocker_args.get("enable_local_indexer"):
        command.append("--enable-local-indexer")
158
159
160
161

    return command


162
class MockerProcess:
163
    """Manages mocker engine instances with shared tokio runtime via --num-workers."""
164

165
166
167
168
169
    def __init__(
        self,
        request,
        mocker_args: Optional[Dict[str, Any]] = None,
        num_mockers: int = 1,
170
        store_backend: str = "etcd",
171
        request_plane: str = "nats",
172
    ):
173
174
        namespace_suffix = generate_random_suffix()
        self.namespace = f"test-namespace-{namespace_suffix}"
175
176
        self.component_name = "mocker"
        self.endpoint = f"dyn://{self.namespace}.{self.component_name}.generate"
177
178
179
        self.num_workers = num_mockers

        mocker_args = mocker_args or {}
180
181
        # Store dp_size for DP-aware test functions
        self.dp_size = mocker_args.get("dp_size")
182
183
184
185
186
187
188
189

        command = _build_mocker_command(
            endpoint=self.endpoint,
            store_backend=store_backend,
            num_workers=num_mockers,
            mocker_args=mocker_args,
        )

190
191
192
        env = os.environ.copy()
        env["DYN_REQUEST_PLANE"] = request_plane

193
194
        self._process = ManagedProcess(
            command=command,
195
            env=env,
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
            timeout=60,
            display_output=True,
            health_check_ports=[],
            health_check_urls=[],
            log_dir=request.node.name,
            terminate_existing=False,
        )
        logger.info(
            f"Created mocker process with {num_mockers} worker(s), endpoint: {self.endpoint}"
        )

    def __enter__(self):
        logger.info(f"Starting mocker process with {self.num_workers} worker(s)")
        self._process.__enter__()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        logger.info("Stopping mocker process")
        self._process.__exit__(exc_type, exc_val, exc_tb)


class DisaggMockerProcess:
    """Manages prefill or decode mocker instances for disaggregated serving.

    Uses --num-workers for shared tokio runtime. For disaggregated serving:
    - Prefill workers: worker_type="prefill", endpoint is namespace.prefill.generate
    - Decode workers: worker_type="decode", endpoint is namespace.backend.generate

    Both prefill and decode workers should share the same namespace for proper discovery.
    """

    def __init__(
        self,
        request,
        namespace: str,
        worker_type: str,
        mocker_args: Optional[Dict[str, Any]] = None,
        num_mockers: int = 1,
        store_backend: str = "etcd",
235
        request_plane: str = "nats",
236
237
238
239
    ):
        if worker_type not in ("prefill", "decode"):
            raise ValueError(
                f"worker_type must be 'prefill' or 'decode', got {worker_type}"
240
            )
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263

        self.namespace = namespace
        self.worker_type = worker_type
        self.num_workers = num_mockers

        # Set component name and endpoint based on worker type
        if worker_type == "prefill":
            self.component_name = "prefill"
            self.endpoint = f"dyn://{self.namespace}.prefill.generate"
        else:
            self.component_name = "backend"
            self.endpoint = f"dyn://{self.namespace}.backend.generate"

        mocker_args = mocker_args or {}

        command = _build_mocker_command(
            endpoint=self.endpoint,
            store_backend=store_backend,
            num_workers=num_mockers,
            mocker_args=mocker_args,
            worker_type=worker_type,
        )

264
265
266
        env = os.environ.copy()
        env["DYN_REQUEST_PLANE"] = request_plane

267
268
        self._process = ManagedProcess(
            command=command,
269
            env=env,
270
271
272
273
274
275
276
277
278
279
280
            timeout=60,
            display_output=True,
            health_check_ports=[],
            health_check_urls=[],
            log_dir=request.node.name,
            terminate_existing=False,
        )
        logger.info(
            f"Created {worker_type} mocker process with {num_mockers} worker(s), "
            f"endpoint: {self.endpoint}"
        )
281

282
    def __enter__(self):
283
284
285
286
        logger.info(
            f"Starting {self.worker_type} mocker process with {self.num_workers} worker(s)"
        )
        self._process.__enter__()
287
        return self
288

289
    def __exit__(self, exc_type, exc_val, exc_tb):
290
291
        logger.info(f"Stopping {self.worker_type} mocker process")
        self._process.__exit__(exc_type, exc_val, exc_tb)
292
293


294
@pytest.mark.timeout(42)  # ~3x average (~13.80s), rounded up
295
@pytest.mark.parametrize("request_plane", ["nats", "tcp"], indirect=True)
296
def test_mocker_kv_router(
297
    request, runtime_services_dynamic_ports, predownload_tokenizers, request_plane
298
):
299
300
301
    """
    Test KV router with multiple mocker engine instances.
    This test doesn't require GPUs and runs quickly for pre-merge validation.
302
    Tests both NATS and TCP request planes.
303
304
    """

305
306
    # runtime_services starts etcd and optionally nats based on request_plane
    logger.info(f"Starting mocker KV router test with request_plane={request_plane}")
307

308
    # Create mocker args dictionary
309
    mocker_args = {"speedup_ratio": SPEEDUP_RATIO, "block_size": BLOCK_SIZE}
310
311

    try:
312
        # Start mocker instances with the new CLI interface
313
        logger.info(f"Starting {NUM_MOCKERS} mocker instances")
314
        mockers = MockerProcess(
315
316
317
318
            request,
            mocker_args=mocker_args,
            num_mockers=NUM_MOCKERS,
            request_plane=request_plane,
319
        )
320
321
        logger.info(f"All mockers using endpoint: {mockers.endpoint}")
        mockers.__enter__()
322

323
        # Get unique port for this test
324
325
326
        frontend_port = get_unique_ports(
            request, num_ports=1, request_plane=request_plane
        )[0]
327
328

        # Run basic router test (starts router internally and waits for workers to be ready)
329
330
331
332
        _test_router_basic(
            engine_workers=mockers,
            block_size=BLOCK_SIZE,
            request=request,
333
            frontend_port=frontend_port,
334
335
            test_payload=TEST_PAYLOAD,
            num_requests=NUM_REQUESTS,
336
            request_plane=request_plane,
337
338
339
        )

    finally:
340
341
        if "mockers" in locals():
            mockers.__exit__(None, None, None)
342
343


344
@pytest.mark.parametrize("store_backend", ["etcd", "file"])
345
@pytest.mark.timeout(60)  # ~3x average (~19.86s), rounded up
346
347
def test_mocker_two_kv_router(
    request,
348
    runtime_services_dynamic_ports,
349
350
351
352
    predownload_tokenizers,
    file_storage_backend,
    store_backend,
):
353
354
355
    """
    Test with two KV routers and multiple mocker engine instances.
    Alternates requests between the two routers to test load distribution.
356
    Tests with both etcd and file storage backends.
357
358
359
    """

    # runtime_services starts etcd and nats
360
361
362
    logger.info(
        f"Starting mocker two KV router test with {store_backend} storage backend"
    )
363

364
    # Create mocker args dictionary
365
366
367
    mocker_args = {"speedup_ratio": SPEEDUP_RATIO, "block_size": BLOCK_SIZE}

    try:
368
        # Start mocker instances with the new CLI interface
369
        logger.info(f"Starting {NUM_MOCKERS} mocker instances")
370
        mockers = MockerProcess(
371
372
373
374
            request,
            mocker_args=mocker_args,
            num_mockers=NUM_MOCKERS,
            store_backend=store_backend,
375
        )
376
377
        logger.info(f"All mockers using endpoint: {mockers.endpoint}")
        mockers.__enter__()
378

379
380
381
382
383
        # Get unique ports for this test (2 ports for two routers)
        router_ports = get_unique_ports(
            request, num_ports=2, store_backend=store_backend
        )

384
385
386
387
388
        # Run two-router test (starts KV routers internally and manages their lifecycle)
        _test_router_two_routers(
            engine_workers=mockers,
            block_size=BLOCK_SIZE,
            request=request,
389
            router_ports=router_ports,
390
391
392
            test_payload=TEST_PAYLOAD,
            num_requests=NUM_REQUESTS,
            store_backend=store_backend,
393
394
395
        )

    finally:
396
397
        if "mockers" in locals():
            mockers.__exit__(None, None, None)
398
399


400
@pytest.mark.skip(reason="Flaky, temporarily disabled")
401
@pytest.mark.timeout(60)  # ~3x average (~19.86s), rounded up (when enabled)
Alec's avatar
Alec committed
402
def test_mocker_kv_router_overload_503(
403
    request, runtime_services_dynamic_ports, predownload_tokenizers
Alec's avatar
Alec committed
404
):
405
    """Test that KV router returns 503 when mocker workers are overloaded."""
406
    logger.info("Starting mocker KV router overload test for 503 status")
407
    # Create mocker args dictionary with limited resources
408
409
410
411
412
    mocker_args = {
        "speedup_ratio": 10,
        "block_size": 4,  # Smaller block size
        "num_gpu_blocks": 64,  # Limited GPU blocks to exhaust quickly
    }
413

414
    try:
415
        # Start single mocker instance with limited resources
416
        logger.info("Starting single mocker instance with limited resources")
417
        mockers = MockerProcess(request, mocker_args=mocker_args, num_mockers=1)
418
419
        logger.info(f"Mocker using endpoint: {mockers.endpoint}")
        mockers.__enter__()
420

421
422
423
        # Get unique port for this test
        frontend_port = get_unique_ports(request, num_ports=1)[0]

424
425
426
427
428
429
430
        # Run overload 503 test
        _test_router_overload_503(
            engine_workers=mockers,
            block_size=4,  # Match the mocker's block size
            request=request,
            frontend_port=frontend_port,
            test_payload=TEST_PAYLOAD,
431
            blocks_threshold=0.2,
432
        )
433
434

    finally:
435
436
        if "mockers" in locals():
            mockers.__exit__(None, None, None)
437

438

439
@pytest.mark.timeout(22)  # ~3x average (~7.10s), rounded up
440
@pytest.mark.parametrize("request_plane", ["nats", "tcp"], indirect=True)
441
def test_kv_push_router_bindings(
442
    request, runtime_services_dynamic_ports, predownload_tokenizers, request_plane
443
):
444
    """Test KvPushRouter Python bindings with mocker engines."""
445
446
447
448
    logger.info("Starting KvPushRouter bindings test")
    mocker_args = {"speedup_ratio": SPEEDUP_RATIO, "block_size": BLOCK_SIZE}

    try:
449
        # Start mocker instances
450
        logger.info(f"Starting {NUM_MOCKERS} mocker instances")
451
        mockers = MockerProcess(
452
453
454
455
            request,
            mocker_args=mocker_args,
            num_mockers=NUM_MOCKERS,
            request_plane=request_plane,
456
        )
457
458
        logger.info(f"All mockers using endpoint: {mockers.endpoint}")
        mockers.__enter__()
459

460
        # Get runtime and create endpoint
461
        runtime = get_runtime(request_plane=request_plane)
462
        namespace = runtime.namespace(mockers.namespace)
463
        component = namespace.component(mockers.component_name)
464
465
        endpoint = component.endpoint("generate")

466
467
468
        # Run Python router bindings test
        _test_python_router_bindings(
            engine_workers=mockers,
469
470
            endpoint=endpoint,
            block_size=BLOCK_SIZE,
471
472
            model_name=MODEL_NAME,
            num_workers=NUM_MOCKERS,
473
        )
474
475

    finally:
476
477
478
479
        if "mockers" in locals():
            mockers.__exit__(None, None, None)


480
481
482
483
484
# NO @pytest.mark.parallel - nats_core variant stops/restarts NATS
@pytest.mark.parametrize(
    "store_backend,use_nats_core,request_plane",
    [
        ("etcd", False, "nats"),  # JetStream mode
485
        ("etcd", True, "tcp"),  # NATS core mode (with gap detection)
486
487
        ("file", False, "nats"),  # File backend
    ],
488
489
490
491
492
    ids=[
        "jetstream",
        "nats",
        "file",
    ],  # "nats_core" commented out to match commented test case
493
)
494
@pytest.mark.timeout(90)  # TODO: figure out a timeout
495
496
def test_indexers_sync(
    request,
497
    runtime_services_dynamic_ports,
498
499
500
    predownload_tokenizers,
    file_storage_backend,
    store_backend,
501
502
    use_nats_core,
    request_plane,
503
):
504
505
506
507
    """
    Test that two KV routers have synchronized indexer states after processing requests.
    This test verifies that both routers converge to the same internal state.

508
509
510
511
512
513
514
515
516
517
    Tests with three configurations:
    - jetstream: etcd backend, JetStream for KV events, NATS request plane
    - nats_core: etcd backend, local indexer with NATS Core, TCP request plane
                 (includes NATS interruption/recovery testing)
    - file: file backend, JetStream for KV events, NATS request plane
    """
    logger.info(
        f"Starting indexers sync test: store_backend={store_backend}, "
        f"use_nats_core={use_nats_core}, request_plane={request_plane}"
    )
518

519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
    # Use the dynamic-port fixture to avoid hardcoded localhost:4222/2379 in parallel runs.
    nats_process, _etcd_process = runtime_services_dynamic_ports

    # Create mocker args dictionary
    mocker_args = {
        "speedup_ratio": SPEEDUP_RATIO,
        "block_size": BLOCK_SIZE,
        "enable_local_indexer": use_nats_core,
    }

    try:
        # Start mocker instances
        logger.info(f"Starting {NUM_MOCKERS} mocker instances")
        mockers = MockerProcess(
            request,
            mocker_args=mocker_args,
            num_mockers=NUM_MOCKERS,
            store_backend=store_backend,
            request_plane=request_plane,
        )
        logger.info(f"All mockers using endpoint: {mockers.endpoint}")
        mockers.__enter__()

        # Use the common test implementation (creates its own runtimes for each router)
        # Note: Consumer verification is done inside _test_router_indexers_sync while routers are alive
        _test_router_indexers_sync(
            engine_workers=mockers,
            block_size=BLOCK_SIZE,
            model_name=MODEL_NAME,
            num_workers=NUM_MOCKERS,
            store_backend=store_backend,
            request_plane=request_plane,
            test_nats_interruption=use_nats_core,
            nats_server=nats_process if use_nats_core else None,
        )

        logger.info("Indexers sync test completed successfully")

    finally:
        if "mockers" in locals():
            mockers.__exit__(None, None, None)


@pytest.mark.timeout(42)  # ~3x average (~13.80s), rounded up
Alec's avatar
Alec committed
563
def test_query_instance_id_returns_worker_and_tokens(
564
    request, runtime_services_dynamic_ports, predownload_tokenizers
Alec's avatar
Alec committed
565
):
566
    """Test query_instance_id annotation with mocker engines."""
567
568
569
570
571
    logger.info("Starting KV router query_instance_id annotation test")
    mocker_args = {"speedup_ratio": SPEEDUP_RATIO, "block_size": BLOCK_SIZE}
    os.makedirs(request.node.name, exist_ok=True)

    try:
572
        # Start mocker instances
573
        logger.info(f"Starting {NUM_MOCKERS} mocker instances")
574
575
576
        mockers = MockerProcess(
            request, mocker_args=mocker_args, num_mockers=NUM_MOCKERS
        )
577
578
        logger.info(f"All mockers using endpoint: {mockers.endpoint}")
        mockers.__enter__()
579

580
581
582
        # Get unique port for this test
        frontend_port = get_unique_ports(request, num_ports=1)[0]

583
584
585
586
587
588
589
590
        # Run query_instance_id annotation test
        _test_router_query_instance_id(
            engine_workers=mockers,
            block_size=BLOCK_SIZE,
            request=request,
            frontend_port=frontend_port,
            test_payload=TEST_PAYLOAD,
        )
591
592

    finally:
593
594
        if "mockers" in locals():
            mockers.__exit__(None, None, None)
595
596


597
@pytest.mark.timeout(29)  # ~3x average (~9.55s), rounded up
598
@pytest.mark.parametrize("request_plane", ["nats", "tcp"], indirect=True)
599
600
601
602
603
604
605
606
607
@pytest.mark.parametrize(
    "use_nats_core,use_kv_events",
    [
        (False, True),  # JetStream mode (default)
        (True, True),  # NATS Core + local indexer mode
        (False, False),  # Approximate mode (--no-kv-events)
    ],
    ids=["jetstream", "nats_core", "no_kv_events"],
)
608
def test_router_decisions(
609
610
611
612
    request,
    runtime_services_dynamic_ports,
    predownload_tokenizers,
    use_nats_core,
613
    use_kv_events,
614
    request_plane,
615
616
617
):
    """Validate KV cache prefix reuse and dp_rank routing by sending progressive requests with overlapping prefixes.

618
619
620
621
622
    Parameterized to test:
    - JetStream mode (default): KV events via JetStream
    - NATS Core mode: KV events via NATS Core with local indexer on workers
    - Approximate mode (--no-kv-events): No KV events, router predicts cache state
      based on routing decisions with TTL-based expiration and pruning
623
    """
624
    # runtime_services_dynamic_ports handles NATS and etcd startup
625
626
627
628
629
630
    if not use_kv_events:
        mode = "Approximate (no-kv-events)"
    elif use_nats_core:
        mode = "NATS Core (local indexer)"
    else:
        mode = "JetStream"
631
632
633
    logger.info(
        f"Starting test router prefix reuse and KV events synchronization ({mode})"
    )
634

Yan Ru Pei's avatar
Yan Ru Pei committed
635
    # Create mocker args dictionary with dp_size=4
636
    # Note: enable_local_indexer only applies when use_kv_events=True and use_nats_core=True
Yan Ru Pei's avatar
Yan Ru Pei committed
637
638
639
640
    mocker_args = {
        "speedup_ratio": SPEEDUP_RATIO,
        "block_size": BLOCK_SIZE,
        "dp_size": 4,
641
        "enable_local_indexer": use_nats_core and use_kv_events,
Yan Ru Pei's avatar
Yan Ru Pei committed
642
    }
643
644

    try:
Yan Ru Pei's avatar
Yan Ru Pei committed
645
        logger.info(
646
            f"Starting 2 mocker instances with dp_size=4 each (8 total dp ranks), {mode}"
647
        )
648
649
650
651
652
653
        mockers = MockerProcess(
            request,
            mocker_args=mocker_args,
            num_mockers=2,
            request_plane=request_plane,
        )
654
        logger.info(f"All mockers using endpoint: {mockers.endpoint}")
655

656
657
658
659
        # Initialize mockers
        mockers.__enter__()

        # Get runtime and create endpoint
660
        runtime = get_runtime(request_plane=request_plane)
661
662
663
664
665
        # Use the namespace from the mockers
        namespace = runtime.namespace(mockers.namespace)
        component = namespace.component("mocker")
        endpoint = component.endpoint("generate")

666
        _test_router_decisions(
667
668
669
670
671
672
            mockers,
            endpoint,
            MODEL_NAME,
            request,
            test_dp_rank=True,
            use_kv_events=use_kv_events,
673
674
675
676
677
        )

    finally:
        if "mockers" in locals():
            mockers.__exit__(None, None, None)
678
679


680
@pytest.mark.parametrize("request_plane", ["nats", "tcp"], indirect=True)
681
@pytest.mark.parametrize("registration_order", ["prefill_first", "decode_first"])
682
@pytest.mark.timeout(59)  # ~3x average (~19.51s), rounded up
683
def test_router_decisions_disagg(
684
685
686
687
688
    request,
    runtime_services_dynamic_ports,
    predownload_tokenizers,
    registration_order,
    request_plane,
689
690
691
692
693
):
    """Validate KV cache prefix reuse in disaggregated prefill-decode setup.

    Tests that progressive requests with overlapping prefixes are routed to the
    same prefill worker due to KV cache reuse.
694
695
696
697

    Parameterized to test both registration orders:
    - prefill_first: prefill workers register before decode workers
    - decode_first: decode workers register before prefill workers
698
    """
699
    # runtime_services_dynamic_ports handles NATS and etcd startup
700
701
702
703
    logger.info(
        f"Starting disaggregated router prefix reuse test "
        f"(registration_order={registration_order})"
    )
704
705
706
707
708
709
710
711
712
713
714
715

    # Generate shared namespace for prefill and decode workers
    namespace_suffix = generate_random_suffix()
    shared_namespace = f"test-namespace-{namespace_suffix}"

    # Create mocker args
    mocker_args = {"speedup_ratio": SPEEDUP_RATIO, "block_size": BLOCK_SIZE}

    prefill_workers = None
    decode_workers = None

    try:
716
717
718
719
720
721
722
723
724
        if registration_order == "prefill_first":
            # Start prefill workers first
            logger.info("Starting 4 prefill mocker instances (first)")
            prefill_workers = DisaggMockerProcess(
                request,
                namespace=shared_namespace,
                worker_type="prefill",
                mocker_args=mocker_args,
                num_mockers=4,
725
                request_plane=request_plane,
726
727
728
729
730
731
732
733
734
735
736
737
            )
            prefill_workers.__enter__()
            logger.info(f"Prefill workers using endpoint: {prefill_workers.endpoint}")

            # Then start decode workers
            logger.info("Starting 4 decode mocker instances (second)")
            decode_workers = DisaggMockerProcess(
                request,
                namespace=shared_namespace,
                worker_type="decode",
                mocker_args=mocker_args,
                num_mockers=4,
738
                request_plane=request_plane,
739
740
741
742
743
744
745
746
747
748
749
750
            )
            decode_workers.__enter__()
            logger.info(f"Decode workers using endpoint: {decode_workers.endpoint}")
        else:
            # Start decode workers first
            logger.info("Starting 4 decode mocker instances (first)")
            decode_workers = DisaggMockerProcess(
                request,
                namespace=shared_namespace,
                worker_type="decode",
                mocker_args=mocker_args,
                num_mockers=4,
751
                request_plane=request_plane,
752
753
754
755
756
757
758
759
760
761
762
763
            )
            decode_workers.__enter__()
            logger.info(f"Decode workers using endpoint: {decode_workers.endpoint}")

            # Then start prefill workers
            logger.info("Starting 4 prefill mocker instances (second)")
            prefill_workers = DisaggMockerProcess(
                request,
                namespace=shared_namespace,
                worker_type="prefill",
                mocker_args=mocker_args,
                num_mockers=4,
764
                request_plane=request_plane,
765
766
767
            )
            prefill_workers.__enter__()
            logger.info(f"Prefill workers using endpoint: {prefill_workers.endpoint}")
768
769

        # Get unique port for this test
770
771
772
        frontend_port = get_unique_ports(
            request, num_ports=1, registration_order=registration_order
        )[0]
773
774

        # Run disagg routing test
775
        _test_router_decisions_disagg(
776
777
778
779
780
781
            prefill_workers=prefill_workers,
            decode_workers=decode_workers,
            block_size=BLOCK_SIZE,
            request=request,
            frontend_port=frontend_port,
            test_payload=TEST_PAYLOAD,
782
            request_plane=request_plane,
783
784
785
786
787
788
789
        )

    finally:
        if decode_workers is not None:
            decode_workers.__exit__(None, None, None)
        if prefill_workers is not None:
            prefill_workers.__exit__(None, None, None)
790
791


792
@pytest.mark.parametrize("request_plane", ["nats", "tcp"], indirect=True)
793
@pytest.mark.timeout(39)  # ~3x average (~12.84s), rounded up
794
def test_busy_threshold_endpoint(
795
    request, runtime_services_dynamic_ports, predownload_tokenizers, request_plane
796
797
798
799
800
801
802
803
804
805
):
    """Test that the /busy_threshold endpoint can be hit and responds correctly.

    TODO: This doesn't actually test any e2e rejection for now. A proper test would:
    1. Set a very low threshold
    2. Send enough requests to exceed the threshold
    3. Verify that subsequent requests are rejected with 503

    For now, this test only verifies the endpoint is accessible and returns valid responses.
    """
806
    # runtime_services_dynamic_ports handles NATS and etcd startup
807
808
809
    logger.info(
        f"Starting busy_threshold endpoint test with request_plane={request_plane}"
    )
810
811
812
813
814
815

    mocker_args = {"speedup_ratio": SPEEDUP_RATIO, "block_size": BLOCK_SIZE}

    try:
        logger.info(f"Starting {NUM_MOCKERS} mocker instances")
        mockers = MockerProcess(
816
817
818
819
            request,
            mocker_args=mocker_args,
            num_mockers=NUM_MOCKERS,
            request_plane=request_plane,
820
821
822
823
        )
        logger.info(f"All mockers using endpoint: {mockers.endpoint}")
        mockers.__enter__()

824
825
826
        frontend_port = get_unique_ports(
            request, num_ports=1, request_plane=request_plane
        )[0]
827
828
829
830
831
832
833

        _test_busy_threshold_endpoint(
            engine_workers=mockers,
            block_size=BLOCK_SIZE,
            request=request,
            frontend_port=frontend_port,
            test_payload=TEST_PAYLOAD,
834
            request_plane=request_plane,
835
836
837
838
839
        )

    finally:
        if "mockers" in locals():
            mockers.__exit__(None, None, None)