Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit663612c

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 parent28ac425 commit663612c

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
classPostgresNode(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
ifself.is_started:
778784
returnself
779785

@@ -789,13 +795,17 @@ def start(self, params=[], wait=True):
789795
nAttempt=0
790796
timeout=1
791797
whileTrue:
798+
assertnAttempt>=0
799+
assertnAttempt<__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
iferrorand'does not exist'inerror:
796804
raiseException
797805
exceptExceptionase:
798-
ifself._should_free_portandnAttempt<5:
806+
assertnAttempt>0
807+
assertnAttempt<=__class__._C_MAX_START_ATEMPTS
808+
ifself._should_free_portandnAttempt<__class__._C_MAX_START_ATEMPTS:
799809
log_files1=self._collect_log_files()
800810
ifself._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
withget_new_node(port=node.port)asnode2:
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
classtagPortManagerProxy:
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
deftest_port_rereserve_during_node_start(self):
1169+
asserttestgres.PostgresNode._C_MAX_START_ATEMPTS==5
1170+
11621171
C_COUNT_OF_BAD_PORT_USAGE=3
11631172

11641173
withget_new_node()asnode1:
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+
deftest_port_conflict(self):
1202+
asserttestgres.PostgresNode._C_MAX_START_ATEMPTS>1
1203+
1204+
C_COUNT_OF_BAD_PORT_USAGE=testgres.PostgresNode._C_MAX_START_ATEMPTS
1205+
1206+
withget_new_node()asnode1:
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+
withget_new_node()asnode2:
1216+
self.assertTrue(node2._should_free_port)
1217+
self.assertEqual(node2.port,node1.port)
1218+
1219+
withself.assertRaises(StartNodeException)asctx:
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
deftest_simple_with_bin_dir(self):
11851239
withget_new_node()asnode:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp