1# Copyright 2013 The ChromiumOS Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4"""Task unittest. 5 6Part of the Chrome build flags optimization. 7""" 8 9__author__ = "[email protected] (Yuheng Long)" 10 11import random 12import sys 13import unittest 14 15import task 16from task import Task 17 18 19# The number of flags be tested. 20NUM_FLAGS = 20 21 22# The random build result values used to test get set result method. 23RANDOM_BUILD_RESULT = 100 24 25# The random test result values used to test get set result method. 26RANDOM_TESTRESULT = 100 27 28 29class MockFlagSet(object): 30 """This class emulates a set of flags. 31 32 It returns the flags and hash value, when the FormattedForUse method and the 33 __hash__ method is called, respectively. These values are initialized when the 34 MockFlagSet instance is constructed. 35 """ 36 37 def __init__(self, flags=0, hash_value=-1): 38 self._flags = flags 39 self._hash_value = hash_value 40 41 def __eq__(self, other): 42 assert isinstance(other, MockFlagSet) 43 return self._flags == other.FormattedForUse() 44 45 def FormattedForUse(self): 46 return self._flags 47 48 def __hash__(self): 49 return self._hash_value 50 51 def GetHash(self): 52 return self._hash_value 53 54 55class TaskTest(unittest.TestCase): 56 """This class test the Task class.""" 57 58 def testEqual(self): 59 """Test the equal method of the task. 60 61 Two tasks are equal if and only if their encapsulated flag_sets are equal. 62 """ 63 64 flags = range(NUM_FLAGS) 65 66 # Two tasks having the same flag set should be equivalent. 67 flag_sets = [MockFlagSet(flag) for flag in flags] 68 for flag_set in flag_sets: 69 assert Task(flag_set) == Task(flag_set) 70 71 # Two tasks having different flag set should be different. 72 for flag_set in flag_sets: 73 test_task = Task(flag_set) 74 other_flag_sets = [ 75 flags for flags in flag_sets if flags != flag_set 76 ] 77 for flag_set1 in other_flag_sets: 78 assert test_task != Task(flag_set1) 79 80 def testHash(self): 81 """Test the hash method of the task. 82 83 Two tasks are equal if and only if their encapsulated flag_sets are equal. 84 """ 85 86 # Random identifier that is not relevant in this test. 87 identifier = random.randint(-sys.maxint - 1, -1) 88 89 flag_sets = [ 90 MockFlagSet(identifier, value) for value in range(NUM_FLAGS) 91 ] 92 for flag_set in flag_sets: 93 # The hash of a task is the same as the hash of its flag set. 94 hash_task = Task(flag_set) 95 hash_value = hash(hash_task) 96 assert hash_value == flag_set.GetHash() 97 98 # The hash of a task does not change. 99 assert hash_value == hash(hash_task) 100 101 def testGetIdentifier(self): 102 """Test the get identifier method of the task. 103 104 The get identifier method should returns the flag set in the build stage. 105 """ 106 107 flag_sets = [MockFlagSet(flag) for flag in range(NUM_FLAGS)] 108 for flag_set in flag_sets: 109 identifier_task = Task(flag_set) 110 111 identifier = identifier_task.GetIdentifier(task.BUILD_STAGE) 112 113 # The task formats the flag set into a string. 114 assert identifier == str(flag_set.FormattedForUse()) 115 116 def testGetSetResult(self): 117 """Test the get and set result methods of the task. 118 119 The get result method should return the same results as were set. 120 """ 121 122 flag_sets = [MockFlagSet(flag) for flag in range(NUM_FLAGS)] 123 for flag_set in flag_sets: 124 result_task = Task(flag_set) 125 126 # The get result method should return the same results as were set, in 127 # build stage. Currently, the build result is a 5-element tuple containing 128 # the checksum of the result image, the performance cost of the build, the 129 # compilation image, the length of the build, and the length of the text 130 # section of the build. 131 result = tuple( 132 [random.randint(0, RANDOM_BUILD_RESULT) for _ in range(5)] 133 ) 134 result_task.SetResult(task.BUILD_STAGE, result) 135 assert result == result_task.GetResult(task.BUILD_STAGE) 136 137 # The checksum is the identifier of the test stage. 138 identifier = result_task.GetIdentifier(task.TEST_STAGE) 139 # The first element of the result tuple is the checksum. 140 assert identifier == result[0] 141 142 # The get result method should return the same results as were set, in 143 # test stage. 144 random_test_result = random.randint(0, RANDOM_TESTRESULT) 145 result_task.SetResult(task.TEST_STAGE, random_test_result) 146 test_result = result_task.GetResult(task.TEST_STAGE) 147 assert test_result == random_test_result 148 149 def testDone(self): 150 """Test the done methods of the task. 151 152 The done method should return false is the task has not perform and return 153 true after the task is finished. 154 """ 155 156 flags = range(NUM_FLAGS) 157 158 flag_sets = [MockFlagSet(flag) for flag in flags] 159 for flag_set in flag_sets: 160 work_task = Task(flag_set) 161 162 # The task has not been compiled nor tested. 163 assert not work_task.Done(task.TEST_STAGE) 164 assert not work_task.Done(task.BUILD_STAGE) 165 166 # After the task has been compiled, it should indicate finished in BUILD 167 # stage. 168 result = tuple( 169 [random.randint(0, RANDOM_BUILD_RESULT) for _ in range(5)] 170 ) 171 work_task.SetResult(task.BUILD_STAGE, result) 172 assert not work_task.Done(task.TEST_STAGE) 173 assert work_task.Done(task.BUILD_STAGE) 174 175 # After the task has been tested, it should indicate finished in TEST 176 # stage. 177 work_task.SetResult( 178 task.TEST_STAGE, random.randint(0, RANDOM_TESTRESULT) 179 ) 180 assert work_task.Done(task.TEST_STAGE) 181 assert work_task.Done(task.BUILD_STAGE) 182 183 184if __name__ == "__main__": 185 unittest.main() 186