~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_remote.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-10-03 23:24:50 UTC
  • mfrom: (2878.1.1 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20071003232450-c831pepea3skddct
merge 0.91 back to trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
106
106
class FakeProtocol(object):
107
107
    """Lookalike SmartClientRequestProtocolOne allowing body reading tests."""
108
108
 
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
111
112
 
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
 
117
        return bytes
 
118
 
 
119
    def cancel_read_body(self):
 
120
        self._fake_client.expecting_body = False
114
121
 
115
122
 
116
123
class FakeClient(_SmartClient):
118
125
    
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.
122
129
 
123
130
        :param respones: A list of response-tuple, body-data pairs to be sent
124
131
            back to callers.
125
132
        """
126
133
        self.responses = responses
127
134
        self._calls = []
 
135
        self.expecting_body = False
128
136
 
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)
137
146
 
138
147
 
139
148
class TestBzrDirOpenBranch(tests.TestCase):
751
760
        # try to copy...
752
761
        remote_repo.sprout(dest_bzrdir)
753
762
 
 
763
    def test_backwards_compatibility(self):
 
764
        """If the server doesn't recognise this request, fallback to VFS.
 
765
        
 
766
        This happens when a current client talks to an older server that
 
767
        doesn't implement 'Repository.tarball'.
 
768
        """
 
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)
 
774
 
 
775
        error_msg = (
 
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(
 
780
            responses, 'path')
 
781
        mock_real_repo = MockRealRepository()
 
782
        remote_repo._real_repository = mock_real_repo
 
783
 
 
784
        # try to copy...
 
785
        remote_repo.sprout(dest_bzrdir)
 
786
 
 
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.")
 
792
 
 
793
 
 
794
class MockRealRepository(object):
 
795
    """Mock of a RemoteRepository's '_real_repository' attribute.
 
796
    
 
797
    Used by TestRepositoryTarball.test_backwards_compatibility.
 
798
    """
 
799
 
 
800
    def __init__(self):
 
801
        self.calls = []
 
802
 
 
803
    def sprout(self, to_bzrdir, revision_id=None):
 
804
        self.calls.append(('sprout', to_bzrdir, revision_id))
 
805
 
754
806
 
755
807
class TestRemoteRepositoryCopyContent(tests.TestCaseWithTransport):
756
808
    """RemoteRepository.copy_content_into optimizations"""