aboutsummaryrefslogtreecommitdiff
path: root/bestflags/mock_task.py
blob: e25daeba3a7a6c0d6f64dc3b909df46cd3ffc5e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Copyright 2013 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""This module defines the common mock tasks used by various 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 _cost field stored the cost. Once this task is performed, i.e., by
          calling the work method or by setting the result from other task, 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._cost = cost
        self._stage = stage

        # Indicate that this method has not been performed yet.
        self._performed = False

    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
        self._performed = True

    def Work(self, stage):
        assert stage == self._stage
        self._performed = True

    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 self._performed

    def LogSteeringCost(self):
        pass


class IdentifierMockTask(MockTask):
    """This class defines the mock task that does not consider the cost.

    The task instances will be inserted into a set. Therefore the hash and the
    equal methods are overridden. The unittests that compares identities of the
    tasks for equality can use this mock task instead of the base mock tack.
    """

    def __hash__(self):
        return self._identifier

    def __eq__(self, other):
        if isinstance(other, MockTask):
            return self._identifier == other.GetIdentifier(self._stage)
        return False