8000 Tests for OsOps::is_port_free are added · postgrespro/testgres@94da63e · GitHub
[go: up one dir, main page]

Skip to content

Commit 94da63e

Browse files
Tests for OsOps::is_port_free are added
1 parent 322fb23 commit 94da63e

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

tests/test_os_ops_common.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import re
1111
import tempfile
1212
import logging
13+
import socket
14+
import threading
1315

1416
from ..testgres import InvalidOperationException
1517
from ..testgres import ExecUtilException
@@ -648,3 +650,100 @@ def test_touch(self, os_ops: OsOperations):
648650
assert os_ops.isfile(filename)
649651

650652
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

Comments
 (0)
0