-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
846c05f
commit 1a2f6da
Showing
12 changed files
with
207 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,16 +27,16 @@ | |
readme = f.read() | ||
|
||
setup( | ||
version='1.9.2', | ||
version='1.9.3', | ||
name='testgres', | ||
packages=['testgres', 'testgres.operations'], | ||
packages=['testgres', 'testgres.operations', 'testgres.helpers'], | ||
description='Testing utility for PostgreSQL and its extensions', | ||
url='https://github.com/postgrespro/testgres', | ||
long_description=readme, | ||
long_description_content_type='text/markdown', | ||
license='PostgreSQL', | ||
author='Ildar Musin', | ||
author_email='[email protected]', | ||
author='Postgres Professional', | ||
author_email='[email protected]', | ||
keywords=['test', 'testing', 'postgresql'], | ||
install_requires=install_requires, | ||
classifiers=[], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import socket | ||
import random | ||
from typing import Set, Iterable, Optional | ||
|
||
|
||
class PortForException(Exception): | ||
pass | ||
|
||
|
||
class PortManager: | ||
def __init__(self, ports_range=(1024, 65535)): | ||
self.ports_range = ports_range | ||
|
||
@staticmethod | ||
def is_port_free(port: int) -> bool: | ||
"""Check if a port is free to use.""" | ||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | ||
try: | ||
s.bind(("", port)) | ||
return True | ||
except OSError: | ||
return False | ||
|
||
def find_free_port(self, ports: Optional[Set[int]] = None, exclude_ports: Optional[Iterable[int]] = None) -> int: | ||
"""Return a random unused port number.""" | ||
if ports is None: | ||
ports = set(range(1024, 65535)) | ||
|
||
if exclude_ports is None: | ||
exclude_ports = set() | ||
|
||
ports.difference_update(set(exclude_ports)) | ||
|
||
sampled_ports = random.sample(tuple(ports), min(len(ports), 100)) | ||
|
||
for port in sampled_ports: | ||
if self.is_port_free(port): | ||
return port | ||
|
||
raise PortForException("Can't select a port") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.