def test_preserve_error_subprocess(): pipe_recv, pipe_send = multiprocessing.Pipe(False) def child(c, pipe): try: c.preserve('preserved') except Exception as e: pipe.send(pickle.dumps(e)) finally: pipe.close() with tempfile.TemporaryDirectory() as d: cache = MultiprocessFileCache(10, dir=d, do_pickle=True) for i in range(10): cache.put(i, str(i)) # Run preservation in the subprocess p = multiprocessing.Process(target=child, args=(cache, pipe_send)) p.start() p.join() cache.close() e = pickle.loads(pipe_recv.recv()) assert isinstance(e, RuntimeError)
def test_cleanup(): with tempfile.TemporaryDirectory() as d: cache = MultiprocessFileCache(10, dir=d, do_pickle=True) for i in range(10): cache.put(i, str(i)) assert len(os.listdir(d)) == 1 cache.close() assert len(os.listdir(d)) == 0
def test_preservation_error_already_exists(): with tempfile.TemporaryDirectory() as d: cache = MultiprocessFileCache(10, dir=d, do_pickle=True) for i in range(10): cache.put(i, str(i)) cache.preserve('preserved') with pytest.raises(ValueError): cache.preserve('preserved') cache.close()
def test_preservation_interoperability(): with tempfile.TemporaryDirectory() as d: cache = MultiprocessFileCache(10, dir=d, do_pickle=True) for i in range(10): cache.put(i, str(i)) assert cache.preserve('preserved') is True cache.close() cache2 = FileCache(10, dir=d, do_pickle=True) assert cache2.preload('preserved') is True for i in range(10): assert str(i) == cache2.get(i) cache2.close()
def test_preservation(): with tempfile.TemporaryDirectory() as d: cache = MultiprocessFileCache(10, dir=d, do_pickle=True) for i in range(10): cache.put(i, str(i)) cache.preserve('preserved') cache.close() # Imitating a new process, fresh load cache2 = MultiprocessFileCache(10, dir=d, do_pickle=True) cache2.preload('preserved') for i in range(10): assert str(i) == cache2.get(i) cache2.close() # No temporary cache file should remain, # and the preserved cache should be kept. assert os.listdir(d) == ['preserved.cached', 'preserved.cachei']