Ejemplo n.º 1
0
    def test_read_next_chunk_eof(self):
        with io.BytesIO() as stream:
            # If:
            # ... I create a reader with a stream that has no bytes
            reader = JSONRPCReader(stream, logger=utils.get_mock_logger())

            # ... and I read a chunk from the stream
            # Then: I should get an exception
            with self.assertRaises(EOFError):
                reader._read_next_chunk()
Ejemplo n.º 2
0
    def test_read_next_chunk_closed(self):
        # Setup: Create a stream that has already been closed
        stream = io.BytesIO()
        stream.close()

        # If:
        # ... I create a reader with a closed stream
        reader = JSONRPCReader(stream, logger=utils.get_mock_logger())

        # ... and I read a chunk from the stream
        # Then: I should get an exception
        with self.assertRaises(ValueError):
            reader._read_next_chunk()
Ejemplo n.º 3
0
    def test_read_next_chunk_resize(self):
        # Setup: Create a byte array for test input
        test_bytes = bytearray(b'1234567890')

        with io.BytesIO(test_bytes) as stream:
            # If:
            # ... I create a reader with an artificially low initial buffer size
            #     and prefill the buffer
            reader = JSONRPCReader(stream, logger=utils.get_mock_logger())
            reader._buffer = bytearray(5)
            reader._buffer_end_offset = 4

            # ... and I read a chunk from the stream
            result = reader._read_next_chunk()

            # Then:
            # ... The read should have succeeded
            self.assertTrue(result, True)

            # ... The size of the buffer should have doubled
            self.assertEqual(len(reader._buffer), 10)

            # ... The buffer end offset should be the size of the buffer
            self.assertEqual(reader._buffer_end_offset, 10)

            # ... The buffer should contain the first 6 elements of the test data
            expected = test_bytes[:6]
            actual = reader._buffer[4:]
            self.assertEqual(actual, expected)
Ejemplo n.º 4
0
    def test_read_next_chunk_success(self):
        # Setup: Create a byte array for test input
        test_bytes = bytearray(b'123')

        with io.BytesIO(test_bytes) as stream:
            # If: I attempt to read a chunk from the stream
            reader = JSONRPCReader(stream, logger=utils.get_mock_logger())
            result = reader._read_next_chunk()

            # Then:
            # ... The result should be true
            self.assertTrue(result)

            # ... The buffer should contain 3 byte and should not have been read yet
            self.assertEqual(reader._buffer_end_offset, len(test_bytes))
            self.assertEqual(reader._read_offset, 0)

            # ... The buffer should now contain the bytes from the stream
            buffer_contents = reader._buffer[:len(test_bytes)]
            self.assertEqual(buffer_contents, test_bytes)