~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smart_transport.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-04-10 14:22:21 UTC
  • mfrom: (3236.3.6 robust-push-back)
  • Revision ID: pqm@pqm.ubuntu.com-20080410142221-5peelooccmd1uc8g
Fix _get_line logic in smart protocol. (Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

Lines of Context:
433
433
        # now disconnect again: this should not do anything, if disconnection
434
434
        # really did disconnect.
435
435
        medium.disconnect()
 
436
 
436
437
    
437
438
    def test_tcp_client_ignores_disconnect_when_not_connected(self):
438
439
        # Doing a disconnect on a new (and thus unconnected) TCP medium
732
733
        server._serve_one_request(SampleRequest('x'))
733
734
        self.assertTrue(server.finished)
734
735
        
 
736
    def test_socket_stream_incomplete_request(self):
 
737
        """The medium should still construct the right protocol version even if
 
738
        the initial read only reads part of the request.
 
739
 
 
740
        Specifically, it should correctly read the protocol version line even
 
741
        if the partial read doesn't end in a newline.  An older, naive
 
742
        implementation of _get_line in the server used to have a bug in that
 
743
        case.
 
744
        """
 
745
        incomplete_request_bytes = protocol.REQUEST_VERSION_TWO + 'hel'
 
746
        rest_of_request_bytes = 'lo\n'
 
747
        expected_response = (
 
748
            protocol.RESPONSE_VERSION_TWO + 'success\nok\x012\n')
 
749
        server_sock, client_sock = self.portable_socket_pair()
 
750
        server = medium.SmartServerSocketStreamMedium(
 
751
            server_sock, None)
 
752
        client_sock.sendall(incomplete_request_bytes)
 
753
        server_protocol = server._build_protocol()
 
754
        client_sock.sendall(rest_of_request_bytes)
 
755
        server._serve_one_request(server_protocol)
 
756
        server_sock.close()
 
757
        self.assertEqual(expected_response, client_sock.recv(50),
 
758
                         "Not a version 2 response to 'hello' request.")
 
759
        self.assertEqual('', client_sock.recv(1))
 
760
 
 
761
    def test_pipe_stream_incomplete_request(self):
 
762
        """The medium should still construct the right protocol version even if
 
763
        the initial read only reads part of the request.
 
764
 
 
765
        Specifically, it should correctly read the protocol version line even
 
766
        if the partial read doesn't end in a newline.  An older, naive
 
767
        implementation of _get_line in the server used to have a bug in that
 
768
        case.
 
769
        """
 
770
        incomplete_request_bytes = protocol.REQUEST_VERSION_TWO + 'hel'
 
771
        rest_of_request_bytes = 'lo\n'
 
772
        expected_response = (
 
773
            protocol.RESPONSE_VERSION_TWO + 'success\nok\x012\n')
 
774
        # Make a pair of pipes, to and from the server
 
775
        to_server, to_server_w = os.pipe()
 
776
        from_server_r, from_server = os.pipe()
 
777
        to_server = os.fdopen(to_server, 'r', 0)
 
778
        to_server_w = os.fdopen(to_server_w, 'w', 0)
 
779
        from_server_r = os.fdopen(from_server_r, 'r', 0)
 
780
        from_server = os.fdopen(from_server, 'w', 0)
 
781
        server = medium.SmartServerPipeStreamMedium(
 
782
            to_server, from_server, None)
 
783
        # Like test_socket_stream_incomplete_request, write an incomplete
 
784
        # request (that does not end in '\n') and build a protocol from it.
 
785
        to_server_w.write(incomplete_request_bytes)
 
786
        server_protocol = server._build_protocol()
 
787
        # Send the rest of the request, and finish serving it.
 
788
        to_server_w.write(rest_of_request_bytes)
 
789
        server._serve_one_request(server_protocol)
 
790
        to_server_w.close()
 
791
        from_server.close()
 
792
        self.assertEqual(expected_response, from_server_r.read(),
 
793
                         "Not a version 2 response to 'hello' request.")
 
794
        self.assertEqual('', from_server_r.read(1))
 
795
        from_server_r.close()
 
796
        to_server.close()
 
797
 
735
798
    def test_pipe_like_stream_with_two_requests(self):
736
799
        # If two requests are read in one go, then two calls to
737
800
        # _serve_one_request should still process both of them as if they had