Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2512835

Browse files
[#264] New property PostrgsNode::port_manager (#269)
This patch adds the RO-property PostgresNode::port_manager. This property returns a used port manager or None. Declaration of PostgresNode::_port_manager was corrected. New tests for PostgresNode::port_manager and PostgresNode::os_ops are added.
1 parent a7662d1 commit 2512835

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

testgres/node.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class PostgresNode(object):
146146
_port: typing.Optional[int]
147147
_should_free_port: bool
148148
_os_ops: OsOperations
149-
_port_manager: PortManager
149+
_port_manager: typing.Optional[PortManager]
150150

151151
def __init__(self,
152152
name=None,
@@ -313,6 +313,11 @@ def os_ops(self) -> OsOperations:
313313
assert isinstance(self._os_ops, OsOperations)
314314
return self._os_ops
315315

316+
@property
317+
def port_manager(self) -> typing.Optional[PortManager]:
318+
assert self._port_manager is None or isinstance(self._port_manager, PortManager)
319+
return self._port_manager
320+
316321
@property
317322
def name(self) -> str:
318323
if self._name is None:

tests/test_testgres_common.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,88 @@ def test_try_to_start_node_after_free_manual_port(self, node_svc: PostgresNodeSe
15031503
):
15041504
node2.start()
15051505

1506+
def test_node__os_ops(self, node_svc: PostgresNodeService):
1507+
assert type(node_svc) == PostgresNodeService # noqa: E721
1508+
1509+
assert node_svc.os_ops is not None
1510+
assert isinstance(node_svc.os_ops, OsOperations)
1511+
1512+
with PostgresNode(name="node", os_ops=node_svc.os_ops, port_manager=node_svc.port_manager) as node:
1513+
# retest
1514+
assert node_svc.os_ops is not None
1515+
assert isinstance(node_svc.os_ops, OsOperations)
1516+
1517+
assert node.os_ops is node_svc.os_ops
1518+
# one more time
1519+
assert node.os_ops is node_svc.os_ops
1520+
1521+
def test_node__port_manager(self, node_svc: PostgresNodeService):
1522+
assert type(node_svc) == PostgresNodeService # noqa: E721
1523+
1524+
assert node_svc.port_manager is not None
1525+
assert isinstance(node_svc.port_manager, PortManager)
1526+
1527+
with PostgresNode(name="node", os_ops=node_svc.os_ops, port_manager=node_svc.port_manager) as node:
1528+
# retest
1529+
assert node_svc.port_manager is not None
1530+
assert isinstance(node_svc.port_manager, PortManager)
1531+
1532+
assert node.port_manager is node_svc.port_manager
1533+
# one more time
1534+
assert node.port_manager is node_svc.port_manager
1535+
1536+
def test_node__port_manager_and_explicit_port(self, node_svc: PostgresNodeService):
1537+
assert type(node_svc) == PostgresNodeService # noqa: E721
1538+
1539+
assert isinstance(node_svc.os_ops, OsOperations)
1540+
assert node_svc.port_manager is not None
1541+
assert isinstance(node_svc.port_manager, PortManager)
1542+
1543+
port = node_svc.port_manager.reserve_port()
1544+
assert type(port) == int # noqa: E721
1545+
1546+
try:
1547+
with PostgresNode(name="node", port=port, os_ops=node_svc.os_ops) as node:
1548+
# retest
1549+
assert isinstance(node_svc.os_ops, OsOperations)
1550+
assert node_svc.port_manager is not None
1551+
assert isinstance(node_svc.port_manager, PortManager)
1552+
1553+
assert node.port_manager is None
1554+
assert node.os_ops is node_svc.os_ops
1555+
1556+
# one more time
1557+
assert node.port_manager is None
1558+
assert node.os_ops is node_svc.os_ops
1559+
finally:
1560+
node_svc.port_manager.release_port(port)
1561+
1562+
def test_node__no_port_manager(self, node_svc: PostgresNodeService):
1563+
assert type(node_svc) == PostgresNodeService # noqa: E721
1564+
1565+
assert isinstance(node_svc.os_ops, OsOperations)
1566+
assert node_svc.port_manager is not None
1567+
assert isinstance(node_svc.port_manager, PortManager)
1568+
1569+
port = node_svc.port_manager.reserve_port()
1570+
assert type(port) == int # noqa: E721
1571+
1572+
try:
1573+
with PostgresNode(name="node", port=port, os_ops=node_svc.os_ops, port_manager=None) as node:
1574+
# retest
1575+
assert isinstance(node_svc.os_ops, OsOperations)
1576+
assert node_svc.port_manager is not None
1577+
assert isinstance(node_svc.port_manager, PortManager)
1578+
1579+
assert node.port_manager is None
1580+
assert node.os_ops is node_svc.os_ops
1581+
1582+
# one more time
1583+
assert node.port_manager is None
1584+
assert node.os_ops is node_svc.os_ops
1585+
finally:
1586+
node_svc.port_manager.release_port(port)
1587+
15061588
@staticmethod
15071589
def helper__get_node(
15081590
node_svc: PostgresNodeService,

0 commit comments

Comments
 (0)