MCPcopy Create free account
hub / github.com/ActiveState/code / _test

Function _test

recipes/Python/576519_Thread_pool_same_API/recipe-576519.py:653–784  ·  view source on GitHub ↗

Some tests

()

Source from the content-addressed store, hash-verified

651
652
653def _test():
654 """Some tests"""
655 import thread, time
656
657 def return42():
658 return 42
659
660 def f(x):
661 return x*x
662
663 def work(seconds):
664 print "[%d] Start to work for %fs..." % (thread.get_ident(), seconds)
665 time.sleep(seconds)
666 print "[%d] Work done (%fs)." % (thread.get_ident(), seconds)
667 return str(seconds)
668
669 ### Test copy/pasted from multiprocessing
670 pool = Pool(9) # start 4 worker threads
671
672 # edge cases
673 assert pool.apply_async(return42, []).get() == 42
674 assert pool.apply(return42, []) == 42
675 assert pool.map(return42, []) == []
676 assert list(pool.imap(return42, iter([]))) == []
677 assert list(pool.imap_unordered(return42, iter([]))) == []
678 assert pool.map_async(return42, []).get() == []
679 assert list(pool.imap_async(return42, iter([])).get()) == []
680 assert list(pool.imap_unordered_async(return42, iter([])).get()) == []
681
682 # basic tests
683 result = pool.apply_async(f, (10,)) # evaluate "f(10)" asynchronously
684 assert result.get(timeout=1) == 100 # ... unless slow computer
685
686 assert pool.map(f, range(10)) == map(f, range(10))
687
688 it = pool.imap(f, range(10))
689 assert it.next() == 0
690 assert it.next() == 1
691 assert it.next(timeout=1) == 4 # ... unless slow computer
692
693 # Test apply_sync exceptions
694 result = pool.apply_async(time.sleep, (3,))
695 try:
696 print result.get(timeout=1) # raises `TimeoutError`
697 except TimeoutError:
698 print "Good. Got expected timeout exception."
699 else:
700 assert False, "Expected exception !"
701 assert result.get() == None # sleep() returns None
702
703 def cb(s):
704 print "Result ready: %s" % s
705
706 # Test imap()
707 assert list(pool.imap(work, xrange(10, 3, -1), chunksize=4)) == map(
708 str, range(10, 3, -1))
709
710 # Test imap_unordered()

Callers 1

recipe-576519.pyFile · 0.70

Calls 15

apply_asyncMethod · 0.95
applyMethod · 0.95
mapMethod · 0.95
imapMethod · 0.95
imap_unorderedMethod · 0.95
map_asyncMethod · 0.95
imap_asyncMethod · 0.95
imap_unordered_asyncMethod · 0.95
terminateMethod · 0.95
listClass · 0.85
rangeFunction · 0.85
mapFunction · 0.85

Tested by

no test coverage detected