forked from appium/python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_helper.py
39 lines (26 loc) · 898 Bytes
/
test_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import os
import socket
class NoAvailablePortError(Exception):
pass
def get_available_from_port_range(from_port: int, to_port: int) -> int:
"""Returns available local port number.
Args:
from_port: The start port to search
to_port: The end port to search
Returns:
int: available local port number which are found first
"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
for port in range(from_port, to_port):
try:
if sock.connect_ex(('localhost', port)) != 0:
return port
finally:
sock.close()
raise NoAvailablePortError(f'No available port between {from_port} and {to_port}')
def is_ci() -> bool:
"""Returns if current execution is running on CI
Returns:
`True` if current executions is on CI
"""
return os.getenv('CI', 'false') == 'true'