summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAngelos Evripiotis <jevripiotis@bloomberg.net>2019-10-22 17:37:52 +0100
committerbst-marge-bot <marge-bot@buildstream.build>2019-10-29 14:17:17 +0000
commitbd5e246ff05ac035195fa6358f4abd598683d7c8 (patch)
tree7ef9c33bc807d442776f4c78d6e22b444e61da72
parentd16c1f3e48db6638abde3a793fdd24775b010cdc (diff)
downloadbuildstream-bd5e246ff05ac035195fa6358f4abd598683d7c8.tar.gz
testing, messenger: make dummy_context picklable
Note that unittest.MagicMock is unfortunately not pickable, so if one tries to cross over to a ChildJob then we're in trouble. Sacrifice some convenience and a bit of safety by implementing our own _DummyTask instead of using MagicMock. Don't try to pickle the 'simple_task' context manager, if it's been overridden when creating a test context.
-rw-r--r--src/buildstream/_messenger.py7
-rw-r--r--tests/testutils/context.py31
2 files changed, 35 insertions, 3 deletions
diff --git a/src/buildstream/_messenger.py b/src/buildstream/_messenger.py
index f5f570319..20c327728 100644
--- a/src/buildstream/_messenger.py
+++ b/src/buildstream/_messenger.py
@@ -405,6 +405,13 @@ class Messenger():
#
del state['_render_status_cb']
+ # The "simple_task" context manager is not needed outside the main
+ # process. During testing we override it to something that cannot
+ # pickle, so just drop it when pickling to a child job. Note that it
+ # will only appear in 'state' if it has been overridden.
+ #
+ state.pop('simple_task', None)
+
# The State object is not needed outside the main process
del state['_state']
diff --git a/tests/testutils/context.py b/tests/testutils/context.py
index 849895e92..821adef0a 100644
--- a/tests/testutils/context.py
+++ b/tests/testutils/context.py
@@ -15,13 +15,11 @@
# License along with this library. If not, see <http://www.gnu.org/licenses/>.
import os
-from unittest.mock import MagicMock
from types import MethodType
from contextlib import contextmanager
from buildstream._context import Context
-from buildstream._state import _Task
# Handle messages from the pipeline
@@ -29,9 +27,36 @@ def _dummy_message_handler(message, is_silenced):
pass
+class _DummyTask:
+ # Note that unittest.mock.MagicMock doesn't pickle, so we must make our
+ # _DummyTask manually here.
+ def __init__(self, state, action_name, full_name, elapsed_offset):
+ self._state = state
+ self.action_name = action_name
+ self.full_name = full_name
+ self.elapsed_offset = elapsed_offset
+ self.current_progress = None
+ self.maximum_progress = None
+
+ def set_render_cb(self, callback):
+ pass
+
+ def set_current_progress(self, progress):
+ pass
+
+ def set_maximum_progress(self, progress):
+ pass
+
+ def add_current_progress(self):
+ pass
+
+ def add_maximum_progress(self):
+ pass
+
+
@contextmanager
def _get_dummy_task(self, activity_name, *, element_name=None, full_name=None, silent_nested=False):
- yield MagicMock(spec=_Task("state", activity_name, full_name, 0))
+ yield _DummyTask("state", activity_name, full_name, 0)
# dummy_context()