~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/TestUtil.py

  • Committer: Vincent Ladeuil
  • Date: 2012-01-18 14:09:19 UTC
  • mto: This revision was merged to the branch mainline in revision 6468.
  • Revision ID: v.ladeuil+lp@free.fr-20120118140919-rlvdrhpc0nq1lbwi
Change set/remove to require a lock for the branch config files.

This means that tests (or any plugin for that matter) do not requires an
explicit lock on the branch anymore to change a single option. This also
means the optimisation becomes "opt-in" and as such won't be as
spectacular as it may be and/or harder to get right (nothing fails
anymore).

This reduces the diff by ~300 lines.

Code/tests that were updating more than one config option is still taking
a lock to at least avoid some IOs and demonstrate the benefits through
the decreased number of hpss calls.

The duplication between BranchStack and BranchOnlyStack will be removed
once the same sharing is in place for local config files, at which point
the Stack class itself may be able to host the changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2011 Canonical Ltd
2
2
#       Author: Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
19
19
import sys
20
20
import logging
21
21
import unittest
 
22
import weakref
 
23
 
 
24
from bzrlib import pyutils
22
25
 
23
26
# Mark this python module as being part of the implementation
24
27
# of unittest: this gives us better tracebacks where the last
27
30
 
28
31
 
29
32
class LogCollector(logging.Handler):
 
33
 
30
34
    def __init__(self):
31
35
        logging.Handler.__init__(self)
32
36
        self.records=[]
 
37
 
33
38
    def emit(self, record):
34
39
        self.records.append(record.getMessage())
35
40
 
58
63
                visitor.visitSuite(test)
59
64
                visitTests(test, visitor)
60
65
            else:
61
 
                print "unvisitable non-unittest.TestCase element %r (%r)" % (test, test.__class__)
 
66
                print "unvisitable non-unittest.TestCase element %r (%r)" % (
 
67
                    test, test.__class__)
 
68
 
 
69
 
 
70
class FailedCollectionCase(unittest.TestCase):
 
71
    """Pseudo-test to run and report failure if given case was uncollected"""
 
72
 
 
73
    def __init__(self, case):
 
74
        super(FailedCollectionCase, self).__init__("fail_uncollected")
 
75
        # GZ 2011-09-16: Maybe catch errors from id() method as cases may be
 
76
        #                in a bit of a funny state by now.
 
77
        self._problem_case_id = case.id()
 
78
 
 
79
    def id(self):
 
80
        if self._problem_case_id[-1:] == ")":
 
81
            return self._problem_case_id[:-1] + ",uncollected)"
 
82
        return self._problem_case_id + "(uncollected)"
 
83
 
 
84
    def fail_uncollected(self):
 
85
        self.fail("Uncollected test case: " + self._problem_case_id)
62
86
 
63
87
 
64
88
class TestSuite(unittest.TestSuite):
77
101
        tests = list(self)
78
102
        tests.reverse()
79
103
        self._tests = []
 
104
        stored_count = 0
 
105
        count_stored_tests = getattr(result, "_count_stored_tests", int)
 
106
        from bzrlib.tests import selftest_debug_flags
 
107
        notify = "uncollected_cases" in selftest_debug_flags
80
108
        while tests:
81
109
            if result.shouldStop:
82
110
                self._tests = reversed(tests)
83
111
                break
84
 
            tests.pop().run(result)
 
112
            case = _run_and_collect_case(tests.pop(), result)()
 
113
            new_stored_count = count_stored_tests()
 
114
            if case is not None and isinstance(case, unittest.TestCase):
 
115
                if stored_count == new_stored_count and notify:
 
116
                    # Testcase didn't fail, but somehow is still alive
 
117
                    FailedCollectionCase(case).run(result)
 
118
                    # Adding a new failure so need to reupdate the count
 
119
                    new_stored_count = count_stored_tests()
 
120
                # GZ 2011-09-16: Previously zombied the case at this point by
 
121
                #                clearing the dict as fallback, skip for now.
 
122
            stored_count = new_stored_count
85
123
        return result
86
124
 
87
125
 
 
126
def _run_and_collect_case(case, res):
 
127
    """Run test case against result and use weakref to drop the refcount"""
 
128
    case.run(res)
 
129
    return weakref.ref(case)
 
130
 
 
131
 
88
132
class TestLoader(unittest.TestLoader):
89
133
    """Custom TestLoader to extend the stock python one."""
90
134
 
106
150
 
107
151
    def loadTestsFromModuleName(self, name):
108
152
        result = self.suiteClass()
109
 
        module = _load_module_by_name(name)
 
153
        module = pyutils.get_named_object(name)
110
154
 
111
155
        result.addTests(self.loadTestsFromModule(module))
112
156
        return result
135
179
        >>>         result.addTests([test, test])
136
180
        >>>     return result
137
181
        """
138
 
        basic_tests = super(TestLoader, self).loadTestsFromModule(module)
 
182
        if sys.version_info < (2, 7):
 
183
            basic_tests = super(TestLoader, self).loadTestsFromModule(module)
 
184
        else:
 
185
            # GZ 2010-07-19: Python 2.7 unittest also uses load_tests but with
 
186
            #                a different and incompatible signature
 
187
            basic_tests = super(TestLoader, self).loadTestsFromModule(module,
 
188
                use_load_tests=False)
139
189
        load_tests = getattr(module, "load_tests", None)
140
190
        if load_tests is not None:
141
191
            return load_tests(basic_tests, module, self)
173
223
            return self.suiteClass()
174
224
 
175
225
 
176
 
def _load_module_by_name(mod_name):
177
 
    parts = mod_name.split('.')
178
 
    module = __import__(mod_name)
179
 
    del parts[0]
180
 
    # for historical reasons python returns the top-level module even though
181
 
    # it loads the submodule; we need to walk down to get the one we want.
182
 
    while parts:
183
 
        module = getattr(module, parts.pop(0))
184
 
    return module
185
 
 
186
 
 
187
226
class TestVisitor(object):
188
227
    """A visitor for Tests"""
 
228
 
189
229
    def visitSuite(self, aTestSuite):
190
230
        pass
 
231
 
191
232
    def visitCase(self, aTestCase):
192
233
        pass