Some tests
()
| 651 | |
| 652 | |
| 653 | def _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() |
no test coverage detected