~bzr-pqm/bzr/bzr.dev

2485.8.5 by Vincent Ladeuil
Factor out InstrumentedTransport.
1
# Copyright (C) 2007 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
18
from bzrlib.hooks import Hooks
19
from bzrlib.tests.test_ftp_transport import TestCaseWithFTPServer
20
from bzrlib.transport import (
21
    register_transport,
22
    unregister_transport,
23
    )
24
from bzrlib.transport.ftp import FtpTransport
25
26
27
class TransportHooks(Hooks):
28
    """Dict-mapping hook name to a list of callables for transport hooks"""
29
30
    def __init__(self):
31
        Hooks.__init__(self)
32
        # invoked when the transport is about to create or reuse
33
        # an ftp connection. The api signature is (transport, ftp_instance)
34
        self['get_FTP'] = []
35
36
37
class InstrumentedTransport(FtpTransport):
38
    """Instrumented transport class to test commands behavior"""
39
40
    hooks = TransportHooks()
41
42
43
class ConnectionHookedTransport(InstrumentedTransport):
44
    """Transport instrumented to inspect connections"""
45
46
    def _get_FTP(self):
47
        """See FtpTransport._get_FTP.
48
49
        This is where we can detect if the connection is reused
50
        or if a new one is created. This a bit ugly, but it's the
51
        easiest until transport classes are refactored.
52
        """
53
        instance = super(ConnectionHookedTransport, self)._get_FTP()
54
        for hook in self.hooks['get_FTP']:
55
            hook(self, instance)
56
        return instance
57
58
59
class TestCaseWithConnectionHookedTransport(TestCaseWithFTPServer):
60
61
    def setUp(self):
62
        super(TestCaseWithConnectionHookedTransport, self).setUp()
63
        ConnectionHookedTransport.hooks.install_hook('get_FTP',
64
                                                     self.get_connection_hook)
65
        # Make our instrumented transport the default ftp transport
66
        register_transport('ftp://', ConnectionHookedTransport)
67
68
        def cleanup():
69
            InstrumentedTransport.hooks = TransportHooks()
70
            unregister_transport('ftp://', ConnectionHookedTransport)
71
72
        self.addCleanup(cleanup)
73
        self.connections = []
74
75
    def get_connection_hook(self, transport, connection):
76
        if connection is not None and connection not in self.connections:
77
            self.connections.append(connection)
78