Skip to content

Fix routes not clearing due to sync re-populating them #372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions aikido_zen/background_process/routes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ def get(self, route_metadata):
key = route_to_key(route_metadata)
return self.routes.get(key)

def get_routes_with_hits(self):
"""Gets you all routes with a positive hits delta"""
result = dict()
for key, route in self.routes.items():
if route["hits_delta_since_sync"] <= 0:
continue # do not add routes without a hit delta
result[key] = route
return result

def clear(self):
"""Deletes all routes"""
self.routes = {}
Expand Down
2 changes: 1 addition & 1 deletion aikido_zen/thread/thread_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def renew(self):
res = comms.get_comms().send_data_to_bg_process(
action="SYNC_DATA",
obj={
"current_routes": dict(self.routes.routes),
"current_routes": self.routes.get_routes_with_hits(),
"reqs": self.reqs,
"middleware_installed": self.middleware_installed,
},
Expand Down
79 changes: 79 additions & 0 deletions aikido_zen/thread/thread_cache_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,82 @@ def test_parses_routes_correctly(
"apispec": {},
},
]


@patch("aikido_zen.background_process.comms.get_comms")
def test_renew_called_with_correct_args(mock_get_comms, thread_cache: ThreadCache):
"""Test that renew calls send_data_to_bg_process with correct arguments."""
mock_comms = MagicMock()
mock_get_comms.return_value = mock_comms

# Setup initial state
thread_cache.increment_stats()
thread_cache.routes.initialize_route({"method": "GET", "route": "/test"})
thread_cache.routes.increment_route({"method": "GET", "route": "/test"})

# Call renew
thread_cache.renew()

# Assert that send_data_to_bg_process was called with the correct arguments
mock_comms.send_data_to_bg_process.assert_called_once_with(
action="SYNC_DATA",
obj={
"current_routes": {
"GET:/test": {
"method": "GET",
"path": "/test",
"hits": 1,
"hits_delta_since_sync": 1,
"apispec": {},
}
},
"reqs": 1,
"middleware_installed": False,
},
receive=True,
)


@patch("aikido_zen.background_process.comms.get_comms")
def test_renew_called_with_empty_routes(mock_get_comms, thread_cache: ThreadCache):
"""Test that renew calls send_data_to_bg_process with empty routes."""
mock_comms = MagicMock()
mock_get_comms.return_value = mock_comms

# Call renew without initializing any routes
thread_cache.renew()

# Assert that send_data_to_bg_process was called with the correct arguments
mock_comms.send_data_to_bg_process.assert_called_once_with(
action="SYNC_DATA",
obj={
"current_routes": {},
"reqs": 0,
"middleware_installed": False,
},
receive=True,
)


@patch("aikido_zen.background_process.comms.get_comms")
def test_renew_called_with_no_requests(mock_get_comms, thread_cache: ThreadCache):
"""Test that renew calls send_data_to_bg_process with zero requests."""
mock_comms = MagicMock()
mock_get_comms.return_value = mock_comms

# Setup initial state with a route but no requests
thread_cache.routes.initialize_route({"method": "GET", "route": "/test"})

# Call renew
thread_cache.renew()

# Assert that send_data_to_bg_process was called with the correct arguments
mock_comms.send_data_to_bg_process.assert_called_once_with(
action="SYNC_DATA",
obj={
"current_routes": {},
"reqs": 0,
"middleware_installed": False,
},
receive=True,
)