Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit94da63e

Browse files
Tests for OsOps::is_port_free are added
1 parent322fb23 commit94da63e

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
importre
1111
importtempfile
1212
importlogging
13+
importsocket
14+
importthreading
1315

1416
from ..testgresimportInvalidOperationException
1517
from ..testgresimportExecUtilException
@@ -648,3 +650,100 @@ def test_touch(self, os_ops: OsOperations):
648650
assertos_ops.isfile(filename)
649651

650652
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.")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp