|
10 | 10 | import re
|
11 | 11 | import tempfile
|
12 | 12 | import logging
|
| 13 | +import socket |
| 14 | +import threading |
13 | 15 |
|
14 | 16 | from ..testgres import InvalidOperationException
|
15 | 17 | from ..testgres import ExecUtilException
|
@@ -648,3 +650,100 @@ def test_touch(self, os_ops: OsOperations):
|
648 | 650 | assert os_ops.isfile(filename)
|
649 | 651 |
|
650 | 652 | os_ops.remove_file(filename)
|
| 653 | + |
| 654 | + def test_is_port_free__true(self, os_ops: OsOperations): |
| 655 | + assert isinstance(os_ops, OsOperations) |
| 656 | + |
| 657 | + C_LIMIT = 10 |
| 658 | + |
| 659 | + ports = set(range(1024, 65535)) |
| 660 | + assert type(ports) == set # noqa: E721 |
| 661 | + |
| 662 | + ok_count = 0 |
| 663 | + no_count = 0 |
| 664 | + |
| 665 | + for port in ports: |
| 666 | + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: |
| 667 | + try: |
| 668 | + s.bind(("", port)) |
| 669 | + except OSError: |
| 670 | + continue |
| 671 | + |
| 672 | + r = os_ops.is_port_free(port) |
| 673 | + |
| 674 | + if r: |
| 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 | + if ok_count == C_LIMIT: |
| 682 | + return |
| 683 | + |
| 684 | + if no_count == C_LIMIT: |
| 685 | + raise RuntimeError("To many false positive test attempts.") |
| 686 | + |
| 687 | + if ok_count == 0: |
| 688 | + raise RuntimeError("No one free port was found.") |
| 689 | + |
| 690 | + def test_is_port_free__false(self, os_ops: OsOperations): |
| 691 | + assert isinstance(os_ops, OsOperations) |
| 692 | + |
| 693 | + C_LIMIT = 10 |
| 694 | + |
| 695 | + ports = set(range(1024, 65535)) |
| 696 | + assert type(ports) == set # noqa: E721 |
| 697 | + |
| 698 | + def LOCAL_server(s: socket.socket): |
| 699 | + assert s is not None |
| 700 | + assert type(s) == socket.socket # noqa: E721 |
| 701 | + |
| 702 | + try: |
| 703 | + while True: |
| 704 | + r = s.accept() |
| 705 | + |
| 706 | + if r is None: |
| 707 | + break |
| 708 | + except Exception as e: |
| 709 | + assert e is not None |
| 710 | + pass |
| 711 | + |
| 712 | + ok_count = 0 |
| 713 | + no_count = 0 |
| 714 | + |
| 715 | + for port in ports: |
| 716 | + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: |
| 717 | + try: |
| 718 | + s.bind(("", port)) |
| 719 | + except OSError: |
| 720 | + continue |
| 721 | + |
| 722 | + th = threading.Thread(target=LOCAL_server, args=[s]) |
| 723 | + |
| 724 | + s.listen(10) |
| 725 | + |
| 726 | + assert type(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 | + if not r: |
| 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 | + if ok_count == C_LIMIT: |
| 743 | + return |
| 744 | + |
| 745 | + if no_count == C_LIMIT: |
| 746 | + raise RuntimeError("To many false positive test attempts.") |
| 747 | + |
| 748 | + if ok_count == 0: |
| 749 | + raise RuntimeError("No one free port was found.") |
0 commit comments