Skip to content

Commit 663612c

Browse files
PostgresNode._C_MAX_START_ATEMPTS=5 is added (+ 1 new test)
Also - TestgresTests.test_the_same_port is updated - TestgresTests.test_port_rereserve_during_node_start is updated - TestgresTests.test_port_conflict is added
1 parent 28ac425 commit 663612c

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

testgres/node.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ def __repr__(self):
128128

129129

130130
class PostgresNode(object):
131+
# a max number of node start attempts
132+
_C_MAX_START_ATEMPTS = 5
133+
131134
def __init__(self, name=None, base_dir=None, port=None, conn_params: ConnectionParams = ConnectionParams(), bin_dir=None, prefix=None):
132135
"""
133136
PostgresNode constructor.
@@ -774,6 +777,9 @@ def start(self, params=[], wait=True):
774777
Returns:
775778
This instance of :class:`.PostgresNode`.
776779
"""
780+
781+
assert __class__._C_MAX_START_ATEMPTS > 1
782+
777783
if self.is_started:
778784
return self
779785

@@ -789,13 +795,17 @@ def start(self, params=[], wait=True):
789795
nAttempt = 0
790796
timeout = 1
791797
while True:
798+
assert nAttempt >= 0
799+
assert nAttempt < __class__._C_MAX_START_ATEMPTS
792800
nAttempt += 1
793801
try:
794802
exit_status, out, error = execute_utility(_params, self.utils_log_file, verbose=True)
795803
if error and 'does not exist' in error:
796804
raise Exception
797805
except Exception as e:
798-
if self._should_free_port and nAttempt < 5:
806+
assert nAttempt > 0
807+
assert nAttempt <= __class__._C_MAX_START_ATEMPTS
808+
if self._should_free_port and nAttempt < __class__._C_MAX_START_ATEMPTS:
799809
log_files1 = self._collect_log_files()
800810
if self._detect_port_conflict(log_files0, log_files1):
801811
log_files0 = log_files1
@@ -806,7 +816,7 @@ def start(self, params=[], wait=True):
806816
time.sleep(timeout)
807817
timeout = min(2 * timeout, 5)
808818
cur_port = self.port
809-
new_port = utils.reserve_port() # throw
819+
new_port = utils.reserve_port() # can raise
810820
try:
811821
options = {'port': str(new_port)}
812822
self.set_auto_conf(options)

tests/test_simple.py

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,8 @@ def test_the_same_port(self):
10531053
node.init().start()
10541054
self.assertTrue(node._should_free_port)
10551055
self.assertEqual(type(node.port), int)
1056+
node_port_copy = node.port
1057+
self.assertEqual(node.safe_psql("SELECT 1;"), b'1\n')
10561058

10571059
with get_new_node(port=node.port) as node2:
10581060
self.assertEqual(type(node2.port), int)
@@ -1064,6 +1066,11 @@ def test_the_same_port(self):
10641066

10651067
self.assertIn("Cannot start node", str(ctx.exception))
10661068

1069+
# node is still working
1070+
self.assertEqual(node.port, node_port_copy)
1071+
self.assertTrue(node._should_free_port)
1072+
self.assertEqual(node.safe_psql("SELECT 3;"), b'3\n')
1073+
10671074
class tagPortManagerProxy:
10681075
sm_prev_testgres_reserve_port = None
10691076
sm_prev_testgres_release_port = None
@@ -1159,13 +1166,16 @@ def _proxy__release_port(dummyPortNumber):
11591166
return __class__.sm_prev_testgres_release_port(dummyPortNumber)
11601167

11611168
def test_port_rereserve_during_node_start(self):
1169+
assert testgres.PostgresNode._C_MAX_START_ATEMPTS == 5
1170+
11621171
C_COUNT_OF_BAD_PORT_USAGE = 3
11631172

11641173
with get_new_node() as node1:
11651174
node1.init().start()
11661175
self.assertTrue(node1._should_free_port)
11671176
self.assertEqual(type(node1.port), int) # noqa: E721
1168-
node1.safe_psql("SELECT 1;")
1177+
node1_port_copy = node1.port
1178+
self.assertEqual(node1.safe_psql("SELECT 1;"), b'1\n')
11691179

11701180
with __class__.tagPortManagerProxy(node1.port, C_COUNT_OF_BAD_PORT_USAGE):
11711181
assert __class__.tagPortManagerProxy.sm_DummyPortNumber == node1.port
@@ -1176,10 +1186,54 @@ def test_port_rereserve_during_node_start(self):
11761186
node2.init().start()
11771187

11781188
self.assertNotEqual(node2.port, node1.port)
1189+
self.assertTrue(node2._should_free_port)
11791190
self.assertEqual(__class__.tagPortManagerProxy.sm_DummyPortCurrentUsage, 0)
11801191
self.assertEqual(__class__.tagPortManagerProxy.sm_DummyPortTotalUsage, C_COUNT_OF_BAD_PORT_USAGE)
1192+
self.assertTrue(node2.is_started)
1193+
1194+
self.assertEqual(node2.safe_psql("SELECT 2;"), b'2\n')
1195+
1196+
# node1 is still working
1197+
self.assertEqual(node1.port, node1_port_copy)
1198+
self.assertTrue(node1._should_free_port)
1199+
self.assertEqual(node1.safe_psql("SELECT 3;"), b'3\n')
1200+
1201+
def test_port_conflict(self):
1202+
assert testgres.PostgresNode._C_MAX_START_ATEMPTS > 1
1203+
1204+
C_COUNT_OF_BAD_PORT_USAGE = testgres.PostgresNode._C_MAX_START_ATEMPTS
1205+
1206+
with get_new_node() as node1:
1207+
node1.init().start()
1208+
self.assertTrue(node1._should_free_port)
1209+
self.assertEqual(type(node1.port), int) # noqa: E721
1210+
node1_port_copy = node1.port
1211+
self.assertEqual(node1.safe_psql("SELECT 1;"), b'1\n')
1212+
1213+
with __class__.tagPortManagerProxy(node1.port, C_COUNT_OF_BAD_PORT_USAGE):
1214+
assert __class__.tagPortManagerProxy.sm_DummyPortNumber == node1.port
1215+
with get_new_node() as node2:
1216+
self.assertTrue(node2._should_free_port)
1217+
self.assertEqual(node2.port, node1.port)
1218+
1219+
with self.assertRaises(StartNodeException) as ctx:
1220+
node2.init().start()
1221+
1222+
self.assertIn("Cannot start node", str(ctx.exception))
1223+
1224+
self.assertEqual(node2.port, node1.port)
1225+
self.assertTrue(node2._should_free_port)
1226+
self.assertEqual(__class__.tagPortManagerProxy.sm_DummyPortCurrentUsage, 1)
1227+
self.assertEqual(__class__.tagPortManagerProxy.sm_DummyPortTotalUsage, C_COUNT_OF_BAD_PORT_USAGE)
1228+
self.assertFalse(node2.is_started)
1229+
1230+
# node2 must release our dummyPort (node1.port)
1231+
self.assertEqual(__class__.tagPortManagerProxy.sm_DummyPortCurrentUsage, 0)
11811232

1182-
node2.safe_psql("SELECT 1;")
1233+
# node1 is still working
1234+
self.assertEqual(node1.port, node1_port_copy)
1235+
self.assertTrue(node1._should_free_port)
1236+
self.assertEqual(node1.safe_psql("SELECT 3;"), b'3\n')
11831237

11841238
def test_simple_with_bin_dir(self):
11851239
with get_new_node() as node:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy