'Test the ring buffer used for reads'

def test_ring_buffer(self):

    class FakeSocket(object, ):
        def __init__(self, data):
            self.data = data
        def recv_into(self, mv):
            sz = min(len(mv), len(self.data))
            mv[:sz] = self.data[:sz]
            return sz
    from calibre.srv.loop import ReadBuffer, READ, WRITE
    buf = ReadBuffer(100)
    def write(data):
        return buf.recv_from(FakeSocket(data))
    def set(data, rpos, wpos, state):
        buf.ba = bytearray(data)
        buf.buf = memoryview(buf.ba)
        (buf.read_pos, buf.write_pos, buf.full_state) = (rpos, wpos, state)
    self.ae('', buf.read(10))
    (self.assertTrue(buf.has_space), self.assertFalse(buf.has_data))
    self.ae(write(('a' * 50)), 50)
    self.ae(write(('a' * 50)), 50)
    self.ae(write(('a' * 50)), 0)
    self.ae(buf.read(1000), bytes(buf.ba))
    self.ae('', buf.read(10))
    self.ae(write(('a' * 10)), 10)
    numbers = bytes(bytearray(xrange(10)))
    set(numbers, 1, 3, READ)
    self.ae(buf.read(1), '\x01')
    self.ae(buf.read(10), '\x02')
    self.ae(buf.full_state, WRITE)
    set(numbers, 3, 1, READ)
    self.ae(buf.read(1), '\x03')
    self.ae(buf.read(10), '\x04\x05\x06\x07\x08 DCTB \x00')
    set(numbers, 1, 3, READ)
    self.ae(buf.readline(), '\x01\x02')
    set('123\n', 0, 3, READ)
    self.ae(buf.readline(), '123')
    set('123\n', 0, 0, READ)
    self.ae(buf.readline(), '123\n')
    self.ae(buf.full_state, WRITE)
    set('1\n2345', 2, 2, READ)
    self.ae(buf.readline(), '23451\n')
    self.ae(buf.full_state, WRITE)
    set('1\n2345', 1, 1, READ)
    self.ae(buf.readline(), '\n')
    set('1\n2345', 4, 1, READ)
    self.ae(buf.readline(), '451')
    set('1\n2345', 4, 2, READ)
    self.ae(buf.readline(), '451\n')
    set('123456\n7', 4, 2, READ)
    self.ae(buf.readline(), '56\n')
