106
106
class FakeProtocol(object):
107
107
"""Lookalike SmartClientRequestProtocolOne allowing body reading tests."""
109
def __init__(self, body):
109
def __init__(self, body, fake_client):
110
110
self._body_buffer = StringIO(body)
111
self._fake_client = fake_client
112
113
def read_body_bytes(self, count=-1):
113
return self._body_buffer.read(count)
114
bytes = self._body_buffer.read(count)
115
if self._body_buffer.tell() == len(self._body_buffer.getvalue()):
116
self._fake_client.expecting_body = False
119
def cancel_read_body(self):
120
self._fake_client.expecting_body = False
116
123
class FakeClient(_SmartClient):
119
126
def __init__(self, responses):
120
127
# We don't call the super init because there is no medium.
121
"""create a FakeClient.
128
"""Create a FakeClient.
123
130
:param respones: A list of response-tuple, body-data pairs to be sent
126
133
self.responses = responses
135
self.expecting_body = False
129
137
def call(self, method, *args):
130
138
self._calls.append(('call', method, args))
133
141
def call_expecting_body(self, method, *args):
134
142
self._calls.append(('call_expecting_body', method, args))
135
143
result = self.responses.pop(0)
136
return result[0], FakeProtocol(result[1])
144
self.expecting_body = True
145
return result[0], FakeProtocol(result[1], self)
139
148
class TestBzrDirOpenBranch(tests.TestCase):
752
761
remote_repo.sprout(dest_bzrdir)
763
def test_backwards_compatibility(self):
764
"""If the server doesn't recognise this request, fallback to VFS.
766
This happens when a current client talks to an older server that
767
doesn't implement 'Repository.tarball'.
769
# Make a regular local repository to receive the results
770
dest_transport = MemoryTransport()
771
dest_transport.mkdir('destrepo')
772
bzrdir_format = bzrdir.format_registry.make_bzrdir('default')
773
dest_bzrdir = bzrdir_format.initialize_on_transport(dest_transport)
776
"Generic bzr smart protocol error: "
777
"bad request 'Repository.tarball'")
778
responses = [(('error', error_msg), '')]
779
remote_repo, client = self.setup_fake_client_and_repository(
781
mock_real_repo = MockRealRepository()
782
remote_repo._real_repository = mock_real_repo
785
remote_repo.sprout(dest_bzrdir)
787
self.assertEqual([('sprout', dest_bzrdir, None)], mock_real_repo.calls,
788
"RemoteRepository didn't fallback to the real repository correctly")
789
self.failIf(client.expecting_body,
790
"The protocol has been left in an unclean state that will cause "
791
"TooManyConcurrentRequests errors.")
794
class MockRealRepository(object):
795
"""Mock of a RemoteRepository's '_real_repository' attribute.
797
Used by TestRepositoryTarball.test_backwards_compatibility.
803
def sprout(self, to_bzrdir, revision_id=None):
804
self.calls.append(('sprout', to_bzrdir, revision_id))
755
807
class TestRemoteRepositoryCopyContent(tests.TestCaseWithTransport):
756
808
"""RemoteRepository.copy_content_into optimizations"""