-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Misc] Implement Singleton Design Pattern for EngineStat Scraper, Req…
…uestStat Monitor, and Router (#131) * update singleton implementation pattern for engine_stat, request_stat and router scraper Signed-off-by: sitloboi2012 <[email protected]> * update fastapi to use state as built-in singleton method aside with custom singleton Signed-off-by: sitloboi2012 <[email protected]> --------- Signed-off-by: sitloboi2012 <[email protected]>
- Loading branch information
1 parent
8134ea5
commit 5247c69
Showing
5 changed files
with
160 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# test_singleton.py | ||
import unittest | ||
|
||
# Import the classes and helper functions from your module. | ||
from vllm_router.request_stats import ( | ||
GetRequestStatsMonitor, | ||
InitializeRequestStatsMonitor, | ||
RequestStatsMonitor, | ||
SingletonMeta, | ||
) | ||
|
||
|
||
class TestRequestStatsMonitorSingleton(unittest.TestCase): | ||
def setUp(self): | ||
# Clear any existing singleton instance for RequestStatsMonitor | ||
if RequestStatsMonitor in SingletonMeta._instances: | ||
del SingletonMeta._instances[RequestStatsMonitor] | ||
|
||
def test_singleton_initialization(self): | ||
sliding_window = 10.0 | ||
# First initialization using the helper. | ||
monitor1 = InitializeRequestStatsMonitor(sliding_window) | ||
# Subsequent retrieval using GetRequestStatsMonitor() should return the same instance. | ||
monitor2 = GetRequestStatsMonitor() | ||
self.assertIs( | ||
monitor1, | ||
monitor2, | ||
"GetRequestStatsMonitor should return the initialized singleton.", | ||
) | ||
|
||
# Directly calling the constructor with the same parameter should also return the same instance. | ||
monitor3 = RequestStatsMonitor(sliding_window) | ||
self.assertIs( | ||
monitor1, | ||
monitor3, | ||
"Direct constructor calls should return the same singleton instance.", | ||
) | ||
|
||
def test_initialization_without_parameter_after_initialized(self): | ||
sliding_window = 10.0 | ||
# First, initialize with the sliding_window. | ||
monitor1 = InitializeRequestStatsMonitor(sliding_window) | ||
# Now, calling the constructor without a parameter should not raise an error | ||
# and should return the already initialized instance. | ||
monitor2 = RequestStatsMonitor() | ||
self.assertIs( | ||
monitor1, | ||
monitor2, | ||
"Calling RequestStatsMonitor() without parameter after initialization should return the singleton.", | ||
) | ||
|
||
def test_initialization_without_parameter_before_initialized(self): | ||
# Ensure no instance is present. | ||
if RequestStatsMonitor in SingletonMeta._instances: | ||
del SingletonMeta._instances[RequestStatsMonitor] | ||
# Calling the constructor without the sliding_window parameter before initialization should raise a ValueError. | ||
with self.assertRaises(ValueError): | ||
RequestStatsMonitor() # This should fail because sliding_window_size is required on first init. | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.