3838from aws_advanced_python_wrapper .wrapper import AwsWrapperConnection
3939from .utils .driver_helper import DriverHelper
4040from .utils .rds_test_utility import RdsTestUtility
41+ from .utils .retry_helper import retry_until
4142from .utils .test_environment import TestEnvironment
4243from .utils .test_environment_features import TestEnvironmentFeatures
4344
@@ -116,7 +117,8 @@ def test_fail_from_writer_to_new_writer_fail_on_connection_invocation(
116117
117118 # assert that we are connected to the new writer after failover happens.
118119 current_connection_id = aurora_utility .query_instance_id (aws_conn )
119- assert aurora_utility .is_db_instance_writer (current_connection_id ) is True
120+ # RDS API lags behind the writer election, so we retry the check.
121+ assert retry_until (lambda : aurora_utility .is_db_instance_writer (current_connection_id ))
120122 assert current_connection_id != initial_writer_id
121123
122124 @pytest .mark .parametrize ("plugins" , ["failover" , "failover_v2" ])
@@ -137,7 +139,8 @@ def test_fail_from_writer_to_new_writer_fail_on_connection_bound_object_invocati
137139
138140 # assert that we are connected to the new writer after failover happens and we can reuse the cursor
139141 current_connection_id = aurora_utility .query_instance_id (aws_conn )
140- assert aurora_utility .is_db_instance_writer (current_connection_id ) is True
142+ # RDS API lags behind the writer election, so we retry the check.
143+ assert retry_until (lambda : aurora_utility .is_db_instance_writer (current_connection_id ))
141144 assert current_connection_id != initial_writer_id
142145
143146 @pytest .mark .parametrize ("plugins" , ["failover,host_monitoring" , "failover,host_monitoring_v2" ,
@@ -166,7 +169,8 @@ def test_fail_from_reader_to_writer(
166169 current_connection_id = aurora_utility .query_instance_id (aws_conn )
167170
168171 assert writer_id == current_connection_id
169- assert aurora_utility .is_db_instance_writer (current_connection_id ) is True
172+ # RDS API lags behind the writer election, so we retry the check.
173+ assert retry_until (lambda : aurora_utility .is_db_instance_writer (current_connection_id ))
170174
171175 @pytest .mark .parametrize ("plugins" , ["failover" , "failover_v2" ])
172176 @enable_on_features ([TestEnvironmentFeatures .FAILOVER_SUPPORTED ])
@@ -195,7 +199,8 @@ def test_fail_from_writer_with_session_states_autocommit(self, test_driver: Test
195199 # Attempt to query the instance id.
196200 current_connection_id = aurora_utility .query_instance_id (conn )
197201 # Assert that we are connected to the new writer after failover happens.
198- assert aurora_utility .is_db_instance_writer (current_connection_id ) is True
202+ # RDS API lags behind the writer election, so we retry the check.
203+ assert retry_until (lambda : aurora_utility .is_db_instance_writer (current_connection_id ))
199204 next_cluster_writer_id = aurora_utility .get_cluster_writer_instance_id ()
200205 assert current_connection_id == next_cluster_writer_id
201206 assert current_connection_id != initial_writer_id
@@ -229,7 +234,8 @@ def test_fail_from_writer_with_session_states_readonly(self, test_driver: TestDr
229234 # Attempt to query the instance id.
230235 current_connection_id = aurora_utility .query_instance_id (conn )
231236 # Assert that we are connected to the new writer after failover happens.
232- assert aurora_utility .is_db_instance_writer (current_connection_id ) is True
237+ # RDS API lags behind the writer election, so we retry the check.
238+ assert retry_until (lambda : aurora_utility .is_db_instance_writer (current_connection_id ))
233239 next_cluster_writer_id = aurora_utility .get_cluster_writer_instance_id ()
234240 assert current_connection_id == next_cluster_writer_id
235241 assert current_connection_id != initial_writer_id
@@ -265,7 +271,8 @@ def test_writer_fail_within_transaction_set_autocommit_false(
265271 current_connection_id : str = aurora_utility .query_instance_id (conn )
266272
267273 # assert that we are connected to the new writer after failover happens
268- assert aurora_utility .is_db_instance_writer (current_connection_id )
274+ # RDS API lags behind the writer election, so we retry the check.
275+ assert retry_until (lambda : aurora_utility .is_db_instance_writer (current_connection_id ))
269276 next_cluster_writer_id : str = aurora_utility .get_cluster_writer_instance_id ()
270277
271278 assert current_connection_id == next_cluster_writer_id
@@ -311,7 +318,8 @@ def test_writer_fail_within_transaction_start_transaction(
311318 current_connection_id : str = aurora_utility .query_instance_id (conn )
312319
313320 # assert that we are connected to the new writer after failover happens
314- assert aurora_utility .is_db_instance_writer (current_connection_id )
321+ # RDS API lags behind the writer election, so we retry the check.
322+ assert retry_until (lambda : aurora_utility .is_db_instance_writer (current_connection_id ))
315323 next_cluster_writer_id : str = aurora_utility .get_cluster_writer_instance_id ()
316324
317325 assert current_connection_id == next_cluster_writer_id
0 commit comments