@@ -520,6 +520,104 @@ def test_token_aware_with_local_table(self):
520520
521521 cluster .shutdown ()
522522
523+ def test_token_aware_with_shuffle_rf2 (self ):
524+ """
525+ Test to validate the hosts are shuffled when the `shuffle_replicas` is truthy
526+ @since 3.8
527+ @jira_ticket PYTHON-676
528+ @expected_result the request are spread across the replicas,
529+ when one of them is down, the requests target the available one
530+
531+ @test_category policy
532+ """
533+ keyspace = 'test_token_aware_with_rf_2'
534+ cluster , session = self ._set_up_shuffle_test (keyspace , replication_factor = 2 )
535+
536+ self ._check_query_order_changes (session = session , keyspace = keyspace )
537+
538+ #check TokenAwarePolicy still return the remaining replicas when one goes down
539+ self .coordinator_stats .reset_counts ()
540+ stop (2 )
541+ self ._wait_for_nodes_down ([2 ], cluster )
542+
543+ self ._query (session , keyspace )
544+
545+ self .coordinator_stats .assert_query_count_equals (self , 1 , 0 )
546+ self .coordinator_stats .assert_query_count_equals (self , 2 , 0 )
547+ self .coordinator_stats .assert_query_count_equals (self , 3 , 12 )
548+
549+ cluster .shutdown ()
550+
551+ def test_token_aware_with_shuffle_rf3 (self ):
552+ """
553+ Test to validate the hosts are shuffled when the `shuffle_replicas` is truthy
554+ @since 3.8
555+ @jira_ticket PYTHON-676
556+ @expected_result the request are spread across the replicas,
557+ when one of them is down, the requests target the other available ones
558+
559+ @test_category policy
560+ """
561+ keyspace = 'test_token_aware_with_rf_3'
562+ cluster , session = self ._set_up_shuffle_test (keyspace , replication_factor = 3 )
563+
564+ self ._check_query_order_changes (session = session , keyspace = keyspace )
565+
566+ # check TokenAwarePolicy still return the remaining replicas when one goes down
567+ self .coordinator_stats .reset_counts ()
568+ stop (1 )
569+ self ._wait_for_nodes_down ([1 ], cluster )
570+
571+ self ._query (session , keyspace )
572+
573+ self .coordinator_stats .assert_query_count_equals (self , 1 , 0 )
574+ query_count_two = self .coordinator_stats .get_query_count (2 )
575+ query_count_three = self .coordinator_stats .get_query_count (3 )
576+ self .assertEqual (query_count_two + query_count_three , 12 )
577+
578+ self .coordinator_stats .reset_counts ()
579+ stop (2 )
580+ self ._wait_for_nodes_down ([2 ], cluster )
581+
582+ self ._query (session , keyspace )
583+
584+ self .coordinator_stats .assert_query_count_equals (self , 1 , 0 )
585+ self .coordinator_stats .assert_query_count_equals (self , 2 , 0 )
586+ self .coordinator_stats .assert_query_count_equals (self , 3 , 12 )
587+
588+ cluster .shutdown ()
589+
590+ def _set_up_shuffle_test (self , keyspace , replication_factor ):
591+ use_singledc ()
592+ cluster , session = self ._cluster_session_with_lbp (
593+ TokenAwarePolicy (RoundRobinPolicy (), shuffle_replicas = True )
594+ )
595+ self ._wait_for_nodes_up (range (1 , 4 ), cluster )
596+
597+ create_schema (cluster , session , keyspace , replication_factor = replication_factor )
598+ return cluster , session
599+
600+ def _check_query_order_changes (self , session , keyspace ):
601+ LIMIT_TRIES , tried , query_counts = 20 , 0 , set ()
602+
603+ while len (query_counts ) <= 1 :
604+ tried += 1
605+ if tried >= LIMIT_TRIES :
606+ raise Exception ("After {0} tries shuffle returned the same output" .format (LIMIT_TRIES ))
607+
608+ self ._insert (session , keyspace )
609+ self ._query (session , keyspace )
610+
611+ loop_qcs = (self .coordinator_stats .get_query_count (1 ),
612+ self .coordinator_stats .get_query_count (2 ),
613+ self .coordinator_stats .get_query_count (3 ))
614+
615+ query_counts .add (loop_qcs )
616+ self .assertEqual (sum (loop_qcs ), 12 )
617+
618+ # end the loop if we get more than one query ordering
619+ self .coordinator_stats .reset_counts ()
620+
523621 def test_white_list (self ):
524622 use_singledc ()
525623 keyspace = 'test_white_list'
0 commit comments