File size: 1,212 Bytes
8f228a5
 
f043c42
8f228a5
 
 
 
f043c42
 
 
 
 
 
 
 
 
8f228a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""Reproduce the Windows reader/writer collision observed on the live curves file."""
import json
import pytest

from novel_tokens import runtime


def test_local_postponement_does_not_consume_a_milestone(tmp_path, monkeypatch):
    monkeypatch.setattr(runtime,"STORE",tmp_path)
    runtime.check_postponed()
    (tmp_path/"postpone.request").touch()
    with pytest.raises(runtime.CampaignPostponed):
        runtime.check_postponed()
    assert not list(tmp_path.glob("*milestones/*.json"))


def test_atomic_writer_retries_sharing_violation(tmp_path, monkeypatch):
    dest = tmp_path / "curves.json"
    runtime.save_json(dest, {"step": 112})
    replace = runtime.os.replace
    attempts = []

    def held_once(source, target):
        attempts.append(target)
        if len(attempts) == 1:
            assert json.loads(dest.read_bytes()) == {"step": 112}
            error = PermissionError("test reader still holds the destination")
            error.winerror = 5
            raise error
        replace(source, target)

    monkeypatch.setattr(runtime.os, "replace", held_once)
    runtime.save_json(dest, {"step": 113})
    assert len(attempts) == 2 and json.loads(dest.read_bytes()) == {"step": 113}