From 805abde11909c14e9b406dbfcadba0b2d2a3ae12 Mon Sep 17 00:00:00 2001 From: Lucas Meneghel Rodrigues Date: Mon, 9 Jun 2014 22:43:42 -0300 Subject: [PATCH] avocado.test: Add Test __getstate__ for serialization In order to make the Test class serializable, implement __getstate__ in a way that only exports the pickleable attributes. This will be necessary for the following patches that will insulate individual test executions in forked processes. Signed-off-by: Lucas Meneghel Rodrigues --- avocado/test.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/avocado/test.py b/avocado/test.py index 16d8af5e..caeb1507 100644 --- a/avocado/test.py +++ b/avocado/test.py @@ -51,9 +51,11 @@ def log_exc_info(exc_info): :param exc_info: Exception info produced by sys.exc_info() """ + log.error('') for line in tb_info(exc_info): for l in line.splitlines(): log.error(l) + log.error('') def prepare_exc_info(exc_info): @@ -102,6 +104,7 @@ class Test(unittest.TestCase): if params is None: params = {} self.params = Params(params) + self._raw_params = params shortname = self.params.get('shortname') s_tag = None @@ -152,6 +155,13 @@ class Test(unittest.TestCase): self.log.debug('Test instance params override defaults whenever available') self.log.debug('') + # If there's a timeout set, log a timeout reminder + if hasattr(self.params, 'timeout'): + self.log.info('Test timeout set. Will wait %.2f s for ' + 'PID %s to end', + float(self.params.timeout), os.getpid()) + self.log.info('') + self.debugdir = None self.resultsdir = None self.status = None @@ -169,6 +179,26 @@ class Test(unittest.TestCase): def __repr__(self): return "Test(%r)" % self.tagged_name + def __getstate__(self): + """ + Pickle only selected attributes of the class for serialization. + + The fact we serialize the class means you'll have to modify this + class if you intend to make significant changes to its structure. + """ + orig = dict(self.__dict__) + d = {} + preserve_attr = ['basedir', 'debugdir', 'depsdir', 'fail_class', + 'fail_reason', 'logdir', 'logfile', 'name', + 'resultsdir', 'srcdir', 'status', 'sysinfodir', + 'tag', 'tagged_name', 'text_output', 'time_elapsed', + 'traceback', 'workdir'] + for key in sorted(orig): + if key in preserve_attr: + d[key] = orig[key] + d['params'] = orig['_raw_params'] + return d + def _set_default(self, key, default): try: self.params[key] -- GitLab