Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Send evt.net.connection only when status changes #4958

Merged
merged 2 commits into from
Dec 3, 2019
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
14 changes: 12 additions & 2 deletions golem/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1583,14 +1583,24 @@ def __init__(self,
interval_seconds: int) -> None:
super().__init__(interval_seconds)
self._client = client
self._last_value = self.poll()

def _run_async(self):
# Skip the async_run call and publish events in the main thread
self._run()

def _run(self):
self._client._publish(Network.evt_connection,
self._client.connection_status())
current_value = self.poll()
if current_value == self._last_value:
return
self._last_value = current_value
self._client._publish( # pylint: disable=protected-access
Network.evt_connection,
self._last_value,
)

def poll(self) -> Dict[str, Any]:
return self._client.connection_status()


class TaskArchiverService(LoopingCallService):
Expand Down
4 changes: 3 additions & 1 deletion tests/golem/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,9 @@ def setUp(self):
)

@patch('golem.client.logger')
def test_run(self, logger):
@patch('golem.client.NetworkConnectionPublisherService.poll')
def test_run(self, poll_mock, logger):
poll_mock.return_value = {'random_key': random.random()}
Copy link
Contributor

Choose a reason for hiding this comment

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

This might very rarely fail, because it might produce the same value twice. You can assign an array to side_effect and it will return next value for each call.

Copy link
Contributor Author

@jiivan jiivan Dec 2, 2019

Choose a reason for hiding this comment

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

It won't fail, because service is initialized in .setUp() which uses original .poll() implementation, that returns totally different kind of dict. Also this value is returned only once.

https://github.com/golemfactory/golem/blob/3b92e82da512a7d3af9c8e31fbdf50638630dc1a/golem/client.py#L1329-L1342

self.service._run()

logger.debug.assert_not_called()
Expand Down