From 1ba52c95f4e73b616c48100998d5ee6d51231226 Mon Sep 17 00:00:00 2001 From: shadeofblue Date: Mon, 10 Jun 2019 11:45:24 +0200 Subject: [PATCH 1/4] treat retrieval of an incompatible message from history as a "message not found" --- golem/network/history.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/golem/network/history.py b/golem/network/history.py index ba4636df9a..8e15e23c47 100644 --- a/golem/network/history.py +++ b/golem/network/history.py @@ -119,7 +119,13 @@ def get_sync_as_message(cls, *args, **kwargs) -> message.base.Message: if not db_result: raise MessageNotFound() db_msg = db_result[0] - return db_msg.as_message() + try: + return db_msg.as_message() + except AttributeError: + # in case an incompatible message from an earlier version of + # golem-messages is retrieved, just treat it the same + # as if the message was not found + raise MessageNotFound() def add(self, msg_dict: dict) -> None: """ From 88bf2aa8d6532b2eab8c6b74ff86a80f81706271 Mon Sep 17 00:00:00 2001 From: shadeofblue Date: Mon, 10 Jun 2019 12:28:07 +0200 Subject: [PATCH 2/4] =?UTF-8?q?"prosz=C4=99,=20prosz=C4=99,=20przecie?= =?UTF-8?q?=C5=BC=20Panu=20nie=20zjem!"...=20;)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/golem/network/test_history.py | 45 +++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/tests/golem/network/test_history.py b/tests/golem/network/test_history.py index 708a1ddba1..acc32e5fab 100644 --- a/tests/golem/network/test_history.py +++ b/tests/golem/network/test_history.py @@ -21,8 +21,7 @@ def message_count(): return NetworkMessage.select().count() - -class TestMessageHistoryService(DatabaseFixture): +class MessageHistoryServiceTestBase(DatabaseFixture): def setUp(self): super().setUp() @@ -32,6 +31,8 @@ def tearDown(self): super().tearDown() history.MessageHistoryService.instance = None + +class TestMessageHistoryService(MessageHistoryServiceTestBase): @staticmethod def _build_dict(task=None, subtask=None): return dict( @@ -256,6 +257,46 @@ def test_loop_remove_sync(self): assert not self.service.remove_sync.called +class TestMessageHistoryGet(MessageHistoryServiceTestBase): + def setUp(self): + super().setUp() + self.msg = msg_factories.tasks.TaskToComputeFactory() + self.msg._fake_sign() + self.node_id = fake.binary(length=64) + self.local_role = fake.random_element(Actor) + self.remote_role = fake.random_element(Actor) + + history.add( + msg=self.msg, + node_id=self.node_id, + local_role=self.local_role, + remote_role=self.remote_role, + sync=True + ) + + def test_get(self): + msg_retrieved = history.get( + 'TaskToCompute', + subtask_id=self.msg.subtask_id, + node_id=self.node_id, + ) + + self.assertEqual(self.msg, msg_retrieved) + + def test_get_pickle_fail(self): + with mock.patch( + 'golem.model.pickle.loads', + mock.Mock(side_effect=AttributeError) + ): + msg_retrieved = history.get( + 'TaskToCompute', + subtask_id=self.msg.subtask_id, + node_id=self.node_id, + ) + + self.assertIsNone(msg_retrieved) + + @mock.patch("golem.network.history.MessageHistoryService.add") class TestAdd(unittest.TestCase): def setUp(self): From 0d426953975d8354c6770042d5737cbe93104500 Mon Sep 17 00:00:00 2001 From: shadeofblue Date: Mon, 10 Jun 2019 12:34:26 +0200 Subject: [PATCH 3/4] lint --- tests/golem/network/test_history.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/golem/network/test_history.py b/tests/golem/network/test_history.py index acc32e5fab..e5e6935988 100644 --- a/tests/golem/network/test_history.py +++ b/tests/golem/network/test_history.py @@ -21,6 +21,7 @@ def message_count(): return NetworkMessage.select().count() + class MessageHistoryServiceTestBase(DatabaseFixture): def setUp(self): From ae3b37bab61ee2829868ef9fca785c15f61c2fb9 Mon Sep 17 00:00:00 2001 From: shadeofblue Date: Mon, 10 Jun 2019 12:47:17 +0200 Subject: [PATCH 4/4] lint. ... --- tests/golem/network/test_history.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/golem/network/test_history.py b/tests/golem/network/test_history.py index e5e6935988..c345dad8e7 100644 --- a/tests/golem/network/test_history.py +++ b/tests/golem/network/test_history.py @@ -285,10 +285,8 @@ def test_get(self): self.assertEqual(self.msg, msg_retrieved) def test_get_pickle_fail(self): - with mock.patch( - 'golem.model.pickle.loads', - mock.Mock(side_effect=AttributeError) - ): + with mock.patch('golem.model.pickle.loads', + mock.Mock(side_effect=AttributeError)): msg_retrieved = history.get( 'TaskToCompute', subtask_id=self.msg.subtask_id,