Esempio n. 1
0
def test_simple_read_success(caplog):
    caplog.set_level(logging.INFO)
    texts = TwoPhaserStageTexts()
    with TwoPhaserStageFiles(texts) as stage_files:
        stage_files.prepare_files({WhichFiles.PRIMARY})
        with two_phase_open(stage_files.primary, 'r') as f:
            assert str(stage_files.primary) == f.name
            assert texts.primary == f.read()
Esempio n. 2
0
def test_recovery_notemporary_noprimary_havebackup_writing(caplog):
    caplog.set_level(logging.INFO)
    texts = TwoPhaserStageTexts()
    with TwoPhaserStageFiles(texts) as stage_files:
        # No Temporary, No Primary, Have Backup, Writing
        #   --> preserve Backup but otherwise a normal commit
        stage_files.prepare_files({WhichFiles.BACKUP})
        with two_phase_open(stage_files.primary, 'w') as f:
            assert str(stage_files.temporary) == f.name
            f.write(texts.primary)
        assert stage_files.primary.exists()
        assert stage_files.backup.exists()
        assert not stage_files.temporary.exists()
        assert not stage_files.probe.exists()
        with two_phase_open(stage_files.primary, 'r') as f:
            assert str(stage_files.primary) == f.name
            assert texts.primary == f.read()
        assert texts.backup == stage_files.backup.read_text()
Esempio n. 3
0
def test_recovery_notemporary_noprimary_havebackup_reading(caplog):
    caplog.set_level(logging.INFO)
    texts = TwoPhaserStageTexts()
    with TwoPhaserStageFiles(texts) as stage_files:
        # No Temporary, No Primary, Have Backup, Reading
        #   --> read from the Backup
        stage_files.prepare_files({WhichFiles.BACKUP})
        with two_phase_open(stage_files.primary, 'r') as f:
            assert str(stage_files.backup) == f.name
            assert texts.backup == f.read()
        assert not stage_files.primary.exists()
        assert stage_files.backup.exists()
        assert not stage_files.temporary.exists()
Esempio n. 4
0
def test_recovery_havetemporary_haveprimary_nobackup(caplog):
    caplog.set_level(logging.INFO)
    texts = TwoPhaserStageTexts()
    with TwoPhaserStageFiles(texts) as stage_files:
        # Have Temporary, Have Primary, No Backup --> no recovery (rollback)
        stage_files.prepare_files({WhichFiles.TEMPORARY, WhichFiles.PRIMARY})
        with two_phase_open(stage_files.primary, 'r') as f:
            assert str(stage_files.primary) == f.name
            assert texts.primary == f.read()
        assert stage_files.primary.exists()
        assert not stage_files.backup.exists()
        assert not stage_files.temporary.exists()
        assert not stage_files.probe.exists()
Esempio n. 5
0
def test_recovery_havetemporary_noprimary_havebackup(caplog):
    caplog.set_level(logging.INFO)
    texts = TwoPhaserStageTexts()
    with TwoPhaserStageFiles(texts) as stage_files:
        # Have Temporary, No Primary, Have Backup
        #   --> recover (commit)
        stage_files.prepare_files({WhichFiles.TEMPORARY, WhichFiles.BACKUP})
        with two_phase_open(stage_files.primary, 'r') as f:
            assert str(stage_files.primary) == f.name
            assert texts.temporary == f.read()
        assert stage_files.primary.exists()
        assert stage_files.backup.exists()
        assert not stage_files.temporary.exists()
        assert not stage_files.probe.exists()
Esempio n. 6
0
def test_simple_write_read_success(caplog):
    caplog.set_level(logging.INFO)
    texts = TwoPhaserStageTexts()
    with TwoPhaserStageFiles(texts) as stage_files:
        # First write: primary exists, backup does not
        with two_phase_open(stage_files.primary, 'w') as f:
            assert str(stage_files.temporary) == f.name
            f.write(texts.primary)
        assert stage_files.primary.exists()
        assert not stage_files.backup.exists()
        assert not stage_files.temporary.exists()
        assert not stage_files.probe.exists()
        with two_phase_open(stage_files.primary, 'r') as f:
            assert str(stage_files.primary) == f.name
            assert texts.primary == f.read()
        # Second write: both exist
        with two_phase_open(stage_files.primary, 'w') as f:
            assert str(stage_files.temporary) == f.name
            f.write(texts.backup)
        assert stage_files.primary.exists()
        assert stage_files.backup.exists()
        assert not stage_files.temporary.exists()
        assert not stage_files.probe.exists()
        with two_phase_open(stage_files.primary, 'r') as f:
            assert str(stage_files.primary) == f.name
            assert texts.backup == f.read()
        assert texts.primary == stage_files.backup.read_text()
        # Third write: both exist
        with two_phase_open(stage_files.primary, 'w') as f:
            assert str(stage_files.temporary) == f.name
            f.write(texts.temporary)
        assert stage_files.primary.exists()
        assert stage_files.backup.exists()
        assert not stage_files.temporary.exists()
        assert not stage_files.probe.exists()
        with two_phase_open(stage_files.primary, 'r') as f:
            assert str(stage_files.primary) == f.name
            assert texts.temporary == f.read()
        assert texts.backup == stage_files.backup.read_text()
Esempio n. 7
0
def test_simple_read_failure(caplog):
    caplog.set_level(logging.INFO)
    with TwoPhaserStageFiles() as stage_files:
        with pytest.raises(FileNotFoundError):
            with two_phase_open(stage_files.primary, 'r') as f:
                text = f.read()