1384
1384
client_protocol_class = None
1385
1385
server_protocol_class = None
1387
def make_client_protocol(self):
1388
client_medium = medium.SmartSimplePipesClientMedium(
1389
StringIO(), StringIO())
1390
return self.client_protocol_class(client_medium.get_request())
1392
def make_server_protocol(self):
1393
out_stream = StringIO()
1394
smart_protocol = self.server_protocol_class(None, out_stream.write)
1395
return smart_protocol, out_stream
1387
1397
def setUp(self):
1388
1398
super(TestSmartProtocol, self).setUp()
1389
self.to_server = StringIO()
1390
self.to_client = StringIO()
1391
self.client_medium = medium.SmartSimplePipesClientMedium(self.to_client,
1393
self.client_protocol = self.client_protocol_class(self.client_medium)
1394
self.smart_server = medium.SmartServerStreamMedium(
1395
memory.MemoryTransport())
1396
self.smart_server_request = request.SmartServerRequestHandler(
1397
None, request.request_handlers)
1398
1399
self.response_marker = getattr(
1399
1400
self.client_protocol_class, 'response_marker', None)
1400
1401
self.request_marker = getattr(
1420
1421
self.assertEqual(expected_serialised, serialised)
1422
1423
def build_protocol_waiting_for_body(self):
1423
out_stream = StringIO()
1424
smart_protocol = self.server_protocol_class(None, out_stream.write)
1424
smart_protocol, out_stream = self.make_server_protocol()
1425
1425
smart_protocol.has_dispatched = True
1426
smart_protocol.request = self.smart_server_request
1426
smart_protocol.request = request.SmartServerRequestHandler(
1427
None, request.request_handlers)
1427
1428
class FakeCommand(object):
1428
1429
def do_body(cmd, body_bytes):
1429
1430
self.end_received = True
1445
1446
# check the encoding of the server for all input_tuples matches
1446
1447
# expected bytes
1447
1448
for input_tuple in input_tuples:
1448
server_output = StringIO()
1449
server_protocol = self.server_protocol_class(
1450
None, server_output.write)
1449
server_protocol, server_output = self.make_server_protocol()
1451
1450
server_protocol._send_response(
1452
1451
_mod_request.SuccessfulSmartServerResponse(input_tuple))
1453
1452
self.assertEqual(expected_bytes, server_output.getvalue())
1464
1463
class CommonSmartProtocolTestMixin(object):
1466
def test_server_offset_serialisation(self):
1467
"""The Smart protocol serialises offsets as a comma and \n string.
1469
We check a number of boundary cases are as expected: empty, one offset,
1470
one with the order of reads not increasing (an out of order read), and
1471
one that should coalesce.
1473
self.assertOffsetSerialisation([], '', self.client_protocol)
1474
self.assertOffsetSerialisation([(1,2)], '1,2', self.client_protocol)
1475
self.assertOffsetSerialisation([(10,40), (0,5)], '10,40\n0,5',
1476
self.client_protocol)
1477
self.assertOffsetSerialisation([(1,2), (3,4), (100, 200)],
1478
'1,2\n3,4\n100,200', self.client_protocol)
1480
1465
def test_errors_are_logged(self):
1481
1466
"""If an error occurs during testing, it is logged to the test log."""
1482
1467
# XXX: should also test than an error inside a SmartServerRequest would
1484
out_stream = StringIO()
1485
smart_protocol = self.server_protocol_class(None, out_stream.write)
1469
smart_protocol, out_stream = self.make_server_protocol()
1486
1470
# This triggers a "bad request" error in all protocol versions.
1487
1471
smart_protocol.accept_bytes('\0\0\0\0malformed request\n')
1488
1472
test_log = self._get_log(keep_log_file=True)
1490
1474
self.assertContainsRe(test_log, 'SmartProtocolError')
1492
1476
def test_connection_closed_reporting(self):
1495
client_medium = medium.SmartSimplePipesClientMedium(input, output)
1496
request = client_medium.get_request()
1497
smart_protocol = self.client_protocol_class(request)
1477
smart_protocol = self.make_client_protocol()
1498
1478
smart_protocol.call('hello')
1499
ex = self.assertRaises(errors.ConnectionReset,
1479
ex = self.assertRaises(errors.ConnectionReset,
1500
1480
smart_protocol.read_response_tuple)
1501
1481
self.assertEqual("Connection closed: "
1502
1482
"please check connectivity and permissions "
1503
1483
"(and try -Dhpss if further diagnosis is required)", str(ex))
1485
def test_server_offset_serialisation(self):
1486
"""The Smart protocol serialises offsets as a comma and \n string.
1488
We check a number of boundary cases are as expected: empty, one offset,
1489
one with the order of reads not increasing (an out of order read), and
1490
one that should coalesce.
1492
client_protocol = self.make_client_protocol()
1493
self.assertOffsetSerialisation([], '', client_protocol)
1494
self.assertOffsetSerialisation([(1,2)], '1,2', client_protocol)
1495
self.assertOffsetSerialisation([(10,40), (0,5)], '10,40\n0,5',
1497
self.assertOffsetSerialisation([(1,2), (3,4), (100, 200)],
1498
'1,2\n3,4\n100,200', client_protocol)
1506
1501
class TestVersionOneFeaturesInProtocolOne(
1507
1502
TestSmartProtocol, CommonSmartProtocolTestMixin):