~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: 2007-08-31 15:30:02 UTC
  • mto: (2535.4.5 streaming-smart-fetch)
  • mto: This revision was merged to the branch mainline in revision 2985.
  • Revision ID: andrew.bennetts@canonical.com-20070831153002-2e21vf67prklfdey
Allow an error to interrupt (and terminate) a streamed response body.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1638
1638
        """
1639
1639
        self.assertBodyStreamSerialisation('5\nchunk' + '0\n', ['', 'chunk'])
1640
1640
 
 
1641
    def test_body_stream_error_serialistion(self):
 
1642
        stream = ['first chunk',
 
1643
                  request.FailedSmartServerResponse(
 
1644
                      ('FailureName', 'failure arg'))]
 
1645
        expected_bytes = (
 
1646
            'b\nfirst chunk' +
 
1647
            'ERR\nFailureName\x01failure arg\n')
 
1648
        self.assertBodyStreamSerialisation(expected_bytes, stream)
 
1649
 
1641
1650
    def test_accept_bytes_of_bad_request_to_protocol(self):
1642
1651
        out_stream = StringIO()
1643
1652
        smart_protocol = protocol.SmartServerRequestProtocolTwo(
1895
1904
        stream = smart_protocol.read_streamed_body()
1896
1905
        self.assertEqual(['1234', '567'], list(stream))
1897
1906
 
 
1907
    def test_read_streamed_body_error(self):
 
1908
        """When a stream is interrupted by an error..."""
 
1909
        a_body_chunk = '4\naaaa'
 
1910
        err_chunk = 'ERR\nErrorArg1\x01arg2\n'
 
1911
        server_bytes = (protocol.RESPONSE_VERSION_TWO +
 
1912
                        "success\nok\n" + a_body_chunk + err_chunk)
 
1913
        input = StringIO(server_bytes)
 
1914
        output = StringIO()
 
1915
        client_medium = medium.SmartSimplePipesClientMedium(input, output)
 
1916
        smart_request = client_medium.get_request()
 
1917
        smart_protocol = protocol.SmartClientRequestProtocolTwo(smart_request)
 
1918
        smart_protocol.call('foo')
 
1919
        smart_protocol.read_response_tuple(True)
 
1920
        expected_chunks = [
 
1921
            'aaaa',
 
1922
            request.FailedSmartServerResponse(('ErrorArg1', 'arg2'))]
 
1923
        stream = smart_protocol.read_streamed_body()
 
1924
        self.assertEqual(expected_chunks, list(stream))
 
1925
 
1898
1926
 
1899
1927
class TestSmartClientUnicode(tests.TestCase):
1900
1928
    """_SmartClient tests for unicode arguments.
2007
2035
        decoder = protocol.ChunkedBodyDecoder()
2008
2036
        self.assertFalse(decoder.finished_reading)
2009
2037
        self.assertEqual(2, decoder.next_read_size())
2010
 
        self.assertEqual('', decoder.read_pending_data())
 
2038
        self.assertEqual(None, decoder.read_next_chunk())
2011
2039
        self.assertEqual('', decoder.unused_data)
2012
2040
 
2013
2041
    def test_empty_content(self):
2015
2043
        decoder = protocol.ChunkedBodyDecoder()
2016
2044
        decoder.accept_bytes('0\n')
2017
2045
        self.assertTrue(decoder.finished_reading)
2018
 
        self.assertEqual('', decoder.read_pending_data())
 
2046
        self.assertEqual(None, decoder.read_next_chunk())
2019
2047
        self.assertEqual('', decoder.unused_data)
2020
2048
 
2021
2049
    def test_one_chunk(self):
2026
2054
        finish = '0\n'
2027
2055
        decoder.accept_bytes(chunk_length + chunk_content + finish)
2028
2056
        self.assertTrue(decoder.finished_reading)
2029
 
        self.assertEqual(chunk_content, decoder.read_pending_data())
 
2057
        self.assertEqual(chunk_content, decoder.read_next_chunk())
2030
2058
        self.assertEqual('', decoder.unused_data)
2031
2059
        
2032
2060
    def test_incomplete_chunk(self):
2042
2070
            5 + 2, decoder.next_read_size(),
2043
2071
            "The next_read_size hint should be the number of missing bytes in "
2044
2072
            "this chunk plus 2 (the shortest possible next chunk: '0\\n')")
2045
 
        self.assertEqual(three_bytes, decoder.read_pending_data())
 
2073
        self.assertEqual(None, decoder.read_next_chunk())
2046
2074
 
2047
2075
    def test_incomplete_length(self):
2048
2076
        """A chunk length hasn't been read until a newline byte has been read.
2059
2087
            "The next_read_size hint should be the length of the chunk plus 2 "
2060
2088
            "(the shortest possible next chunk: '0\\n')")
2061
2089
        self.assertFalse(decoder.finished_reading)
2062
 
        self.assertEqual('', decoder.read_pending_data())
 
2090
        self.assertEqual(None, decoder.read_next_chunk())
2063
2091
 
2064
2092
    def test_two_chunks(self):
2065
2093
        """Content from multiple chunks is concatenated."""
2069
2097
        finish = '0\n'
2070
2098
        decoder.accept_bytes(chunk_one + chunk_two + finish)
2071
2099
        self.assertTrue(decoder.finished_reading)
2072
 
        self.assertEqual('aaabbbbb', decoder.read_pending_data())
 
2100
        self.assertEqual('aaa', decoder.read_next_chunk())
 
2101
        self.assertEqual('bbbbb', decoder.read_next_chunk())
 
2102
        self.assertEqual(None, decoder.read_next_chunk())
2073
2103
        self.assertEqual('', decoder.unused_data)
2074
2104
 
2075
2105
    def test_excess_bytes(self):
2079
2109
        excess_bytes = "excess bytes"
2080
2110
        decoder.accept_bytes(chunked_body + excess_bytes)
2081
2111
        self.assertTrue(decoder.finished_reading)
2082
 
        self.assertEqual('aaaaa', decoder.read_pending_data())
 
2112
        self.assertEqual('aaaaa', decoder.read_next_chunk())
2083
2113
        self.assertEqual(excess_bytes, decoder.unused_data)
2084
2114
        self.assertEqual(
2085
2115
            1, decoder.next_read_size(),
2094
2124
        finish = '0\n'
2095
2125
        decoder.accept_bytes(chunk_prefix + chunk_bytes + finish)
2096
2126
        self.assertTrue(decoder.finished_reading)
2097
 
        self.assertEqual(chunk_bytes, decoder.read_pending_data())
 
2127
        self.assertEqual(chunk_bytes, decoder.read_next_chunk())
2098
2128
 
2099
2129
    def test_byte_at_a_time(self):
2100
2130
        """A complete body fed to the decoder one byte at a time should not
2111
2141
        for byte in (chunk_length + chunk_content + finish):
2112
2142
            decoder.accept_bytes(byte)
2113
2143
        self.assertTrue(decoder.finished_reading)
2114
 
        self.assertEqual(chunk_content, decoder.read_pending_data())
 
2144
        self.assertEqual(chunk_content, decoder.read_next_chunk())
2115
2145
        self.assertEqual('', decoder.unused_data)
2116
2146
 
2117
2147
    def test_read_pending_data_resets(self):
2121
2151
        chunk_two = '3\nbbb'
2122
2152
        finish = '0\n'
2123
2153
        decoder.accept_bytes(chunk_one)
2124
 
        self.assertEqual('aaa', decoder.read_pending_data())
 
2154
        self.assertEqual('aaa', decoder.read_next_chunk())
2125
2155
        decoder.accept_bytes(chunk_two)
2126
 
        self.assertEqual('bbb', decoder.read_pending_data())
2127
 
        self.assertEqual('', decoder.read_pending_data())
 
2156
        self.assertEqual('bbb', decoder.read_next_chunk())
 
2157
        self.assertEqual(None, decoder.read_next_chunk())
 
2158
 
 
2159
    def test_decode_error(self):
 
2160
        decoder = protocol.ChunkedBodyDecoder()
 
2161
        chunk_one = 'b\nfirst chunk'
 
2162
        error_chunk = 'ERR\nFailureName\x01failure arg\n'
 
2163
        decoder.accept_bytes(chunk_one + error_chunk)
 
2164
        self.assertTrue(decoder.finished_reading)
 
2165
        self.assertEqual('first chunk', decoder.read_next_chunk())
 
2166
        expected_failure = request.FailedSmartServerResponse(
 
2167
            ('FailureName', 'failure arg'))
 
2168
        self.assertEqual(expected_failure, decoder.read_next_chunk())
2128
2169
 
2129
2170
 
2130
2171
class TestSuccessfulSmartServerResponse(tests.TestCase):