/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/TestUtil.py

  • Committer: John Arbash Meinel
  • Date: 2011-04-20 14:27:19 UTC
  • mto: This revision was merged to the branch mainline in revision 5837.
  • Revision ID: john@arbash-meinel.com-20110420142719-advs1k5vztqzbrgv
Fix bug #767177. Be more agressive with file.close() calls.

Our test suite gets a number of thread leaks and failures because it happens to get async
SFTPFile.close() calls. (if an SFTPFile closes due to __del__ it is done as an async request,
while if you call SFTPFile.close() it is done as a synchronous request.)
We have a couple other cases, probably. Namely SFTPTransport.get() also does an async
prefetch of the content, so if you don't .read() you'll also leak threads that think they
are doing work that you want.

The biggest change here, though, is using a try/finally in a generator, which is not 
python2.4 compatible.

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
20
20
import logging
21
21
import unittest
22
22
 
 
23
from bzrlib import pyutils
 
24
 
23
25
# Mark this python module as being part of the implementation
24
26
# of unittest: this gives us better tracebacks where the last
25
27
# shown frame is the test code, not our assertXYZ.
27
29
 
28
30
 
29
31
class LogCollector(logging.Handler):
 
32
 
30
33
    def __init__(self):
31
34
        logging.Handler.__init__(self)
32
35
        self.records=[]
 
36
 
33
37
    def emit(self, record):
34
38
        self.records.append(record.getMessage())
35
39
 
58
62
                visitor.visitSuite(test)
59
63
                visitTests(test, visitor)
60
64
            else:
61
 
                print "unvisitable non-unittest.TestCase element %r (%r)" % (test, test.__class__)
 
65
                print "unvisitable non-unittest.TestCase element %r (%r)" % (
 
66
                    test, test.__class__)
62
67
 
63
68
 
64
69
class TestSuite(unittest.TestSuite):
106
111
 
107
112
    def loadTestsFromModuleName(self, name):
108
113
        result = self.suiteClass()
109
 
        module = _load_module_by_name(name)
 
114
        module = pyutils.get_named_object(name)
110
115
 
111
116
        result.addTests(self.loadTestsFromModule(module))
112
117
        return result
135
140
        >>>         result.addTests([test, test])
136
141
        >>>     return result
137
142
        """
138
 
        basic_tests = super(TestLoader, self).loadTestsFromModule(module)
 
143
        if sys.version_info < (2, 7):
 
144
            basic_tests = super(TestLoader, self).loadTestsFromModule(module)
 
145
        else:
 
146
            # GZ 2010-07-19: Python 2.7 unittest also uses load_tests but with
 
147
            #                a different and incompatible signature
 
148
            basic_tests = super(TestLoader, self).loadTestsFromModule(module,
 
149
                use_load_tests=False)
139
150
        load_tests = getattr(module, "load_tests", None)
140
151
        if load_tests is not None:
141
152
            return load_tests(basic_tests, module, self)
173
184
            return self.suiteClass()
174
185
 
175
186
 
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
187
class TestVisitor(object):
188
188
    """A visitor for Tests"""
 
189
 
189
190
    def visitSuite(self, aTestSuite):
190
191
        pass
 
192
 
191
193
    def visitCase(self, aTestCase):
192
194
        pass