示例#1
0
def test_delete_and_read(num_reads):
    from binlog.reader import TDSReader
    from binlog.writer import TDSWriter
    MAX_LOG_EVENTS = 10
    try:
        tmpdir = mktemp()

        writer = TDSWriter(tmpdir, max_log_events=MAX_LOG_EVENTS)
        reader = TDSReader(tmpdir, checkpoint='test')

        for x in range(25):
            writer.append(str(x).encode("ascii"))

        for x in range(num_reads):
            reader.next_record()

        if num_reads > MAX_LOG_EVENTS:
            writer.delete(1)
        else:
            with pytest.raises(ValueError):
                writer.delete(1)
            pass
    except:
        raise
    finally:
        shutil.rmtree(tmpdir)
示例#2
0
def test_status_and_delete_match(reads):
    """reader.status is consistent with writer.delete."""
    from binlog.reader import TDSReader
    from binlog.writer import TDSWriter
    try:
        tmpdir = mktemp()

        writer = TDSWriter(tmpdir, max_log_events=MAX_LOG_EVENTS)
        reader = TDSReader(tmpdir, checkpoint='test')

        for x in range(25):
            writer.append(x)

        for x in range(reads):
            rec = reader.next_record()
            reader.ack(rec)
        reader.save()

        status = reader.status() 

        for idx, can_delete in status.items():
            if idx == max(status.keys()):
                # We cannot delete the last DB cause is current.
                assert not can_delete
                with pytest.raises(ValueError):
                    writer.delete(idx)
            else:
                if can_delete:
                    writer.delete(idx)
    except:
        raise
    finally:
        shutil.rmtree(tmpdir)
示例#3
0
def test_status_and_delete_match(reads):
    """reader.status is consistent with writer.delete."""
    from binlog.reader import TDSReader
    from binlog.writer import TDSWriter
    try:
        tmpdir = mktemp()

        writer = TDSWriter(tmpdir, max_log_events=MAX_LOG_EVENTS)
        reader = TDSReader(tmpdir, checkpoint='test')

        for x in range(25):
            writer.append(str(x).encode("ascii"))

        for x in range(reads):
            rec = reader.next_record()
            reader.ack(rec)
        reader.save()

        status = reader.status()

        for idx, can_delete in status.items():
            if idx == max(status.keys()):
                # We cannot delete the last DB cause is current.
                assert not can_delete
                with pytest.raises(ValueError):
                    writer.delete(idx)
            else:
                if can_delete:
                    writer.delete(idx)
    except:
        raise
    finally:
        shutil.rmtree(tmpdir)
示例#4
0
def test_reader_starts_with_the_lowest_database_available():
    from binlog.reader import TDSReader
    from binlog.writer import TDSWriter

    try:
        tmpdir = mktemp()

        writer = TDSWriter(tmpdir, max_log_events=10)

        for x in range(25):
            writer.append(str(x).encode("ascii"))

        writer.delete(1)

        reader = TDSReader(tmpdir, checkpoint='test')
        for x in range(10, 25):
            rec = reader.next_record()
            assert int(rec.value) == x
        assert reader.next_record() is None
    except:
        raise
    finally:
        shutil.rmtree(tmpdir)
示例#5
0
def test_Server_protocol_not_store_in_binlog_when_no_data():
    from binlog.server import Server
    from binlog.reader import TDSReader
    import tempfile

    with tempfile.TemporaryDirectory() as base:
        s = Server(base, "/tmp/server.sock")

        p = s.get_protocol()()
        p.connection_made(None)
        p.connection_lost(None)

        r = TDSReader(base)
        assert r.next_record() is None
示例#6
0
def test_Server_protocol_not_store_in_binlog_when_no_data():
    from binlog.server import Server
    from binlog.reader import TDSReader
    import tempfile

    with tempfile.TemporaryDirectory() as base:
        s = Server(base, "/tmp/server.sock")

        p = s.get_protocol()()
        p.connection_made(None)
        p.connection_lost(None)

        r = TDSReader(base)
        assert r.next_record() is None
示例#7
0
def test_Server_protocol_store_in_binlog():
    from binlog.server import Server
    from binlog.reader import TDSReader
    import tempfile

    with tempfile.TemporaryDirectory() as base:
        s = Server(base, "/tmp/server.sock")

        p = s.get_protocol()()
        p.connection_made(None)
        p.data_received(b"TEST")
        p.connection_lost(None)

        r = TDSReader(base)
        assert r.next_record().value == b"TEST"
示例#8
0
def test_Server_protocol_store_in_binlog():
    from binlog.server import Server
    from binlog.reader import TDSReader
    import tempfile

    with tempfile.TemporaryDirectory() as base:
        s = Server(base, "/tmp/server.sock")

        p = s.get_protocol()()
        p.connection_made(None)
        p.data_received(b"TEST")
        p.connection_lost(None)

        r = TDSReader(base)
        assert r.next_record().value == b"TEST"
示例#9
0
import signal
import time

from binlog.reader import TDSReader

r = TDSReader('test', checkpoint='status')

def save_status():
    """
    Save a checkpoint with the current status.
    
    Only the acknowledged entries will be included in the checkpoint.

    """
    r.save()

    
atexit.register(save_status)

for i in cycle(range(1, 20001)):
    if i == 20000:  # Make a checkpoint each 20k reads.
        save_status()

    n = r.next_record()  # Read the next log entry.

    if n is None:
        time.sleep(1)
    else:
        print('.', end='', flush=True)
        r.ack(n)  # Acknowledge the reception of the entry.