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