~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

(jelmer) Add a post_connect hook for transports. (Bazaar Developers)

Show diffs side-by-side

added added

removed removed

Lines of Context:
2734
2734
 
2735
2735
    def setUp(self):
2736
2736
        super(TestCaseWithMemoryTransport, self).setUp()
2737
 
        # Ensure that ConnectedTransport doesn't leak sockets
2738
 
        def get_transport_from_url_with_cleanup(*args, **kwargs):
2739
 
            t = orig_get_transport_from_url(*args, **kwargs)
2740
 
            if isinstance(t, _mod_transport.ConnectedTransport):
2741
 
                self.addCleanup(t.disconnect)
2742
 
            return t
2743
 
 
2744
 
        orig_get_transport_from_url = self.overrideAttr(
2745
 
            _mod_transport, 'get_transport_from_url',
2746
 
            get_transport_from_url_with_cleanup)
 
2737
 
 
2738
        def _add_disconnect_cleanup(transport):
 
2739
            """Schedule disconnection of given transport at test cleanup
 
2740
 
 
2741
            This needs to happen for all connected transports or leaks occur.
 
2742
 
 
2743
            Note reconnections may mean we call disconnect multiple times per
 
2744
            transport which is suboptimal but seems harmless.
 
2745
            """
 
2746
            self.addCleanup(transport.disconnect)
 
2747
 
 
2748
        _mod_transport.Transport.hooks.install_named_hook('post_connect',
 
2749
            _add_disconnect_cleanup, None)
 
2750
 
2747
2751
        self._make_test_root()
2748
2752
        self.addCleanup(os.chdir, os.getcwdu())
2749
2753
        self.makeAndChdirToTestDir()