cocogni4d / code /evaluation-code /v0.2-0c2690ae /tests /pipeline /test_gen_cache_atomic.py
VEHwang's picture
Upload CoCogni4D-Bench (questions, media, scripts, render records, code) (part 7)
7ff74f6 verified
Raw History Blame Contribute Delete
2.25 kB
"""Cache files are written whole or not at all: a run killed mid-write leaves nothing that later runs load (10-22 EOFError)."""
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch
import numpy as np
from embodied_pipeline.gen import scene
class Atomic(unittest.TestCase):
def test_visibility_version_and_parameters_invalidate_both_caches(self):
with tempfile.TemporaryDirectory() as d:
h = scene.House.__new__(scene.House)
h.cache_dir, h.key = Path(d), 'house'
h.cell, h.origin, h.stops = .1, np.zeros(2), np.array([[.4, .4]])
with patch.object(h, '_block', return_value=np.zeros((8, 8), bool)) as build:
h.vis_table(1., max_m=1.)
self.assertEqual(build.call_count, 1)
# A fresh object must read disk, not just reuse the in-process table.
h2 = scene.House.__new__(scene.House)
h2.__dict__.update({k: v for k, v in h.__dict__.items() if not k.startswith('_vis_')})
h2.vis_table(1., max_m=1.)
self.assertEqual(build.call_count, 1)
with patch.object(scene, 'VIS_VERSION', scene.VIS_VERSION+1):
h.vis_table(1., max_m=1.)
h.vis_table(1., lo_m=.5, max_m=1.)
h.vis_table(1., max_m=1.1)
self.assertEqual(build.call_count, 4)
self.assertEqual(len(list(Path(d).glob('*.npz'))), 4)
def test_interrupted_write_leaves_no_file_whole_write_loads(self):
with tempfile.TemporaryDirectory() as d:
path = Path(d)/'h-vis.npz'
def dies(part, **arrays):
Path(part).write_bytes(b'PK') # a truncated archive, then the process is killed
raise KeyboardInterrupt
with self.assertRaises(KeyboardInterrupt):
scene._save(path, dies, vis=np.ones(3, bool))
self.assertFalse(path.exists())
scene._save(path, np.savez_compressed, vis=np.ones(3, bool), W=2)
with np.load(path) as z:
self.assertEqual((z['vis'].tolist(), int(z['W'])), ([True]*3, 2))
if __name__ == '__main__':
unittest.main()