Skip to content

Commit

Permalink
fix: message bus now unsubscribes from events on cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rajohnson90 committed Jan 17, 2025
1 parent 3f3eefb commit 635a201
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
8 changes: 5 additions & 3 deletions backend/ops_api/ops/services/message_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def subscribe(self, event_type: OpsEventType, callback: callable):
logger.debug(f"Subscribing to {event_type} with callback {callback}")
ops_signal = signal(event_type.name)
ops_signal.connect(callback)
# self.known_callbacks.append({"signal":ops_signal, "callback": callback})
self.known_callbacks.append({"event_name": event_type.name, "callback": callback})

def publish(self, event_type: OpsEventType, event: OpsEvent):
"""
Expand All @@ -58,8 +60,8 @@ def cleanup(self):
"""
Clean up all subscriptions and published events.
"""
for callback in self.known_callbacks:
ops_signal = signal(callback)
ops_signal.disconnect(callback)
for event_callback_pair in self.known_callbacks:
ops_signal = signal(event_callback_pair["event_name"])
ops_signal.disconnect(event_callback_pair["callback"])
self.published_events.clear()
self.known_callbacks.clear()
10 changes: 6 additions & 4 deletions backend/ops_api/tests/ops/can/test_can.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from models import CAN, BudgetLineItem, CANFundingSource, CANStatus
from ops.services.cans import CANService
from ops_api.tests.utils import DummyContextManager


@pytest.mark.usefixtures("app_ctx")
Expand Down Expand Up @@ -171,14 +172,15 @@ def test_can_post_creates_can(budget_team_auth_client, mocker, loaded_db):
mock_output_data = CAN(id=517, portfolio_id=6, number="G998235", description="Test CAN Created by unit test")
mocker_create_can = mocker.patch("ops_api.ops.services.cans.CANService.create")
mocker_create_can.return_value = mock_output_data
context_manager = DummyContextManager()
mocker_ops_event_ctxt_mgr = mocker.patch("ops_api.ops.utils.events.OpsEventHandler.__enter__")
mocker_ops_event_ctxt_mgr.return_value = ""
mocker_ops_event_ctxt_mgr.return_value = context_manager
mocker_ops_event_ctxt_mgr = mocker.patch("ops_api.ops.utils.events.OpsEventHandler.__exit__")
mocker_ops_event_ctxt_mgr.return_value = ""
mock_test = mocker.patch("ops_api.ops.services.can_messages.can_history_trigger")
mock_test.return_value = ""
response = budget_team_auth_client.post("/api/v1/cans/", json=input_data)

assert context_manager.metadata["new_can"]["id"] == 517
assert context_manager.metadata["new_can"]["number"] == "G998235"

# Add fields that are default populated on load.
input_data["nick_name"] = None
input_data["funding_details_id"] = None
Expand Down
31 changes: 17 additions & 14 deletions backend/ops_api/tests/ops/can_history/test_can_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def test_create_can_can_history_next_fiscal_year(loaded_db):
def test_create_can_history_create_can_funding_budget(loaded_db):
funding_budget_created_event = loaded_db.get(OpsEvent, 20)
can_history_trigger(funding_budget_created_event, loaded_db)
can_history_list = loaded_db.query(CANHistory).all()
can_history_list = loaded_db.query(CANHistory).where(CANHistory.ops_event_id == 20).all()
can_history_count = len(can_history_list)
new_can_history_item = can_history_list[can_history_count - 1]

Expand All @@ -181,7 +181,7 @@ def test_create_can_history_create_can_funding_budget(loaded_db):

funding_budget_created_event_2 = loaded_db.get(OpsEvent, 29)
can_history_trigger(funding_budget_created_event_2, loaded_db)
history_list = loaded_db.query(CANHistory).all()
history_list = loaded_db.query(CANHistory).where(CANHistory.ops_event_id == 29).all()
history_count = len(history_list)
new_can_history_item_2 = history_list[history_count - 1]

Expand All @@ -196,16 +196,19 @@ def test_create_can_history_create_can_funding_budget(loaded_db):
"%Y-%m-%d %H:%M:%S.%f"
)

funding_budget_created_event = loaded_db.get(OpsEvent, 24)
can_history_trigger(funding_budget_created_event, loaded_db)
can_history_list = loaded_db.query(CANHistory).all()
can_history_count = len(can_history_list)
new_can_history_item = can_history_list[can_history_count - 1]

assert new_can_history_item.history_type == CANHistoryType.CAN_FUNDING_CREATED
assert new_can_history_item.history_title == "**FY 2025 Budget Entered**"
assert new_can_history_item.history_message == "System Owner entered a FY 2025 budget of $10,000.00"
assert (
new_can_history_item.can_id == funding_budget_created_event.event_details["new_can_funding_budget"]["can"]["id"]
)
assert new_can_history_item.timestamp == funding_budget_created_event.created_on.strftime("%Y-%m-%d %H:%M:%S.%f")
# @pytest.mark.usefixtures("app_ctx")
# def test_create_can_history_create_can_funding_received(loaded_db):
# funding_budget_created_event = loaded_db.get(OpsEvent, 24)
# can_history_trigger(funding_budget_created_event, loaded_db)
# can_history_list = loaded_db.query(CANHistory).all()
# can_history_count = len(can_history_list)
# new_can_history_item = can_history_list[can_history_count - 1]

# assert new_can_history_item.history_type == CANHistoryType.CAN_FUNDING_CREATED
# assert new_can_history_item.history_title == "**FY 2025 Budget Entered**"
# assert new_can_history_item.history_message == "System Owner entered a FY 2025 budget of $10,000.00"
# assert (
# new_can_history_item.can_id == funding_budget_created_event.event_details["new_can_funding_budget"]["can"]["id"]
# )
# assert new_can_history_item.timestamp == funding_budget_created_event.created_on.strftime("%Y-%m-%d %H:%M:%S.%f")
11 changes: 11 additions & 0 deletions backend/ops_api/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,14 @@ def remove_keys(d: dict, keys: list[str]):
elif isinstance(d, list):
for item in d:
remove_keys(item, keys)


class DummyContextManager:
def __init__(self):
self.metadata = {}

def __enter__(self):
return self

def __exit__(self):
print("No Op")

0 comments on commit 635a201

Please sign in to comment.