diff options
author | Yuheng Long <yuhenglong@google.com> | 2013-07-15 10:12:44 -0700 |
---|---|---|
committer | ChromeBot <chrome-bot@google.com> | 2013-07-16 10:58:56 -0700 |
commit | a791546e80cede30d5325bec834b35b99b7e7bfe (patch) | |
tree | a3f565fa0081d627df1705ba68dd04cddc256fa6 /bestflags/mock_task.py | |
parent | 26ec76c8a9d4f5dd023513d2772fa6cd4b6749ea (diff) | |
download | toolchain-utils-a791546e80cede30d5325bec834b35b99b7e7bfe.tar.gz |
Extract out a common mock task into a module.
The mock tasks are used in multiple unitest modules. It is not extracted out
into a module to enable reuse.
BUG=None
TEST=unit testing for the pipeline stage and pipeline workers.
Change-Id: Iee436266f7ea26c2877377f52208cc5cd31e7815
Reviewed-on: https://gerrit-int.chromium.org/41001
Reviewed-by: Simon Que <sque@google.com>
Reviewed-by: Luis Lozano <llozano@chromium.org>
Commit-Queue: Yuheng Long <yuhenglong@google.com>
Tested-by: Yuheng Long <yuhenglong@google.com>
Diffstat (limited to 'bestflags/mock_task.py')
-rw-r--r-- | bestflags/mock_task.py | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/bestflags/mock_task.py b/bestflags/mock_task.py new file mode 100644 index 00000000..2582f111 --- /dev/null +++ b/bestflags/mock_task.py @@ -0,0 +1,64 @@ +"""This module defines the common mock task used by varies unit tests. + +Part of the Chrome build flags optimization. +""" + +__author__ = 'yuhenglong@google.com (Yuheng Long)' + +# Pick an integer at random. +POISONPILL = 975 + + +class MockTask(object): + """This class emulates an actual task. + + It does not do the actual work, but simply returns the result as given when + this task is constructed. + """ + + def __init__(self, stage, identifier, cost=0): + """Set up the results for this task. + + Args: + stage: the stage of this test is in. + identifier: the identifier of this task. + cost: the mock cost of this task. + + The _pre_cost field stored the cost. Once this task is performed, i.e., by + calling the work method, the _cost field will have this cost. The stage + field verifies that the module being tested and the unitest are in the + same stage. If the unitest does not care about cost of this task, the cost + parameter should be leaved blank. + """ + + self._identifier = identifier + self._pre_cost = cost + self._stage = stage + + def __eq__(self, other): + if isinstance(other, MockTask): + return (self._identifier == other.GetIdentifier(self._stage) and + self._cost == other.GetResult(self._stage)) + return False + + def GetIdentifier(self, stage): + assert stage == self._stage + return self._identifier + + def SetResult(self, stage, cost): + assert stage == self._stage + self._cost = cost + + def Work(self, stage): + assert stage == self._stage + self._cost = self._pre_cost + + def GetResult(self, stage): + assert stage == self._stage + return self._cost + + def Done(self, stage): + """Indicates whether the task has been performed.""" + + assert stage == self._stage + return '_cost' in self.__dict__ |