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