Rev 2517: Keep credentials used at connection creation for reconnection purposes. in file:///v/home/vila/src/experimental/reuse.transports/
Vincent Ladeuil
v.ladeuil+lp at free.fr
Sun Jun 3 13:58:51 BST 2007
At file:///v/home/vila/src/experimental/reuse.transports/
------------------------------------------------------------
revno: 2517
revision-id: v.ladeuil+lp at free.fr-20070603125849-hn65adfp30185xyu
parent: v.ladeuil+lp at free.fr-20070603125726-xrnl6fu53figdd0x
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: reuse.transports
timestamp: Sun 2007-06-03 14:58:49 +0200
message:
Keep credentials used at connection creation for reconnection purposes.
* bzrlib/transport/__init__.py:
(ConnectedTransport._init_connection): New method. Keep related
code together.
(ConnectedTransport._set_connection): Add a credentials parameter.
(ConnectedTransport._get_credentials): New method.
* bzrlib/tests/test_transport.py:
(TestConnectedTransport.test_connection_sharing_propagate_credentials):
New test.
modified:
bzrlib/tests/test_transport.py testtransport.py-20050718175618-e5cdb99f4555ddce
bzrlib/transport/__init__.py transport.py-20050711165921-4978aa7ce1285ad5
-------------- next part --------------
=== modified file 'bzrlib/tests/test_transport.py'
--- a/bzrlib/tests/test_transport.py 2007-06-02 16:54:28 +0000
+++ b/bzrlib/tests/test_transport.py 2007-06-03 12:58:49 +0000
@@ -645,23 +645,40 @@
def test_connection_sharing(self):
t = ConnectedTransport('foo://user@host.com/abs/path')
- self.assertEquals(None, t.get_connection())
+ self.assertIs(None, t._get_connection())
c = t.clone('subdir')
- self.assertEquals(None, c.get_connection())
+ self.assertIs(None, c._get_connection())
# But as soon as one transport connects, the other get
# the connection too
connection = object()
- t.set_connection(connection)
- self.assertIs(connection, t.get_connection())
- self.assertIs(connection, c.get_connection())
+ t._set_connection(connection)
+ self.assertIs(connection, t._get_connection())
+ self.assertIs(connection, c._get_connection())
# Temporary failure, we need to create a new connection
new_connection = object()
- t.set_connection(new_connection)
- self.assertIs(new_connection, t.get_connection())
- self.assertIs(new_connection, c.get_connection())
+ t._set_connection(new_connection)
+ self.assertIs(new_connection, t._get_connection())
+ self.assertIs(new_connection, c._get_connection())
+
+ def test_connection_sharing_propagate_credentials(self):
+ t = ConnectedTransport('foo://user@host.com/abs/path')
+ self.assertIs(None, t._get_connection())
+ self.assertIs(None, t._password)
+ c = t.clone('subdir')
+ self.assertEquals(None, c._get_connection())
+ self.assertIs(None, t._password)
+
+ # Simulate the user entering a password
+ password = 'secret'
+ connection = object()
+ t._set_connection(connection, password)
+ self.assertIs(connection, t._get_connection())
+ self.assertIs(password, t._get_credentials())
+ self.assertIs(connection, c._get_connection())
+ self.assertIs(password, c._get_credentials())
class TestReusedTransports(TestCase):
=== modified file 'bzrlib/transport/__init__.py'
--- a/bzrlib/transport/__init__.py 2007-06-02 16:54:28 +0000
+++ b/bzrlib/transport/__init__.py 2007-06-03 12:58:49 +0000
@@ -1065,14 +1065,10 @@
super(ConnectedTransport, self).__init__(base)
if from_transport is None:
- # We use a list as a container for the connection so that the
- # connection will be shared even if a transport is cloned before
- # the first effective connection (generally the first request
- # made). It also guarantees that the connection will still be
- # shared if a transport needs to reconnect after a temporary
- # failure.
- self._connection = [None]
+ self._init_connection()
else:
+ # set_connection MUST not be used here, see set_connection for an
+ # explanation
self._connection = from_transport._connection
def clone(self, offset=None):
@@ -1212,20 +1208,38 @@
remote_path = self._combine_paths(self._path, relative)
return remote_path
- def set_connection(self, connection):
+ def _init_connection(self):
+ self._connection = [(None, None)]
+
+ def _set_connection(self, connection, credentials=None):
"""Set the transport specific connection object.
Note: To ensure that connection is still shared after a temporary
failure and a new one needs to be created, daughter classes should
- always call this method to set the connection. No assumptions are made
- about the object type of the connection object.
+ always call this method to set the connection.
+
+ :param connection: An opaque object representing the connection used by
+ the daughter class.
+
+ :param credentials: An opaque object representing the credentials
+ needed to create the connection.
"""
- self._connection[0] = connection
-
- def get_connection(self):
+ # We use a list as a container for the connection so that the
+ # connection will be shared even if a transport is cloned before the
+ # first effective connection (generally the first request made). It
+ # also guarantees that the connection will still be shared if a
+ # transport needs to reconnect after a temporary failure.
+
+ self._connection[0] = (connection, credentials)
+
+ def _get_connection(self):
"""Returns the transport specific connection object."""
- return self._connection[0]
+ return self._connection[0][0]
+ def _get_credentials(self):
+ """Returns the credentials needed to establish a connection."""
+ (connection, credentials) = self._connection[0]
+ return self._connection[0][1]
# jam 20060426 For compatibility we copy the functions here
More information about the bazaar-commits
mailing list