Skip to content

PortManager #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
09dab31
PostgresNode refactoring [PostgresNodePortManager and RO-properties]
dmitry-lipetsk Apr 3, 2025
ef095d3
[FIX] clone_with_new_name_and_base_dir did not respect port_manager
dmitry-lipetsk Apr 4, 2025
0f842fd
PostgresNodePortManager__ThisHost is defined (was: PostgresNodePortMa…
dmitry-lipetsk Apr 4, 2025
e1e609e
PostgresNodePortManager__Generic is added
dmitry-lipetsk Apr 4, 2025
285e5b7
PostgresNodePortManager is added in public API
dmitry-lipetsk Apr 4, 2025
a974866
Test structures were refactored (local, local2, remote)
dmitry-lipetsk Apr 4, 2025
110947d
CI files are updated
dmitry-lipetsk Apr 4, 2025
4f49dde
TestTestgresCommon.test_pgbench is added
dmitry-lipetsk Apr 4, 2025
4fbf51d
PostgresNodePortManager is updated [error messages]
dmitry-lipetsk Apr 4, 2025
17c73cb
PostgresNodePortManager(+company) was moved in own file - port_manage…
dmitry-lipetsk Apr 4, 2025
b1cee19
PortManager was deleted [amen]
dmitry-lipetsk Apr 4, 2025
c5ad907
PostgresNodePortManager was renamed with PortManager
dmitry-lipetsk Apr 4, 2025
0967057
TestTestgresCommon.test_unix_sockets is added
dmitry-lipetsk Apr 4, 2025
1d450b2
TestTestgresCommon.test_the_same_port is added
dmitry-lipetsk Apr 4, 2025
4a38b35
[TestTestgresCommon] New tests are added
dmitry-lipetsk Apr 4, 2025
322fb23
RemoteOperations::is_port_free is updated
dmitry-lipetsk Apr 4, 2025
94da63e
Tests for OsOps::is_port_free are added
dmitry-lipetsk Apr 4, 2025
88f9b73
TestTestgresCommon is corrected [python problems]
dmitry-lipetsk Apr 4, 2025
d9558ce
The call of RaiseError.CommandExecutionError is fixed [message, not m…
dmitry-lipetsk Apr 4, 2025
0da4c21
[CI] ubuntu 24.04 does not have nc
dmitry-lipetsk Apr 4, 2025
0058508
RemoteOperations is update [private method names]
dmitry-lipetsk Apr 5, 2025
8f3a566
test_is_port_free__true is updated
dmitry-lipetsk Apr 5, 2025
d8ebdb7
RemoteOperations::is_port_free is updated (comments)
dmitry-lipetsk Apr 5, 2025
30e472c
setup.py is updated [testgres.helpers was deleted]
dmitry-lipetsk Apr 5, 2025
c94bbb5
Comment in node.py is updated
dmitry-lipetsk Apr 5, 2025
04f88c7
PostgresNode::_node was deleted [use self._os_ops.host]
dmitry-lipetsk Apr 5, 2025
0a3442a
PostgresNode::start is corrected [error message]
dmitry-lipetsk Apr 5, 2025
30124f3
Merge branch 'master' into D20250403_001--port_manager
dmitry-lipetsk Apr 5, 2025
13e71d8
[FIX] PostgresNode.__init__ must not test "os_ops.host" attribute.
dmitry-lipetsk Apr 5, 2025
9e14f4a
PostgresNode.free_port always set a port to None
dmitry-lipetsk Apr 6, 2025
739ef61
[FIX] flake8 (noqa: E721)
dmitry-lipetsk Apr 6, 2025
696cc1e
PortManager__Generic is refactored
dmitry-lipetsk Apr 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
PostgresNodePortManager was renamed with PortManager
  • Loading branch information
dmitry-lipetsk committed Apr 4, 2025
commit c5ad9078e98495ae904f23cba8b86a6c6e9ddceb
4 changes: 2 additions & 2 deletions testgres/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
DumpFormat

from .node import PostgresNode, NodeApp
from .node import PostgresNodePortManager
from .node import PortManager

from .utils import \
reserve_port, \
Expand Down Expand Up @@ -63,7 +63,7 @@
"TestgresException", "ExecUtilException", "QueryException", "TimeoutException", "CatchUpException", "StartNodeException", "InitNodeException", "BackupException", "InvalidOperationException",
"XLogMethod", "IsolationLevel", "NodeStatus", "ProcessType", "DumpFormat",
"PostgresNode", "NodeApp",
"PostgresNodePortManager",
"PortManager",
"reserve_port", "release_port", "bound_ports", "get_bin_path", "get_pg_config", "get_pg_version",
"First", "Any",
"OsOperations", "LocalOperations", "RemoteOperations", "ConnectionParams"
Expand Down
28 changes: 14 additions & 14 deletions testgres/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@
BackupException, \
InvalidOperationException

from .port_manager import PostgresNodePortManager
from .port_manager import PostgresNodePortManager__ThisHost
from .port_manager import PostgresNodePortManager__Generic
from .port_manager import PortManager
from .port_manager import PortManager__ThisHost
from .port_manager import PortManager__Generic

from .logger import TestgresLogger

Expand Down Expand Up @@ -146,7 +146,7 @@ class PostgresNode(object):
_port: typing.Optional[int]
_should_free_port: bool
_os_ops: OsOperations
_port_manager: PostgresNodePortManager
_port_manager: PortManager

def __init__(self,
name=None,
Expand All @@ -156,7 +156,7 @@ def __init__(self,
bin_dir=None,
prefix=None,
os_ops: typing.Optional[OsOperations] = None,
port_manager: typing.Optional[PostgresNodePortManager] = None):
port_manager: typing.Optional[PortManager] = None):
"""
PostgresNode constructor.

Expand All @@ -170,7 +170,7 @@ def __init__(self,
"""
assert port is None or type(port) == int # noqa: E721
assert os_ops is None or isinstance(os_ops, OsOperations)
assert port_manager is None or isinstance(port_manager, PostgresNodePortManager)
assert port_manager is None or isinstance(port_manager, PortManager)

# private
if os_ops is None:
Expand Down Expand Up @@ -203,13 +203,13 @@ def __init__(self,
self._port_manager = None
else:
if port_manager is not None:
assert isinstance(port_manager, PostgresNodePortManager)
assert isinstance(port_manager, PortManager)
self._port_manager = port_manager
else:
self._port_manager = __class__._get_port_manager(self._os_ops)

assert self._port_manager is not None
assert isinstance(self._port_manager, PostgresNodePortManager)
assert isinstance(self._port_manager, PortManager)

self._port = self._port_manager.reserve_port() # raises
assert type(self._port) == int # noqa: E721
Expand Down Expand Up @@ -269,15 +269,15 @@ def _get_os_ops(conn_params: ConnectionParams) -> OsOperations:
return LocalOperations(conn_params)

@staticmethod
def _get_port_manager(os_ops: OsOperations) -> PostgresNodePortManager:
def _get_port_manager(os_ops: OsOperations) -> PortManager:
assert os_ops is not None
assert isinstance(os_ops, OsOperations)

if isinstance(os_ops, LocalOperations):
return PostgresNodePortManager__ThisHost()
return PortManager__ThisHost()

# TODO: Throw exception "Please define a port manager."
return PostgresNodePortManager__Generic(os_ops)
return PortManager__Generic(os_ops)

def clone_with_new_name_and_base_dir(self, name: str, base_dir: str):
assert name is None or type(name) == str # noqa: E721
Expand All @@ -289,7 +289,7 @@ def clone_with_new_name_and_base_dir(self, name: str, base_dir: str):
raise InvalidOperationException("PostgresNode without PortManager can't be cloned.")

assert self._port_manager is not None
assert isinstance(self._port_manager, PostgresNodePortManager)
assert isinstance(self._port_manager, PortManager)
assert self._os_ops is not None
assert isinstance(self._os_ops, OsOperations)

Expand Down Expand Up @@ -1124,7 +1124,7 @@ def LOCAL__raise_cannot_start_node__std(from_exception):
else:
assert self._should_free_port
assert self._port_manager is not None
assert isinstance(self._port_manager, PostgresNodePortManager)
assert isinstance(self._port_manager, PortManager)
assert __class__._C_MAX_START_ATEMPTS > 1

log_files0 = self._collect_log_files()
Expand Down Expand Up @@ -1331,7 +1331,7 @@ def free_port(self):
assert type(self._port) == int # noqa: E721

assert self._port_manager is not None
assert isinstance(self._port_manager, PostgresNodePortManager)
assert isinstance(self._port_manager, PortManager)

port = self._port
self._should_free_port = False
Expand Down
16 changes: 8 additions & 8 deletions testgres/port_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@
import random


class PostgresNodePortManager:
class PortManager:
def __init__(self):
super().__init__()

def reserve_port(self) -> int:
raise NotImplementedError("PostgresNodePortManager::reserve_port is not implemented.")
raise NotImplementedError("PortManager::reserve_port is not implemented.")

def release_port(self, number: int) -> None:
assert type(number) == int # noqa: E721
raise NotImplementedError("PostgresNodePortManager::release_port is not implemented.")
raise NotImplementedError("PortManager::release_port is not implemented.")


class PostgresNodePortManager__ThisHost(PostgresNodePortManager):
sm_single_instance: PostgresNodePortManager = None
class PortManager__ThisHost(PortManager):
sm_single_instance: PortManager = None
sm_single_instance_guard = threading.Lock()

def __init__(self):
pass

def __new__(cls) -> PostgresNodePortManager:
assert __class__ == PostgresNodePortManager__ThisHost
def __new__(cls) -> PortManager:
assert __class__ == PortManager__ThisHost
assert __class__.sm_single_instance_guard is not None

if __class__.sm_single_instance is None:
Expand All @@ -46,7 +46,7 @@ def release_port(self, number: int) -> None:
return utils.release_port(number)


class PostgresNodePortManager__Generic(PostgresNodePortManager):
class PortManager__Generic(PortManager):
_os_ops: OsOperations
_allocated_ports_guard: object
_allocated_ports: set[int]
Expand Down
3 changes: 3 additions & 0 deletions testgres/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ def internal__release_port(port):
Free port provided by reserve_port().
"""

assert type(port) == int # noqa: E721
assert port in bound_ports

bound_ports.discard(port)


Expand Down
18 changes: 9 additions & 9 deletions tests/helpers/global_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from ...testgres.operations.local_ops import LocalOperations
from ...testgres.operations.remote_ops import RemoteOperations

from ...testgres.node import PostgresNodePortManager
from ...testgres.node import PostgresNodePortManager__ThisHost
from ...testgres.node import PostgresNodePortManager__Generic
from ...testgres.node import PortManager
from ...testgres.node import PortManager__ThisHost
from ...testgres.node import PortManager__Generic

import os

Expand Down Expand Up @@ -37,22 +37,22 @@ class OsOpsDescrs:


class PortManagers:
sm_remote_port_manager = PostgresNodePortManager__Generic(OsOpsDescrs.sm_remote_os_ops)
sm_remote_port_manager = PortManager__Generic(OsOpsDescrs.sm_remote_os_ops)

sm_local_port_manager = PostgresNodePortManager__ThisHost()
sm_local_port_manager = PortManager__ThisHost()

sm_local2_port_manager = PostgresNodePortManager__Generic(OsOpsDescrs.sm_local_os_ops)
sm_local2_port_manager = PortManager__Generic(OsOpsDescrs.sm_local_os_ops)


class PostgresNodeService:
sign: str
os_ops: OsOperations
port_manager: PostgresNodePortManager
port_manager: PortManager

def __init__(self, sign: str, os_ops: OsOperations, port_manager: PostgresNodePortManager):
def __init__(self, sign: str, os_ops: OsOperations, port_manager: PortManager):
assert type(sign) == str # noqa: E721
assert isinstance(os_ops, OsOperations)
assert isinstance(port_manager, PostgresNodePortManager)
assert isinstance(port_manager, PortManager)
self.sign = sign
self.os_ops = os_ops
self.port_manager = port_manager
Expand Down
6 changes: 3 additions & 3 deletions tests/test_testgres_common.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .helpers.global_data import PostgresNodeService
from .helpers.global_data import PostgresNodeServices
from .helpers.global_data import OsOperations
from .helpers.global_data import PostgresNodePortManager
from .helpers.global_data import PortManager

from ..testgres.node import PgVer
from ..testgres.node import PostgresNode
Expand Down Expand Up @@ -70,7 +70,7 @@ def node_svc(self, request: pytest.FixtureRequest) -> PostgresNodeService:
assert isinstance(request, pytest.FixtureRequest)
assert isinstance(request.param, PostgresNodeService)
assert isinstance(request.param.os_ops, OsOperations)
assert isinstance(request.param.port_manager, PostgresNodePortManager)
assert isinstance(request.param.port_manager, PortManager)
return request.param

def test_version_management(self, node_svc: PostgresNodeService):
Expand Down Expand Up @@ -1125,7 +1125,7 @@ def test_pgbench(self, node_svc: PostgresNodeService):
def helper__get_node(node_svc: PostgresNodeService, name=None):
assert isinstance(node_svc, PostgresNodeService)
assert isinstance(node_svc.os_ops, OsOperations)
assert isinstance(node_svc.port_manager, PostgresNodePortManager)
assert isinstance(node_svc.port_manager, PortManager)
return PostgresNode(
name,
conn_params=None,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_testgres_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def helper__get_node(name=None):

assert isinstance(svc, PostgresNodeService)
assert isinstance(svc.os_ops, testgres.OsOperations)
assert isinstance(svc.port_manager, testgres.PostgresNodePortManager)
assert isinstance(svc.port_manager, testgres.PortManager)

return testgres.PostgresNode(
name,
Expand Down