MCPcopy Create free account
hub / github.com/catboost/catboost / test_memoize_thread_safe

Function test_memoize_thread_safe

library/python/func/ut/test_func.py:290–322  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

288
289
290def test_memoize_thread_safe():
291 class Counter(object):
292 def __init__(self, s):
293 self.val = s
294
295 def inc(self):
296 self.val += 1
297 return self.val
298
299 @func.memoize(thread_safe=True)
300 def io_job(n):
301 time.sleep(0.05)
302 return Counter(n)
303
304 def worker(n):
305 assert io_job(n).inc() == n + 1
306 assert io_job(n).inc() == n + 2
307 assert io_job(n*10).inc() == n*10 + 1
308 assert io_job(n*10).inc() == n*10 + 2
309
310 threads = []
311 for i in range(5):
312 threads.append(threading.Thread(target=worker, args=(i+1,)))
313
314 st = time.time()
315
316 for thread in threads:
317 thread.start()
318 for thread in threads:
319 thread.join()
320
321 elapsed_time = time.time() - st
322 assert elapsed_time >= 0.5
323
324
325if __name__ == '__main__':

Callers

nothing calls this directly

Calls 4

rangeFunction · 0.50
appendMethod · 0.45
startMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected