/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smart_transport.py

  • Committer: Andrew Bennetts
  • Date: 2008-04-02 00:14:00 UTC
  • mfrom: (3324 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3756.
  • Revision ID: andrew.bennetts@canonical.com-20080402001400-r1pqse38i03dl97w
Merge from bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
        client,
35
35
        medium,
36
36
        protocol,
37
 
        request,
38
37
        request as _mod_request,
39
38
        server,
40
39
        vfs,
1144
1143
    """
1145
1144
        
1146
1145
    def test_hello(self):
1147
 
        cmd = request.HelloRequest(None)
 
1146
        cmd = _mod_request.HelloRequest(None, '/')
1148
1147
        response = cmd.execute()
1149
1148
        self.assertEqual(('ok', '2'), response.args)
1150
1149
        self.assertEqual(None, response.body)
1156
1155
        wt.add('hello')
1157
1156
        rev_id = wt.commit('add hello')
1158
1157
        
1159
 
        cmd = request.GetBundleRequest(self.get_transport())
 
1158
        cmd = _mod_request.GetBundleRequest(self.get_transport(), '/')
1160
1159
        response = cmd.execute('.', rev_id)
1161
1160
        bundle = serializer.read_bundle(StringIO(response.body))
1162
1161
        self.assertEqual((), response.args)
1171
1170
 
1172
1171
    def build_handler(self, transport):
1173
1172
        """Returns a handler for the commands in protocol version one."""
1174
 
        return request.SmartServerRequestHandler(transport,
1175
 
                                                 request.request_handlers)
 
1173
        return _mod_request.SmartServerRequestHandler(
 
1174
            transport, _mod_request.request_handlers, '/')
1176
1175
 
1177
1176
    def test_construct_request_handler(self):
1178
1177
        """Constructing a request handler should be easy and set defaults."""
1179
 
        handler = request.SmartServerRequestHandler(None, None)
 
1178
        handler = _mod_request.SmartServerRequestHandler(None, commands=None,
 
1179
                root_client_path='/')
1180
1180
        self.assertFalse(handler.finished_reading)
1181
1181
 
1182
1182
    def test_hello(self):
1188
1188
    def test_disable_vfs_handler_classes_via_environment(self):
1189
1189
        # VFS handler classes will raise an error from "execute" if
1190
1190
        # BZR_NO_SMART_VFS is set.
1191
 
        handler = vfs.HasRequest(None)
 
1191
        handler = vfs.HasRequest(None, '/')
1192
1192
        # set environment variable after construction to make sure it's
1193
1193
        # examined.
1194
1194
        # Note that we can safely clobber BZR_NO_SMART_VFS here, because setUp
1253
1253
        handler.accept_body('100,1')
1254
1254
        handler.end_of_body()
1255
1255
        self.assertTrue(handler.finished_reading)
1256
 
        self.assertEqual(('ShortReadvError', 'a-file', '100', '1', '0'),
 
1256
        self.assertEqual(('ShortReadvError', './a-file', '100', '1', '0'),
1257
1257
            handler.response.args)
1258
1258
        self.assertEqual(None, handler.response.body)
1259
1259
 
1344
1344
            self.to_server)
1345
1345
        self.client_protocol = self.client_protocol_class(self.client_medium)
1346
1346
        self.smart_server = InstrumentedServerProtocol(self.server_to_client)
1347
 
        self.smart_server_request = request.SmartServerRequestHandler(
1348
 
            None, request.request_handlers)
 
1347
        self.smart_server_request = _mod_request.SmartServerRequestHandler(
 
1348
            None, _mod_request.request_handlers, root_client_path='/')
1349
1349
 
1350
1350
    def assertOffsetSerialisation(self, expected_offsets, expected_serialised,
1351
1351
        client):
1360
1360
        """
1361
1361
        # XXX: '_deserialise_offsets' should be a method of the
1362
1362
        # SmartServerRequestProtocol in future.
1363
 
        readv_cmd = vfs.ReadvRequest(None)
 
1363
        readv_cmd = vfs.ReadvRequest(None, '/')
1364
1364
        offsets = readv_cmd._deserialise_offsets(expected_serialised)
1365
1365
        self.assertEqual(expected_offsets, offsets)
1366
1366
        serialised = client._serialise_offsets(offsets)
1375
1375
            def do_body(cmd, body_bytes):
1376
1376
                self.end_received = True
1377
1377
                self.assertEqual('abcdefg', body_bytes)
1378
 
                return request.SuccessfulSmartServerResponse(('ok', ))
 
1378
                return _mod_request.SuccessfulSmartServerResponse(('ok', ))
1379
1379
        smart_protocol.request._command = FakeCommand()
1380
1380
        # Call accept_bytes to make sure that internal state like _body_decoder
1381
1381
        # is initialised.  This test should probably be given a clearer
1540
1540
            None, lambda x: None)
1541
1541
        self.assertEqual(1, smart_protocol.next_read_size())
1542
1542
        smart_protocol._send_response(
1543
 
            request.SuccessfulSmartServerResponse(('x',)))
 
1543
            _mod_request.SuccessfulSmartServerResponse(('x',)))
1544
1544
        self.assertEqual(0, smart_protocol.next_read_size())
1545
1545
 
1546
1546
    def test__send_response_errors_with_base_response(self):
1548
1548
        smart_protocol = protocol.SmartServerRequestProtocolOne(
1549
1549
            None, lambda x: None)
1550
1550
        self.assertRaises(AttributeError, smart_protocol._send_response,
1551
 
            request.SmartServerResponse(('x',)))
 
1551
            _mod_request.SmartServerResponse(('x',)))
1552
1552
 
1553
1553
    def test_query_version(self):
1554
1554
        """query_version on a SmartClientProtocolOne should return a number.
1735
1735
 
1736
1736
    def test_body_stream_error_serialistion(self):
1737
1737
        stream = ['first chunk',
1738
 
                  request.FailedSmartServerResponse(
 
1738
                  _mod_request.FailedSmartServerResponse(
1739
1739
                      ('FailureName', 'failure arg'))]
1740
1740
        expected_bytes = (
1741
1741
            'chunked\n' + 'b\nfirst chunk' +
1827
1827
            None, lambda x: None)
1828
1828
        self.assertEqual(1, smart_protocol.next_read_size())
1829
1829
        smart_protocol._send_response(
1830
 
            request.SuccessfulSmartServerResponse(('x',)))
 
1830
            _mod_request.SuccessfulSmartServerResponse(('x',)))
1831
1831
        self.assertEqual(0, smart_protocol.next_read_size())
1832
1832
 
1833
1833
    def test__send_response_with_body_stream_sets_finished_reading(self):
1835
1835
            None, lambda x: None)
1836
1836
        self.assertEqual(1, smart_protocol.next_read_size())
1837
1837
        smart_protocol._send_response(
1838
 
            request.SuccessfulSmartServerResponse(('x',), body_stream=[]))
 
1838
            _mod_request.SuccessfulSmartServerResponse(('x',), body_stream=[]))
1839
1839
        self.assertEqual(0, smart_protocol.next_read_size())
1840
1840
 
1841
1841
    def test__send_response_errors_with_base_response(self):
1843
1843
        smart_protocol = protocol.SmartServerRequestProtocolTwo(
1844
1844
            None, lambda x: None)
1845
1845
        self.assertRaises(AttributeError, smart_protocol._send_response,
1846
 
            request.SmartServerResponse(('x',)))
 
1846
            _mod_request.SmartServerResponse(('x',)))
1847
1847
 
1848
1848
    def test__send_response_includes_failure_marker(self):
1849
1849
        """FailedSmartServerResponse have 'failed\n' after the version."""
1851
1851
        smart_protocol = protocol.SmartServerRequestProtocolTwo(
1852
1852
            None, out_stream.write)
1853
1853
        smart_protocol._send_response(
1854
 
            request.FailedSmartServerResponse(('x',)))
 
1854
            _mod_request.FailedSmartServerResponse(('x',)))
1855
1855
        self.assertEqual(protocol.RESPONSE_VERSION_TWO + 'failed\nx\n',
1856
1856
                         out_stream.getvalue())
1857
1857
 
1861
1861
        smart_protocol = protocol.SmartServerRequestProtocolTwo(
1862
1862
            None, out_stream.write)
1863
1863
        smart_protocol._send_response(
1864
 
            request.SuccessfulSmartServerResponse(('x',)))
 
1864
            _mod_request.SuccessfulSmartServerResponse(('x',)))
1865
1865
        self.assertEqual(protocol.RESPONSE_VERSION_TWO + 'success\nx\n',
1866
1866
                         out_stream.getvalue())
1867
1867
 
2022
2022
        smart_protocol.read_response_tuple(True)
2023
2023
        expected_chunks = [
2024
2024
            'aaaa',
2025
 
            request.FailedSmartServerResponse(('error arg1', 'arg2'))]
 
2025
            _mod_request.FailedSmartServerResponse(('error arg1', 'arg2'))]
2026
2026
        stream = smart_protocol.read_streamed_body()
2027
2027
        self.assertEqual(expected_chunks, list(stream))
2028
2028
 
2047
2047
        input = StringIO("\n")
2048
2048
        output = StringIO()
2049
2049
        client_medium = medium.SmartSimplePipesClientMedium(input, output)
2050
 
        smart_client = client._SmartClient(client_medium)
 
2050
        smart_client = client._SmartClient(client_medium, 'ignored base')
2051
2051
        self.assertRaises(TypeError,
2052
2052
            smart_client.call_with_body_bytes, method, args, body)
2053
2053
        self.assertEqual("", output.getvalue())
2283
2283
        decoder.accept_bytes(chunk_one + error_signal + error_chunks + finish)
2284
2284
        self.assertTrue(decoder.finished_reading)
2285
2285
        self.assertEqual('first chunk', decoder.read_next_chunk())
2286
 
        expected_failure = request.FailedSmartServerResponse(
 
2286
        expected_failure = _mod_request.FailedSmartServerResponse(
2287
2287
            ('part1', 'part2'))
2288
2288
        self.assertEqual(expected_failure, decoder.read_next_chunk())
2289
2289
 
2299
2299
class TestSuccessfulSmartServerResponse(tests.TestCase):
2300
2300
 
2301
2301
    def test_construct_no_body(self):
2302
 
        response = request.SuccessfulSmartServerResponse(('foo', 'bar'))
 
2302
        response = _mod_request.SuccessfulSmartServerResponse(('foo', 'bar'))
2303
2303
        self.assertEqual(('foo', 'bar'), response.args)
2304
2304
        self.assertEqual(None, response.body)
2305
2305
 
2306
2306
    def test_construct_with_body(self):
2307
 
        response = request.SuccessfulSmartServerResponse(
2308
 
            ('foo', 'bar'), 'bytes')
 
2307
        response = _mod_request.SuccessfulSmartServerResponse(('foo', 'bar'),
 
2308
                                                              'bytes')
2309
2309
        self.assertEqual(('foo', 'bar'), response.args)
2310
2310
        self.assertEqual('bytes', response.body)
2311
2311
        # repr(response) doesn't trigger exceptions.
2313
2313
 
2314
2314
    def test_construct_with_body_stream(self):
2315
2315
        bytes_iterable = ['abc']
2316
 
        response = request.SuccessfulSmartServerResponse(
 
2316
        response = _mod_request.SuccessfulSmartServerResponse(
2317
2317
            ('foo', 'bar'), body_stream=bytes_iterable)
2318
2318
        self.assertEqual(('foo', 'bar'), response.args)
2319
2319
        self.assertEqual(bytes_iterable, response.body_stream)
2322
2322
        """'body' and 'body_stream' are mutually exclusive."""
2323
2323
        self.assertRaises(
2324
2324
            errors.BzrError,
2325
 
            request.SuccessfulSmartServerResponse, (), 'body', ['stream'])
 
2325
            _mod_request.SuccessfulSmartServerResponse, (), 'body', ['stream'])
2326
2326
 
2327
2327
    def test_is_successful(self):
2328
2328
        """is_successful should return True for SuccessfulSmartServerResponse."""
2329
 
        response = request.SuccessfulSmartServerResponse(('error',))
 
2329
        response = _mod_request.SuccessfulSmartServerResponse(('error',))
2330
2330
        self.assertEqual(True, response.is_successful())
2331
2331
 
2332
2332
 
2333
2333
class TestFailedSmartServerResponse(tests.TestCase):
2334
2334
 
2335
2335
    def test_construct(self):
2336
 
        response = request.FailedSmartServerResponse(('foo', 'bar'))
 
2336
        response = _mod_request.FailedSmartServerResponse(('foo', 'bar'))
2337
2337
        self.assertEqual(('foo', 'bar'), response.args)
2338
2338
        self.assertEqual(None, response.body)
2339
 
        response = request.FailedSmartServerResponse(('foo', 'bar'), 'bytes')
 
2339
        response = _mod_request.FailedSmartServerResponse(('foo', 'bar'), 'bytes')
2340
2340
        self.assertEqual(('foo', 'bar'), response.args)
2341
2341
        self.assertEqual('bytes', response.body)
2342
2342
        # repr(response) doesn't trigger exceptions.
2344
2344
 
2345
2345
    def test_is_successful(self):
2346
2346
        """is_successful should return False for FailedSmartServerResponse."""
2347
 
        response = request.FailedSmartServerResponse(('error',))
 
2347
        response = _mod_request.FailedSmartServerResponse(('error',))
2348
2348
        self.assertEqual(False, response.is_successful())
2349
2349
 
2350
2350