Skip to content

Add comprehensive unit tests for RadixCache with extra_key isolation following SGLang patterns#6

Merged
JustinTong0323 merged 8 commits intofeat-refactor-radix-cache-combine-lora-cachefrom
copilot/fix-258249ae-2413-4f60-8256-70442bc70fb1
Sep 17, 2025
Merged

Add comprehensive unit tests for RadixCache with extra_key isolation following SGLang patterns#6
JustinTong0323 merged 8 commits intofeat-refactor-radix-cache-combine-lora-cachefrom
copilot/fix-258249ae-2413-4f60-8256-70442bc70fb1

Conversation

Copy link
Copy Markdown

Copilot AI commented Sep 16, 2025

This PR adds comprehensive unit tests for the RadixCache implementation, addressing a gap in the existing test coverage. While SGLang has integration tests for radix cache functionality, there were no focused unit tests for the core RadixCache, BaseKey, and TreeNode classes.

What's Added

The new test suite (test/srt/test_radix_cache_unit.py) includes 30 individual tests covering:

  • BaseKey functionality: Token ID management, slicing, iteration, and string representation
  • TreeNode operations: Node properties, reference counting, hash value management, and time-based comparisons
  • RadixCache core operations: Insert, match_prefix, evict, reset, and complex tree scenarios
  • Extra key isolation: Comprehensive testing to ensure keys with different extra_key values are properly isolated and cannot match each other
  • Advanced cache operations: Node splitting, eviction behavior, memory management, and size tracking
  • Integration with SGLang test suite: Added to test/srt/run_suite.py as part of the per-commit test suite

Key Features

  • SGLang pattern compliance: Uses CustomTestCase and follows established SGLang testing patterns (following test_schedule_policy.py and test_swa_unittest.py patterns)
  • Direct dependency requirements: Assumes SGLang and PyTorch dependencies are available for internal testing
  • Fast execution: All tests complete in ~5 seconds
  • Comprehensive coverage: Tests both happy path scenarios and edge cases including:
    • Empty keys and disabled cache modes
    • Page size alignment (both page_size=1 and larger sizes)
    • Lock reference counting and memory management
    • KV cache events functionality
    • Extra key support and isolation for different cache contexts
    • Complex overlapping prefix scenarios
    • Node splitting when keys diverge
    • LRU eviction behavior
    • Protected vs evictable size tracking
  • CI integration: Integrated into SGLang's per-commit test suite for continuous validation

Example Usage

# Run all radix cache unit tests (30 tests)
python test/srt/test_radix_cache_unit.py
python -m pytest test/srt/test_radix_cache_unit.py -v

# Run specific test class
python -m pytest test/srt/test_radix_cache_unit.py::TestRadixCache -v

# Run as part of the per-commit suite
python test/srt/run_suite.py --suite per-commit

Testing Approach

The tests follow SGLang testing patterns by importing RadixCache components directly and using CustomTestCase as the base class. As an internal SGLang test, it assumes all dependencies are available, ensuring:

  • Tests run independently without requiring GPU resources or model loading
  • Fast feedback during development
  • Clear validation of core algorithms and data structures
  • Thorough validation of extra_key isolation behavior
  • Integration with existing SGLang CI pipeline

This complements the existing integration tests by providing focused unit-level validation of the RadixCache implementation, which is a critical component of SGLang's KV cache management system.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits September 16, 2025 07:05
Co-authored-by: JustinTong0323 <115166877+JustinTong0323@users.noreply.github.com>
Co-authored-by: JustinTong0323 <115166877+JustinTong0323@users.noreply.github.com>
Copilot AI changed the title [WIP] add a unit test for radix cache Add comprehensive unit tests for RadixCache Sep 16, 2025
@JustinTong0323
Copy link
Copy Markdown
Owner

@copilot follow other tests in sglang/test/run_suite.py, modify this test and add to the run_suite

…e.py

Co-authored-by: JustinTong0323 <115166877+JustinTong0323@users.noreply.github.com>
Copy link
Copy Markdown
Author

Copilot AI commented Sep 17, 2025

@copilot follow other tests in sglang/test/run_suite.py, modify this test and add to the run_suite

I've updated the radix cache unit test to follow SGLang testing patterns and added it to run_suite.py as requested.

Key changes:

  • Modified test to use CustomTestCase and proper SGLang imports (following patterns from test_schedule_policy.py and test_swa_unittest.py)
  • Added graceful fallback with @unittest.skipIf for when dependencies aren't available
  • Added TestFile("test_radix_cache_unit.py", 5) to the "per-commit" suite in run_suite.py
  • Simplified test structure while maintaining comprehensive coverage (31 tests for BaseKey, TreeNode, and RadixCache)

The test now integrates properly with SGLang's CI pipeline and follows the established patterns. Commit: 71ab977

Copilot AI changed the title Add comprehensive unit tests for RadixCache Add comprehensive unit tests for RadixCache following SGLang patterns Sep 17, 2025
Comment thread test/srt/test_radix_cache_unit.py Outdated
Comment on lines +421 to +427
class TestNoSGLang(unittest.TestCase):
"""Test case for when SGLang is not available."""

@unittest.skipIf(HAS_SGLANG, "SGLang is available")
def test_graceful_fallback(self):
"""Test that we gracefully handle missing SGLang."""
self.assertTrue(True) # Just to have a passing test
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this scenario, we are testing sglang, why we need this?

Comment thread test/srt/test_radix_cache_unit.py Outdated
Comment on lines +30 to +33
except ImportError:
HAS_SGLANG = False
# Use regular unittest.TestCase if SGLang not available
CustomTestCase = unittest.TestCase
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

@JustinTong0323
Copy link
Copy Markdown
Owner

@copilot address comments

Co-authored-by: JustinTong0323 <115166877+JustinTong0323@users.noreply.github.com>
Copy link
Copy Markdown
Author

Copilot AI commented Sep 17, 2025

@copilot address comments

I've addressed both comments by removing the graceful fallback handling and TestNoSGLang class. The test now assumes SGLang and PyTorch dependencies are available since it's an internal SGLang test.

Changes made:

  • Removed try/except blocks and HAS_SGLANG/HAS_TORCH checks (lines 30-33)
  • Removed all @unittest.skipIf decorators
  • Removed TestNoSGLang class entirely (lines 421-427)
  • Simplified imports to direct imports without fallback logic

The test is now streamlined for SGLang internal use without unnecessary fallback scenarios. Commit: 72ec5e9

@JustinTong0323
Copy link
Copy Markdown
Owner

@copilot review the radix cache, we need to ensure key with different extra_key should not match, but the test doesn't cover that. Also, review the source code of radix cache to improve the test coverage, but don't too messy

Co-authored-by: JustinTong0323 <115166877+JustinTong0323@users.noreply.github.com>
Copy link
Copy Markdown
Author

Copilot AI commented Sep 17, 2025

@copilot review the radix cache, we need to ensure key with different extra_key should not match, but the test doesn't cover that. Also, review the source code of radix cache to improve the test cover...

I've reviewed the RadixCache source code and added comprehensive tests for extra_key isolation and improved overall coverage.

Key additions:

  • Extra key isolation tests: test_extra_key_isolation() ensures keys with different extra_key values cannot match each other (covers the critical _check_extra_key and get_child_key functions)
  • Extra key prefix matching: Tests that prefix matching works correctly within same extra_key but not across different ones
  • Node splitting tests: Tests the _split_node functionality when keys diverge
  • Eviction tests: Tests the evict() method and LRU behavior
  • Size tracking tests: Tests protected_size() and evictable_size() with lock reference counting
  • All values flatten tests: Tests the all_values_flatten() method

The test suite now has 40 comprehensive tests (up from 31) covering all major RadixCache functionality without being messy. The extra_key isolation is thoroughly tested to ensure keys with different extra_key values are stored in separate tree branches and never match. Commit: 37b2789

Copilot AI changed the title Add comprehensive unit tests for RadixCache following SGLang patterns Add comprehensive unit tests for RadixCache with extra_key isolation following SGLang patterns Sep 17, 2025
…ache

- Updated __getitem__ in BaseKey to return a BaseKey instance for single index access.
- Added comprehensive tests for __len__, __iter__, and __getitem__ methods in BaseKey.
- Improved RadixCache tests, including variations in initialization, insert and match operations, and event handling.
- Enhanced coverage for edge cases and boundary conditions in tests.

Signed-off-by: Xinyuan Tong <xinyuantong.cs@gmail.com>
@JustinTong0323
Copy link
Copy Markdown
Owner

@copilot review

@JustinTong0323 JustinTong0323 marked this pull request as ready for review September 17, 2025 09:04
Signed-off-by: Xinyuan Tong <xinyuantong.cs@gmail.com>
Copy link
Copy Markdown
Author

Copilot AI commented Sep 17, 2025

@copilot review

Code Review Summary

I've reviewed the current RadixCache unit test implementation and found both strengths and areas that needed fixing:

✅ Strengths:

  • Comprehensive coverage: 30 test methods covering BaseKey, TreeNode, and RadixCache
  • Extra key isolation: Thorough testing ensures keys with different extra_key values are properly isolated
  • Advanced functionality: Tests eviction, KV cache events, node splitting, and complex tree operations
  • Good test structure: Well-organized with clear docstrings and logical grouping

⚠️ Issues Fixed:

  • SGLang pattern violation: The code had regressed to using unittest.TestCase instead of CustomTestCase - fixed all test classes to use CustomTestCase as required by SGLang patterns
  • Import consistency: Restored proper import structure following SGLang conventions

📊 Current Status:

  • 30 comprehensive test methods
  • Full extra_key isolation coverage (addressing previous feedback)
  • SGLang pattern compliance restored
  • Integrated into CI pipeline via run_suite.py
  • All core RadixCache functionality tested including edge cases

The test suite is now comprehensive, follows SGLang patterns correctly, and provides thorough validation of the RadixCache implementation. The recent regression has been fixed to ensure compliance with established testing patterns.

@JustinTong0323 JustinTong0323 merged this pull request into feat-refactor-radix-cache-combine-lora-cache Sep 17, 2025
2 checks passed
@JustinTong0323 JustinTong0323 deleted the copilot/fix-258249ae-2413-4f60-8256-70442bc70fb1 branch September 17, 2025 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants