@@ -769,3 +769,81 @@ async fn test_monitor_health_state_race() {
769769
770770 replicas. shutdown ( ) ;
771771}
772+
773+ #[ tokio:: test]
774+ async fn test_include_primary_if_replica_banned_no_bans ( ) {
775+ let primary_config = create_test_pool_config ( "127.0.0.1" , 5432 ) ;
776+ let primary_pool = Pool :: new ( & primary_config) ;
777+ primary_pool. launch ( ) ;
778+
779+ let replica_configs = [ create_test_pool_config ( "localhost" , 5432 ) ] ;
780+
781+ let replicas = Replicas :: new (
782+ & Some ( primary_pool) ,
783+ & replica_configs,
784+ LoadBalancingStrategy :: Random ,
785+ ReadWriteSplit :: IncludePrimaryIfReplicaBanned ,
786+ ) ;
787+ replicas. launch ( ) ;
788+
789+ let request = Request :: default ( ) ;
790+
791+ // When no replicas are banned, primary should NOT be used
792+ let mut used_pool_ids = HashSet :: new ( ) ;
793+ for _ in 0 ..20 {
794+ let conn = replicas. get ( & request) . await . unwrap ( ) ;
795+ used_pool_ids. insert ( conn. pool . id ( ) ) ;
796+ }
797+
798+ // Should only use replica pool
799+ assert_eq ! ( used_pool_ids. len( ) , 1 ) ;
800+
801+ // Verify primary pool ID is not in the set of used pools
802+ let primary_id = replicas. primary . as_ref ( ) . unwrap ( ) . pool . id ( ) ;
803+ assert ! ( !used_pool_ids. contains( & primary_id) ) ;
804+
805+ // Shutdown both primary and replicas
806+ replicas. primary . as_ref ( ) . unwrap ( ) . pool . shutdown ( ) ;
807+ replicas. shutdown ( ) ;
808+ }
809+
810+ #[ tokio:: test]
811+ async fn test_include_primary_if_replica_banned_with_ban ( ) {
812+ let primary_config = create_test_pool_config ( "127.0.0.1" , 5432 ) ;
813+ let primary_pool = Pool :: new ( & primary_config) ;
814+ primary_pool. launch ( ) ;
815+
816+ let replica_configs = [ create_test_pool_config ( "localhost" , 5432 ) ] ;
817+
818+ let replicas = Replicas :: new (
819+ & Some ( primary_pool) ,
820+ & replica_configs,
821+ LoadBalancingStrategy :: Random ,
822+ ReadWriteSplit :: IncludePrimaryIfReplicaBanned ,
823+ ) ;
824+ replicas. launch ( ) ;
825+
826+ // Ban the replica
827+ let replica_ban = & replicas. replicas [ 0 ] . ban ;
828+ replica_ban. ban ( Error :: ServerError , Duration :: from_millis ( 1000 ) ) ;
829+
830+ let request = Request :: default ( ) ;
831+
832+ // When replica is banned, primary SHOULD be used
833+ let mut used_pool_ids = HashSet :: new ( ) ;
834+ for _ in 0 ..20 {
835+ let conn = replicas. get ( & request) . await . unwrap ( ) ;
836+ used_pool_ids. insert ( conn. pool . id ( ) ) ;
837+ }
838+
839+ // Should only use primary pool since replica is banned
840+ assert_eq ! ( used_pool_ids. len( ) , 1 ) ;
841+
842+ // Verify primary pool ID is in the set of used pools
843+ let primary_id = replicas. primary . as_ref ( ) . unwrap ( ) . pool . id ( ) ;
844+ assert ! ( used_pool_ids. contains( & primary_id) ) ;
845+
846+ // Shutdown both primary and replicas
847+ replicas. primary . as_ref ( ) . unwrap ( ) . pool . shutdown ( ) ;
848+ replicas. shutdown ( ) ;
849+ }
0 commit comments