Skip to content

Commit

Permalink
Add EventsEndpoint.send_event method
Browse files Browse the repository at this point in the history
  • Loading branch information
kozlovsky committed Apr 14, 2023
1 parent d983a06 commit ecfe264
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
11 changes: 7 additions & 4 deletions src/tribler/core/components/restapi/rest/events_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ def __init__(self, notifier: Notifier, public_key: str = None):

def on_notification(self, topic, *args, **kwargs):
if topic in topics_to_send_to_gui:
data = {"topic": topic.__name__, "args": args, "kwargs": kwargs}
self.async_group.add_task(self.write_data(data))
self.send_event({"topic": topic.__name__, "args": args, "kwargs": kwargs})

def on_circuit_removed(self, circuit: Circuit, additional_info: str):
# The original notification contains non-JSON-serializable argument, so we send another one to GUI
Expand Down Expand Up @@ -117,7 +116,11 @@ def should_skip_message(self, message: MessageDict) -> bool:

return False

async def write_data(self, message: MessageDict):
def send_event(self, message: MessageDict):
if not self.should_skip_message(message):
self.async_group.add_task(self._write_data(message))

async def _write_data(self, message: MessageDict):
"""
Write data over the event socket if it's open.
"""
Expand All @@ -144,7 +147,7 @@ def on_tribler_exception(self, reported_error: ReportedError):

message = self.error_message(reported_error)
if self.has_connection_to_gui():
self.async_group.add_task(self.write_data(message))
self.send_event(message)
elif not self.undelivered_error:
# If there are several undelivered errors, we store the first error as more important and skip other
self.undelivered_error = message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,18 @@ async def test_events(rest_manager, notifier: Notifier):
await event_socket_task


@patch.object(EventsEndpoint, 'write_data')
@patch.object(EventsEndpoint, '_write_data')
@patch.object(EventsEndpoint, 'has_connection_to_gui', new=MagicMock(return_value=True))
async def test_on_tribler_exception_has_connection_to_gui(mocked_write_data, events_endpoint, reported_error):
# test that in case of established connection to GUI, `on_tribler_exception` will work
# as a normal events_endpoint function, that is call `write_data`
# as a normal events_endpoint function, that is call `_write_data`
events_endpoint.on_tribler_exception(reported_error)

mocked_write_data.assert_called_once()
assert not events_endpoint.undelivered_error


@patch.object(EventsEndpoint, 'write_data')
@patch.object(EventsEndpoint, '_write_data')
@patch.object(EventsEndpoint, 'has_connection_to_gui', new=MagicMock(return_value=False))
async def test_on_tribler_exception_no_connection_to_gui(mocked_write_data, events_endpoint, reported_error):
# test that if no connection to GUI, then `on_tribler_exception` will store
Expand All @@ -136,7 +136,7 @@ async def test_on_tribler_exception_no_connection_to_gui(mocked_write_data, even
assert events_endpoint.undelivered_error == events_endpoint.error_message(reported_error)


@patch.object(EventsEndpoint, 'write_data', new=MagicMock())
@patch.object(EventsEndpoint, '_write_data', new=MagicMock())
@patch.object(EventsEndpoint, 'has_connection_to_gui', new=MagicMock(return_value=False))
async def test_on_tribler_exception_stores_only_first_error(events_endpoint, reported_error):
# test that if no connection to GUI, then `on_tribler_exception` will store
Expand Down

0 comments on commit ecfe264

Please sign in to comment.