aboutsummaryrefslogtreecommitdiff
path: root/bestflags/task_test.py
diff options
context:
space:
mode:
authorYuheng Long <yuhenglong@google.com>2013-07-25 10:02:36 -0700
committerChromeBot <chrome-bot@google.com>2013-07-30 17:08:54 -0700
commitb15d41c6d6324e343fbea19abaed3717417f3cde (patch)
treeafbdbad72db0734112a0860bc778de6192562924 /bestflags/task_test.py
parenta5712a2c71aa665dcca808963d152228890c8364 (diff)
downloadtoolchain-utils-b15d41c6d6324e343fbea19abaed3717417f3cde.tar.gz
Add the task module.
BUG=None TEST=unit testings for the pipeline stage, pipeline workers, generation, steering and task. Change-Id: I38987a12d7a48ec027d42465300a7226c760ea9d Reviewed-on: https://gerrit-int.chromium.org/41659 Reviewed-by: Simon Que <sque@google.com> Commit-Queue: Yuheng Long <yuhenglong@google.com> Tested-by: Yuheng Long <yuhenglong@google.com>
Diffstat (limited to 'bestflags/task_test.py')
-rw-r--r--bestflags/task_test.py173
1 files changed, 159 insertions, 14 deletions
diff --git a/bestflags/task_test.py b/bestflags/task_test.py
index ec3d5b6e..3777a097 100644
--- a/bestflags/task_test.py
+++ b/bestflags/task_test.py
@@ -9,31 +9,176 @@ Part of the Chrome build flags optimization.
__author__ = 'yuhenglong@google.com (Yuheng Long)'
+import random
+import sys
import unittest
import task
+from task import Task
+# The number of flags be tested.
+NUM_FLAGS = 20
+
+# The random build result values used to test get set result method.
+RANDOM_BUILD_RESULT = 100
+
+# The random test result values used to test get set result method.
+RANDOM_TESTRESULT = 100
+
+
+# Create task for unittestings that only uses the flag set field of the task.
+def _IdentifierTask(identifier):
+ return Task(identifier, None, None, None, None, None)
-class TaskTest(unittest.TestCase):
- """This class test the Task class.
- The getter and setter should function properly.
+class MockFlagSet(object):
+ """This class emulates a set of flags.
+
+ It returns the flags and hash value, when the FormattedForUse method and the
+ __hash__ method is called, respectively. These values are initialized when the
+ MockFlagSet instance is constructed.
"""
- def setUp(self):
- pass
+ def __init__(self, flags=0, hash_value=-1):
+ self._flags = flags
+ self._hash_value = hash_value
+
+ def __eq__(self, other):
+ assert isinstance(other, MockFlagSet)
+ return self._flags == other.FormattedForUse()
+
+ def FormattedForUse(self):
+ return self._flags
+
+ def __hash__(self):
+ return self._hash_value
+
+ def GetHash(self):
+ return self._hash_value
+
+
+class TaskTest(unittest.TestCase):
+ """This class test the Task class."""
+
+ def testEqual(self):
+ """Test the equal method of the task.
+
+ Two tasks are equal if and only if their encapsulated flag_sets are equal.
+ """
+
+ flags = range(NUM_FLAGS)
+
+ # Two tasks having the same flag set should be equivalent.
+ flag_sets = [MockFlagSet(flag) for flag in flags]
+ for flag_set in flag_sets:
+ task0 = _IdentifierTask(flag_set)
+ task1 = _IdentifierTask(flag_set)
+
+ assert task0 == task1
+
+ # Two tasks having different flag set should be different.
+ for flag_set in flag_sets:
+ task0 = _IdentifierTask(flag_set)
+ other_flag_sets = [flags for flags in flag_sets if flags != flag_set]
+ for flag_set1 in other_flag_sets:
+ task1 = _IdentifierTask(flag_set1)
+ assert task0 != task1
+
+ def testHash(self):
+ """Test the hash method of the task.
+
+ Two tasks are equal if and only if their encapsulated flag_sets are equal.
+ """
+
+ # Random identifier that is not relevant in this test.
+ identifier = random.randint(-sys.maxint - 1, -1)
+
+ flag_sets = [MockFlagSet(identifier, value) for value in range(NUM_FLAGS)]
+ for flag_set in flag_sets:
+ # The hash of a task is the same as the hash of its flag set.
+ hash_task = _IdentifierTask(flag_set)
+ h0 = hash(hash_task)
+ assert h0 == flag_set.GetHash()
+
+ # The hash of a task does not change.
+ h1 = hash(hash_task)
+ assert h0 == h1
+
+ def testGetIdentifier(self):
+ """Test the get identifier method of the task.
+
+ The get identifier method should returns the flag set in the build stage.
+ """
+
+ flag_sets = [MockFlagSet(flag) for flag in range(NUM_FLAGS)]
+ for flag_set in flag_sets:
+ identifier_task = _IdentifierTask(flag_set)
+
+ identifier = identifier_task.GetIdentifier(task.BUILD_STAGE)
+
+ # The task formats the flag set into a string.
+ assert identifier == str(flag_set.FormattedForUse())
+
+ def testGetSetResult(self):
+ """Test the get and set result methods of the task.
+
+ The get result method should return the same results as were set.
+ """
+
+ flag_sets = [MockFlagSet(flag) for flag in range(NUM_FLAGS)]
+ for flag_set in flag_sets:
+ result_task = _IdentifierTask(flag_set)
+
+ # The get result method should return the same results as were set, in
+ # build stage. Currently, the build result is a 5-element tuple containing
+ # the checksum of the result image, the performance cost of the build, the
+ # compilation image, the length of the build, and the length of the text
+ # section of the build.
+ result = tuple([random.randint(0, RANDOM_BUILD_RESULT) for _ in range(5)])
+ result_task.SetResult(task.BUILD_STAGE, result)
+ assert result == result_task.GetResult(task.BUILD_STAGE)
+
+ # The checksum is the identifier of the test stage.
+ identifier = result_task.GetIdentifier(task.TEST_STAGE)
+ # The first element of the result tuple is the checksum.
+ assert identifier == result[0]
+
+ # The get result method should return the same results as were set, in
+ # test stage.
+ random_test_result = random.randint(0, RANDOM_TESTRESULT)
+ result_task.SetResult(task.TEST_STAGE, random_test_result)
+ test_result = result_task.GetResult(task.TEST_STAGE)
+ assert test_result == random_test_result
+
+ def testDone(self):
+ """Test the done methods of the task.
+
+ The done method should return false is the task has not perform and return
+ true after the task is finished.
+ """
+
+ flags = range(NUM_FLAGS)
+
+ flag_sets = [MockFlagSet(flag) for flag in flags]
+ for flag_set in flag_sets:
+ work_task = _IdentifierTask(flag_set)
- def testFlag(self):
- """"Test proper access of the flags."""
- pass
+ # The task has not been compiled nor tested.
+ assert not work_task.Done(task.TEST_STAGE)
+ assert not work_task.Done(task.BUILD_STAGE)
- def testChecksum(self):
- """"Test proper access of the check sum."""
- pass
+ # After the task has been compiled, it should indicate finished in BUILD
+ # stage.
+ result = tuple([random.randint(0, RANDOM_BUILD_RESULT) for _ in range(5)])
+ work_task.SetResult(task.BUILD_STAGE, result)
+ assert not work_task.Done(task.TEST_STAGE)
+ assert work_task.Done(task.BUILD_STAGE)
- def testImage(self):
- """"Test proper access of the image."""
- pass
+ # After the task has been tested, it should indicate finished in TEST
+ # stage.
+ work_task.SetResult(task.TEST_STAGE, random.randint(0, RANDOM_TESTRESULT))
+ assert work_task.Done(task.TEST_STAGE)
+ assert work_task.Done(task.BUILD_STAGE)
if __name__ == '__main__':
unittest.main()