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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
import pytest
import multiprocessing
import random
import threading
import time
import six
import library.python.func as func
def test_map0():
assert None is func.map0(lambda x: x + 1, None)
assert 3 == func.map0(lambda x: x + 1, 2)
assert None is func.map0(len, None)
assert 2 == func.map0(len, [1, 2])
def test_single():
assert 1 == func.single([1])
with pytest.raises(Exception):
assert 1 == func.single([])
with pytest.raises(Exception):
assert 1 == func.single([1, 2])
def test_memoize():
class Counter(object):
@staticmethod
def inc():
Counter._qty = getattr(Counter, '_qty', 0) + 1
return Counter._qty
@func.memoize()
def t1(a):
return a, Counter.inc()
@func.memoize()
def t2(a):
return a, Counter.inc()
@func.memoize()
def t3(a):
return a, Counter.inc()
@func.memoize()
def t4(a):
return a, Counter.inc()
@func.memoize()
def t5(a, b, c):
return a + b + c, Counter.inc()
@func.memoize()
def t6():
return Counter.inc()
@func.memoize(limit=2)
def t7(a, _b):
return a, Counter.inc()
assert (1, 1) == t1(1)
assert (1, 1) == t1(1)
assert (2, 2) == t1(2)
assert (2, 2) == t1(2)
assert (1, 3) == t2(1)
assert (1, 3) == t2(1)
assert (2, 4) == t2(2)
assert (2, 4) == t2(2)
assert (1, 5) == t3(1)
assert (1, 5) == t3(1)
assert (2, 6) == t3(2)
assert (2, 6) == t3(2)
assert (1, 7) == t4(1)
assert (1, 7) == t4(1)
assert (2, 8) == t4(2)
assert (2, 8) == t4(2)
assert (6, 9) == t5(1, 2, 3)
assert (6, 9) == t5(1, 2, 3)
assert (7, 10) == t5(1, 2, 4)
assert (7, 10) == t5(1, 2, 4)
assert 11 == t6()
assert 11 == t6()
assert (1, 12) == t7(1, None)
assert (2, 13) == t7(2, None)
assert (1, 12) == t7(1, None)
assert (2, 13) == t7(2, None)
# removed result for (1, None)
assert (3, 14) == t7(3, None)
assert (1, 15) == t7(1, None)
class ClassWithMemoizedMethod(object):
def __init__(self):
self.a = 0
@func.memoize(True)
def t(self, i):
self.a += i
return i
obj = ClassWithMemoizedMethod()
assert 10 == obj.t(10)
assert 10 == obj.a
assert 10 == obj.t(10)
assert 10 == obj.a
assert 20 == obj.t(20)
assert 30 == obj.a
assert 20 == obj.t(20)
assert 30 == obj.a
def test_first():
assert func.first([0, [], (), None, False, {}, 0.0, '1', 0]) == '1'
assert func.first([]) is None
assert func.first([0]) is None
def test_split():
assert func.split([1, 1], lambda x: x) == ([1, 1], [])
assert func.split([0, 0], lambda x: x) == ([], [0, 0])
assert func.split([], lambda x: x) == ([], [])
assert func.split([1, 0, 1], lambda x: x) == ([1, 1], [0])
def test_flatten_dict():
assert func.flatten_dict({"a": 1, "b": 2}) == {"a": 1, "b": 2}
assert func.flatten_dict({"a": 1}) == {"a": 1}
assert func.flatten_dict({}) == {}
assert func.flatten_dict({"a": 1, "b": {"c": {"d": 2}}}) == {"a": 1, "b.c.d": 2}
assert func.flatten_dict({"a": 1, "b": {"c": {"d": 2}}}, separator="/") == {"a": 1, "b/c/d": 2}
def test_threadsafe_singleton():
class ShouldBeSingle(six.with_metaclass(func.Singleton, object)):
def __new__(cls, *args, **kwargs):
time.sleep(0.1)
return super(ShouldBeSingle, cls).__new__(cls, *args, **kwargs)
threads_count = 100
threads = [None] * threads_count
results = [None] * threads_count
def class_factory(results, i):
time.sleep(0.1)
results[i] = ShouldBeSingle()
for i in range(threads_count):
threads[i] = threading.Thread(target=class_factory, args=(results, i))
for i in range(threads_count):
threads[i].start()
for i in range(threads_count):
threads[i].join()
assert len(set(results)) == 1
def test_memoize_thread_local():
class Counter(object):
def __init__(self, s):
self.val = s
def inc(self):
self.val += 1
return self.val
@func.memoize(thread_local=True)
def get_counter(start):
return Counter(start)
def th_inc():
assert get_counter(0).inc() == 1
assert get_counter(0).inc() == 2
assert get_counter(10).inc() == 11
assert get_counter(10).inc() == 12
th_inc()
th = threading.Thread(target=th_inc)
th.start()
th.join()
def test_memoize_not_thread_safe():
class Counter(object):
def __init__(self, s):
self.val = s
def inc(self):
self.val += 1
return self.val
@func.memoize(thread_safe=False)
def io_job(n):
time.sleep(0.1)
return Counter(n)
def worker(n):
assert io_job(n).inc() == n + 1
assert io_job(n).inc() == n + 2
assert io_job(n*10).inc() == n*10 + 1
assert io_job(n*10).inc() == n*10 + 2
assert io_job(n).inc() == n + 3
threads = []
for i in range(5):
threads.append(threading.Thread(target=worker, args=(i+1,)))
st = time.time()
for thread in threads:
thread.start()
for thread in threads:
thread.join()
elapsed_time = time.time() - st
assert elapsed_time < 0.5
def test_memoize_not_thread_safe_concurrent():
class Counter(object):
def __init__(self, s):
self.val = s
def inc(self):
self.val += 1
return self.val
@func.memoize(thread_safe=False)
def io_job(n):
time.sleep(0.1)
return Counter(n)
def worker():
io_job(100).inc()
th1 = threading.Thread(target=worker)
th2 = threading.Thread(target=worker)
th3 = threading.Thread(target=worker)
th1.start()
time.sleep(0.05)
th2.start()
th1.join()
assert io_job(100).inc() == 100 + 2
th3.start()
# th3 instantly got counter from memory
assert io_job(100).inc() == 100 + 4
th2.join()
# th2 shoud increase th1 counter
assert io_job(100).inc() == 100 + 6
def test_memoize_not_thread_safe_stress():
@func.memoize(thread_safe=False)
def job():
for _ in range(1000):
hash = random.getrandbits(128)
return hash
def worker(n):
hash = job()
results[n] = hash
num_threads = min(multiprocessing.cpu_count()*4, 64)
threads = []
results = [None for _ in range(num_threads)]
for i in range(num_threads):
thread = threading.Thread(target=worker, args=(i,))
threads.append(thread)
for thread in threads:
thread.start()
for thread in threads:
thread.join()
assert len(set(results)) == 1
def test_memoize_thread_safe():
class Counter(object):
def __init__(self, s):
self.val = s
def inc(self):
self.val += 1
return self.val
@func.memoize(thread_safe=True)
def io_job(n):
time.sleep(0.05)
return Counter(n)
def worker(n):
assert io_job(n).inc() == n + 1
assert io_job(n).inc() == n + 2
assert io_job(n*10).inc() == n*10 + 1
assert io_job(n*10).inc() == n*10 + 2
threads = []
for i in range(5):
threads.append(threading.Thread(target=worker, args=(i+1,)))
st = time.time()
for thread in threads:
thread.start()
for thread in threads:
thread.join()
elapsed_time = time.time() - st
assert elapsed_time >= 0.5
if __name__ == '__main__':
pytest.main([__file__])
|