Repository navigation
Expand file tree
/
Copy pathtest_single_cache.py
More file actions
134 lines (109 loc) · 4.88 KB
/
Copy pathtest_single_cache.py
File metadata and controls
134 lines (109 loc) · 4.88 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import os
import shutil
import unittest
from time import sleep
from executorlib import SingleNodeExecutor, get_cache_data
from executorlib.standalone.serialize import cloudpickle_register
try:
import h5py
skip_h5py_test = False
except ImportError:
skip_h5py_test = True
def get_error(a):
raise ValueError(a)
def sum_with_wait(a, b):
sleep((a + b) / 10)
return a + b
def init_counter():
return {"counter": [0]}
def increment(counter):
counter[0] += 1
return counter[0]
class AddClass:
def __call__(self, a, b):
return a+b
@unittest.skipIf(
skip_h5py_test, "h5py is not installed, so the h5io tests are skipped."
)
class TestCacheFunctions(unittest.TestCase):
def test_cache_data(self):
cache_directory = os.path.abspath("executorlib_cache")
with SingleNodeExecutor(cache_directory=cache_directory) as exe:
self.assertTrue(exe)
future_lst = [exe.submit(sum, [i, i]) for i in range(1, 4)]
result_lst = [f.result() for f in future_lst]
cache_lst = get_cache_data(cache_directory=cache_directory)
self.assertEqual(sum([c["output"] for c in cache_lst]), sum(result_lst))
self.assertEqual(
sum([sum(c["input_args"][0]) for c in cache_lst]), sum(result_lst)
)
def test_cache_data_class(self):
cache_directory = os.path.abspath("executorlib_cache")
with SingleNodeExecutor(cache_directory=cache_directory) as exe:
self.assertTrue(exe)
cloudpickle_register(ind=1)
add_instance = AddClass()
future_lst = [exe.submit(add_instance, a=i, b=i) for i in range(1, 4)]
result_lst = [f.result() for f in future_lst]
cache_lst = get_cache_data(cache_directory=cache_directory)
self.assertEqual(sum([c["output"] for c in cache_lst]), sum(result_lst))
self.assertEqual(
sum([sum([c["input_kwargs"]["a"], c["input_kwargs"]["b"]]) for c in cache_lst]), sum(result_lst)
)
def test_cache_key(self):
cache_directory = os.path.abspath("executorlib_cache")
with SingleNodeExecutor(cache_directory=cache_directory) as exe:
self.assertTrue(exe)
future_lst = [exe.submit(sum, [i, i], resource_dict={"cache_key": "same/j" + str(i)}) for i in range(1, 4)]
result_lst = [f.result() for f in future_lst]
cache_lst = get_cache_data(cache_directory=cache_directory)
for entry in cache_lst:
self.assertTrue("same" in entry['filename'])
self.assertEqual(sum([c["output"] for c in cache_lst]), sum(result_lst))
self.assertEqual(
sum([sum(c["input_args"][0]) for c in cache_lst]), sum(result_lst)
)
def test_cache_duplicate_function(self):
cache_directory = os.path.abspath("cache_duplicate")
with SingleNodeExecutor(hostname_localhost=True, cache_directory=cache_directory) as exe:
f1 = exe.submit(sum_with_wait, 1, 1)
f2 = exe.submit(sum_with_wait, 1, 1)
self.assertEqual(f1.result(), f2.result())
def test_cache_error(self):
cache_directory = os.path.abspath("cache_error")
with SingleNodeExecutor(cache_directory=cache_directory) as exe:
self.assertTrue(exe)
cloudpickle_register(ind=1)
f = exe.submit(get_error, a=1)
with self.assertRaises(ValueError):
print(f.result())
def test_cache_error_file(self):
cache_directory = os.path.abspath("cache_error")
error_out = "error.out"
with SingleNodeExecutor(cache_directory=cache_directory) as exe:
self.assertTrue(exe)
cloudpickle_register(ind=1)
f = exe.submit(get_error, a=1, resource_dict={"error_log_file": error_out})
with self.assertRaises(ValueError):
print(f.result())
self.assertTrue(os.path.exists(error_out))
os.remove(error_out)
def test_cache_init_function_warning(self):
cache_directory = os.path.abspath("cache_init_function")
with self.assertWarns(UserWarning) as cm:
with SingleNodeExecutor(
max_workers=1,
block_allocation=True,
init_function=init_counter,
cache_directory=cache_directory,
) as exe:
cloudpickle_register(ind=1)
result_lst = [exe.submit(increment).result() for _ in range(3)]
self.assertIn("init_function", str(cm.warning))
# Documents the incompatibility from https://github.com/pyiron/executorlib/issues/1084
self.assertEqual(result_lst, [1, 1, 1])
def tearDown(self):
shutil.rmtree("executorlib_cache", ignore_errors=True)
shutil.rmtree("cache_error", ignore_errors=True)
shutil.rmtree("cache_duplicate", ignore_errors=True)
shutil.rmtree("cache_init_function", ignore_errors=True)