DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (1aeaa33a64f9)

VCS Links

Line Code
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from __future__ import absolute_import, print_function, unicode_literals

import unittest

from taskgraph import optimize
from taskgraph.taskgraph import TaskGraph
from taskgraph import graph
from taskgraph.task import Task
from mozunit import main
from slugid import nice as slugid


class Remove(optimize.OptimizationStrategy):

    def should_remove_task(self, task, params, arg):
        return True


class Replace(optimize.OptimizationStrategy):

    def should_replace_task(self, task, params, taskid):
        return taskid


class TestOptimize(unittest.TestCase):

    strategies = {
        'never': optimize.OptimizationStrategy(),
        'remove': Remove(),
        'replace': Replace(),
    }

    def make_task(self, label, optimization=None, task_def=None, optimized=None,
                  task_id=None, dependencies=None):
        task_def = task_def or {'sample': 'task-def'}
        task = Task(kind='test', label=label, attributes={}, task=task_def)
        task.optimization = optimization
        task.task_id = task_id
        if dependencies is not None:
            task.task['dependencies'] = sorted(dependencies)
        return task

    def make_graph(self, *tasks_and_edges):
        tasks = {t.label: t for t in tasks_and_edges if isinstance(t, Task)}
        edges = {e for e in tasks_and_edges if not isinstance(e, Task)}
        return TaskGraph(tasks, graph.Graph(set(tasks), edges))

    def make_opt_graph(self, *tasks_and_edges):
        tasks = {t.task_id: t for t in tasks_and_edges if isinstance(t, Task)}
        edges = {e for e in tasks_and_edges if not isinstance(e, Task)}
        return TaskGraph(tasks, graph.Graph(set(tasks), edges))

    def make_triangle(self, **opts):
        """
        Make a "triangle" graph like this:

          t1 <-------- t3
           `---- t2 --'
        """
        return self.make_graph(
            self.make_task('t1', opts.get('t1')),
            self.make_task('t2', opts.get('t2')),
            self.make_task('t3', opts.get('t3')),
            ('t3', 't2', 'dep'),
            ('t3', 't1', 'dep2'),
            ('t2', 't1', 'dep'))

    def assert_remove_tasks(self, graph, exp_removed, do_not_optimize=set()):
        got_removed = optimize.remove_tasks(
            target_task_graph=graph,
            optimizations=optimize._get_optimizations(graph, self.strategies),
            params={},
            do_not_optimize=do_not_optimize)
        self.assertEqual(got_removed, exp_removed)

    def test_remove_tasks_never(self):
        "A graph full of optimization=never has nothing removed"
        graph = self.make_triangle()
        self.assert_remove_tasks(graph, set())

    def test_remove_tasks_all(self):
        "A graph full of optimization=remove has removes everything"
        graph = self.make_triangle(
            t1={'remove': None},
            t2={'remove': None},
            t3={'remove': None})
        self.assert_remove_tasks(graph, {'t1', 't2', 't3'})

    def test_remove_tasks_blocked(self):
        "Removable tasks that are depended on by non-removable tasks are not removed"
        graph = self.make_triangle(
            t1={'remove': None},
            t3={'remove': None})
        self.assert_remove_tasks(graph, {'t3'})

    def test_remove_tasks_do_not_optimize(self):
        "Removable tasks that are marked do_not_optimize are not removed"
        graph = self.make_triangle(
            t1={'remove': None},
            t2={'remove': None},  # but do_not_optimize
            t3={'remove': None})
        self.assert_remove_tasks(graph, {'t3'}, do_not_optimize={'t2'})

    def assert_replace_tasks(self, graph, exp_replaced, exp_removed=set(), exp_label_to_taskid={},
                             do_not_optimize=None, label_to_taskid=None, removed_tasks=None,
                             existing_tasks=None):
        do_not_optimize = do_not_optimize or set()
        label_to_taskid = label_to_taskid or {}
        removed_tasks = removed_tasks or set()
        existing_tasks = existing_tasks or {}

        got_replaced = optimize.replace_tasks(
            target_task_graph=graph,
            optimizations=optimize._get_optimizations(graph, self.strategies),
            params={},
            do_not_optimize=do_not_optimize,
            label_to_taskid=label_to_taskid,
            removed_tasks=removed_tasks,
            existing_tasks=existing_tasks)
        self.assertEqual(got_replaced, exp_replaced)
        self.assertEqual(removed_tasks, exp_removed)
        self.assertEqual(label_to_taskid, exp_label_to_taskid)

    def test_replace_tasks_never(self):
        "No tasks are replaced when strategy is 'never'"
        graph = self.make_triangle()
        self.assert_replace_tasks(graph, set())

    def test_replace_tasks_all(self):
        "All replacable tasks are replaced when strategy is 'replace'"
        graph = self.make_triangle(
            t1={'replace': 'e1'},
            t2={'replace': 'e2'},
            t3={'replace': 'e3'})
        self.assert_replace_tasks(
            graph,
            exp_replaced={'t1', 't2', 't3'},
            exp_label_to_taskid={'t1': 'e1', 't2': 'e2', 't3': 'e3'})

    def test_replace_tasks_blocked(self):
        "A task cannot be replaced if it depends on one that was not replaced"
        graph = self.make_triangle(
            t1={'replace': 'e1'},
            t3={'replace': 'e3'})
        self.assert_replace_tasks(
            graph,
            exp_replaced={'t1'},
            exp_label_to_taskid={'t1': 'e1'})

    def test_replace_tasks_do_not_optimize(self):
        "A task cannot be replaced if it depends on one that was not replaced"
        graph = self.make_triangle(
            t1={'replace': 'e1'},
            t2={'replace': 'xxx'},  # but do_not_optimize
            t3={'replace': 'e3'})
        self.assert_replace_tasks(
            graph,
            exp_replaced={'t1'},
            exp_label_to_taskid={'t1': 'e1'},
            do_not_optimize={'t2'})

    def test_replace_tasks_removed(self):
        "A task can be replaced with nothing"
        graph = self.make_triangle(
            t1={'replace': 'e1'},
            t2={'replace': True},
            t3={'replace': True})
        self.assert_replace_tasks(
            graph,
            exp_replaced={'t1'},
            exp_removed={'t2', 't3'},
            exp_label_to_taskid={'t1': 'e1'})

    def assert_subgraph(self, graph, removed_tasks, replaced_tasks,
                        label_to_taskid, exp_subgraph, exp_label_to_taskid):
        self.maxDiff = None
        optimize.slugid = ('tid{}'.format(i) for i in xrange(1, 10)).next
        try:
            got_subgraph = optimize.get_subgraph(graph, removed_tasks,
                                                 replaced_tasks, label_to_taskid)
        finally:
            optimize.slugid = slugid
        self.assertEqual(got_subgraph.graph, exp_subgraph.graph)
        self.assertEqual(got_subgraph.tasks, exp_subgraph.tasks)
        self.assertEqual(label_to_taskid, exp_label_to_taskid)

    def test_get_subgraph_no_change(self):
        "get_subgraph returns a similarly-shaped subgraph when nothing is removed"
        graph = self.make_triangle()
        self.assert_subgraph(
            graph, set(), set(), {},
            self.make_opt_graph(
                self.make_task('t1', task_id='tid1', dependencies={}),
                self.make_task('t2', task_id='tid2', dependencies={'tid1'}),
                self.make_task('t3', task_id='tid3', dependencies={'tid1', 'tid2'}),
                ('tid3', 'tid2', 'dep'),
                ('tid3', 'tid1', 'dep2'),
                ('tid2', 'tid1', 'dep')),
            {'t1': 'tid1', 't2': 'tid2', 't3': 'tid3'})

    def test_get_subgraph_removed(self):
        "get_subgraph returns a smaller subgraph when tasks are removed"
        graph = self.make_triangle()
        self.assert_subgraph(
            graph, {'t2', 't3'}, set(), {},
            self.make_opt_graph(
                self.make_task('t1', task_id='tid1', dependencies={})),
            {'t1': 'tid1'})

    def test_get_subgraph_replaced(self):
        "get_subgraph returns a smaller subgraph when tasks are replaced"
        graph = self.make_triangle()
        self.assert_subgraph(
            graph, set(), {'t1', 't2'}, {'t1': 'e1', 't2': 'e2'},
            self.make_opt_graph(
                self.make_task('t3', task_id='tid1', dependencies={'e1', 'e2'})),
            {'t1': 'e1', 't2': 'e2', 't3': 'tid1'})

    def test_get_subgraph_removed_dep(self):
        "get_subgraph raises an Exception when a task depends on a removed task"
        graph = self.make_triangle()
        with self.assertRaises(Exception):
            optimize.get_subgraph(graph, {'t2'}, set(), {})


if __name__ == '__main__':
    main()