'Test log rotation'

def test_log_rotation(self):

    from calibre.srv.utils import RotatingLog
    from calibre.ptempfile import TemporaryDirectory
    with TemporaryDirectory() as tdir:
        fname = os.path.join(tdir, u'log')
        l = RotatingLog(fname, max_size=100)
        def history():
            return {int(x.rpartition(u'.')[(-1)]) for x in glob((fname + u'.*'))}
        def log_size():
            ssize = l.outputs[0].stream.tell()
            self.ae(ssize, l.outputs[0].current_pos)
            self.ae(ssize, os.path.getsize(fname))
            return ssize
        self.ae(log_size(), 0)
        l((u'a' * 99))
        self.ae(log_size(), 100)
        (l(u'b'), l(u'c'))
        self.ae(log_size(), 2)
        self.ae(history(), {1})
        for i in u'abcdefg':
            l((i * 101))
        self.assertLessEqual(log_size(), 100)
        self.ae(history(), {1, 2, 3, 4, 5})
