|
10 | 10 | importre
|
11 | 11 | importtempfile
|
12 | 12 | importlogging
|
| 13 | +importsocket |
| 14 | +importthreading |
13 | 15 |
|
14 | 16 | from ..testgresimportInvalidOperationException
|
15 | 17 | from ..testgresimportExecUtilException
|
@@ -648,3 +650,100 @@ def test_touch(self, os_ops: OsOperations):
|
648 | 650 | assertos_ops.isfile(filename)
|
649 | 651 |
|
650 | 652 | os_ops.remove_file(filename)
|
| 653 | + |
| 654 | +deftest_is_port_free__true(self,os_ops:OsOperations): |
| 655 | +assertisinstance(os_ops,OsOperations) |
| 656 | + |
| 657 | +C_LIMIT=10 |
| 658 | + |
| 659 | +ports=set(range(1024,65535)) |
| 660 | +asserttype(ports)==set# noqa: E721 |
| 661 | + |
| 662 | +ok_count=0 |
| 663 | +no_count=0 |
| 664 | + |
| 665 | +forportinports: |
| 666 | +withsocket.socket(socket.AF_INET,socket.SOCK_STREAM)ass: |
| 667 | +try: |
| 668 | +s.bind(("",port)) |
| 669 | +exceptOSError: |
| 670 | +continue |
| 671 | + |
| 672 | +r=os_ops.is_port_free(port) |
| 673 | + |
| 674 | +ifr: |
| 675 | +ok_count+=1 |
| 676 | +logging.info("OK. Port {} is free.".format(port)) |
| 677 | +else: |
| 678 | +no_count+=1 |
| 679 | +logging.warning("NO. Port {} is not free.".format(port)) |
| 680 | + |
| 681 | +ifok_count==C_LIMIT: |
| 682 | +return |
| 683 | + |
| 684 | +ifno_count==C_LIMIT: |
| 685 | +raiseRuntimeError("To many false positive test attempts.") |
| 686 | + |
| 687 | +ifok_count==0: |
| 688 | +raiseRuntimeError("No one free port was found.") |
| 689 | + |
| 690 | +deftest_is_port_free__false(self,os_ops:OsOperations): |
| 691 | +assertisinstance(os_ops,OsOperations) |
| 692 | + |
| 693 | +C_LIMIT=10 |
| 694 | + |
| 695 | +ports=set(range(1024,65535)) |
| 696 | +asserttype(ports)==set# noqa: E721 |
| 697 | + |
| 698 | +defLOCAL_server(s:socket.socket): |
| 699 | +assertsisnotNone |
| 700 | +asserttype(s)==socket.socket# noqa: E721 |
| 701 | + |
| 702 | +try: |
| 703 | +whileTrue: |
| 704 | +r=s.accept() |
| 705 | + |
| 706 | +ifrisNone: |
| 707 | +break |
| 708 | +exceptExceptionase: |
| 709 | +asserteisnotNone |
| 710 | +pass |
| 711 | + |
| 712 | +ok_count=0 |
| 713 | +no_count=0 |
| 714 | + |
| 715 | +forportinports: |
| 716 | +withsocket.socket(socket.AF_INET,socket.SOCK_STREAM)ass: |
| 717 | +try: |
| 718 | +s.bind(("",port)) |
| 719 | +exceptOSError: |
| 720 | +continue |
| 721 | + |
| 722 | +th=threading.Thread(target=LOCAL_server,args=[s]) |
| 723 | + |
| 724 | +s.listen(10) |
| 725 | + |
| 726 | +asserttype(th)==threading.Thread# noqa: E721 |
| 727 | +th.start() |
| 728 | + |
| 729 | +try: |
| 730 | +r=os_ops.is_port_free(port) |
| 731 | +finally: |
| 732 | +s.shutdown(2) |
| 733 | +th.join() |
| 734 | + |
| 735 | +ifnotr: |
| 736 | +ok_count+=1 |
| 737 | +logging.info("OK. Port {} is not free.".format(port)) |
| 738 | +else: |
| 739 | +no_count+=1 |
| 740 | +logging.warning("NO. Port {} does not accept connection.".format(port)) |
| 741 | + |
| 742 | +ifok_count==C_LIMIT: |
| 743 | +return |
| 744 | + |
| 745 | +ifno_count==C_LIMIT: |
| 746 | +raiseRuntimeError("To many false positive test attempts.") |
| 747 | + |
| 748 | +ifok_count==0: |
| 749 | +raiseRuntimeError("No one free port was found.") |