~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smart_transport.py

  • Committer: Andrew Bennetts
  • Date: 2008-05-21 02:34:32 UTC
  • mto: (3452.2.9 inter-remote-pack)
  • mto: This revision was merged to the branch mainline in revision 3452.
  • Revision ID: andrew.bennetts@canonical.com-20080521023432-rj4qn81st9sou7np
Buffer encoding of v3 messages to minimise write/send calls.  Doubles the speed of pushing over TCP with 500ms latency loopback.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2566
2566
            'e', # end
2567
2567
            output.getvalue())
2568
2568
 
 
2569
    def test_call_writes_just_once(self):
 
2570
        """A bodyless request is written to the medium all at once."""
 
2571
        medium_request = StubMediumRequest()
 
2572
        encoder = protocol.ProtocolThreeRequester(medium_request)
 
2573
        encoder.call('arg1', 'arg2', 'arg3')
 
2574
        self.assertEqual(
 
2575
            ['accept_bytes', 'finished_writing'], medium_request.calls)
 
2576
 
 
2577
    def test_call_with_body_bytes_writes_just_once(self):
 
2578
        """A request with body bytes is written to the medium all at once."""
 
2579
        medium_request = StubMediumRequest()
 
2580
        encoder = protocol.ProtocolThreeRequester(medium_request)
 
2581
        encoder.call_with_body_bytes(('arg', 'arg'), 'body bytes')
 
2582
        self.assertEqual(
 
2583
            ['accept_bytes', 'finished_writing'], medium_request.calls)
 
2584
 
 
2585
 
 
2586
class StubMediumRequest(object):
 
2587
    """A stub medium request that tracks the number of times accept_bytes is
 
2588
    called.
 
2589
    """
 
2590
    def __init__(self):
 
2591
        self.calls = []
 
2592
    def accept_bytes(self, bytes):
 
2593
        self.calls.append('accept_bytes')
 
2594
    def finished_writing(self):
 
2595
        self.calls.append('finished_writing')
 
2596
 
2569
2597
 
2570
2598
class TestResponseEncodingProtocolThree(tests.TestCase):
2571
2599
 
2589
2617
            'e')
2590
2618
 
2591
2619
 
 
2620
class TestResponseEncoderBufferingProtocolThree(tests.TestCase):
 
2621
    """Tests for buffering of responses.
 
2622
 
 
2623
    We want to avoid doing many small writes when one would do, to avoid
 
2624
    unnecessary network overhead.
 
2625
    """
 
2626
 
 
2627
    def setUp(self):
 
2628
        self.writes = []
 
2629
        self.responder = protocol.ProtocolThreeResponder(self.writes.append)
 
2630
 
 
2631
    def assertWriteCount(self, expected_count):
 
2632
        self.assertEqual(
 
2633
            expected_count, len(self.writes),
 
2634
            "Too many writes: %r" % (self.writes,))
 
2635
        
 
2636
    def test_send_error_writes_just_once(self):
 
2637
        """An error response is written to the medium all at once."""
 
2638
        self.responder.send_error(Exception('An exception string.'))
 
2639
        self.assertWriteCount(1)
 
2640
 
 
2641
    def test_send_response_writes_just_once(self):
 
2642
        """A normal response with no body is written to the medium all at once.
 
2643
        """
 
2644
        response = _mod_request.SuccessfulSmartServerResponse(('arg', 'arg'))
 
2645
        self.responder.send_response(response)
 
2646
        self.assertWriteCount(1)
 
2647
 
 
2648
    def test_send_response_with_body_writes_just_once(self):
 
2649
        """A normal response with a monolithic body is written to the medium
 
2650
        all at once.
 
2651
        """
 
2652
        response = _mod_request.SuccessfulSmartServerResponse(
 
2653
            ('arg', 'arg'), body='body bytes')
 
2654
        self.responder.send_response(response)
 
2655
        self.assertWriteCount(1)
 
2656
 
 
2657
    def test_send_response_with_body_stream_writes_once_per_chunk(self):
 
2658
        """A normal response with a stream body is written to the medium
 
2659
        writes to the medium once per chunk.
 
2660
        """
 
2661
        # Construct a response with stream with 2 chunks in it.
 
2662
        response = _mod_request.SuccessfulSmartServerResponse(
 
2663
            ('arg', 'arg'), body_stream=['chunk1', 'chunk2'])
 
2664
        self.responder.send_response(response)
 
2665
        # We will write 3 times: exactly once for each chunk, plus a final
 
2666
        # write to end the response.
 
2667
        self.assertWriteCount(3)
 
2668
 
 
2669
 
2592
2670
class TestSmartClientUnicode(tests.TestCase):
2593
2671
    """_SmartClient tests for unicode arguments.
2594
2672